Comprehensive Java utility library providing collections, strings, beans, dates, I/O, and numerous other utility functions.
—
Date parsing, formatting, manipulation, and calendar operations with support for both legacy Date API and Java 8+ time API through the DateUtil class.
Convert Date objects to formatted strings using various patterns.
/**
* Format date using specified pattern
* @param date the date to format
* @param format the format pattern (e.g., "yyyy-MM-dd HH:mm:ss")
* @return formatted date string
*/
public static String format(Date date, String format);
/**
* Format date using predefined DateTime format (yyyy-MM-dd HH:mm:ss)
* @param date the date to format
* @return formatted date string
*/
public static String formatDateTime(Date date);
/**
* Format date using predefined Date format (yyyy-MM-dd)
* @param date the date to format
* @return formatted date string
*/
public static String formatDate(Date date);
/**
* Format date using predefined Time format (HH:mm:ss)
* @param date the date to format
* @return formatted time string
*/
public static String formatTime(Date date);Usage Examples:
import cn.hutool.core.date.DateUtil;
import java.util.Date;
Date now = new Date();
// Custom formatting
String custom = DateUtil.format(now, "yyyy-MM-dd HH:mm:ss");
String iso = DateUtil.format(now, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
// Predefined formats
String dateTime = DateUtil.formatDateTime(now); // "2023-01-15 14:30:25"
String dateOnly = DateUtil.formatDate(now); // "2023-01-15"
String timeOnly = DateUtil.formatTime(now); // "14:30:25"Parse strings into Date objects using various patterns with intelligent format detection.
/**
* Parse date string using specified format
* @param dateStr the date string to parse
* @param format the format pattern
* @return parsed Date object
* @throws DateException if parsing fails
*/
public static Date parse(String dateStr, String format);
/**
* Parse date string with automatic format detection
* @param dateStr the date string to parse
* @return parsed Date object
* @throws DateException if parsing fails
*/
public static Date parse(String dateStr);
/**
* Parse date string safely, returning null if parsing fails
* @param dateStr the date string to parse
* @param format the format pattern
* @return parsed Date object or null if parsing fails
*/
public static Date parseQuietly(String dateStr, String format);Usage Examples:
// Explicit format parsing
Date date1 = DateUtil.parse("2023-01-15 14:30:25", "yyyy-MM-dd HH:mm:ss");
Date date2 = DateUtil.parse("15/01/2023", "dd/MM/yyyy");
// Automatic format detection
Date date3 = DateUtil.parse("2023-01-15"); // Detects yyyy-MM-dd
Date date4 = DateUtil.parse("2023-01-15 14:30:25"); // Detects yyyy-MM-dd HH:mm:ss
// Safe parsing
Date date5 = DateUtil.parseQuietly("invalid-date", "yyyy-MM-dd"); // Returns nullPerform date arithmetic and calculate differences between dates.
/**
* Calculate difference between two dates
* @param beginDate start date
* @param endDate end date
* @param unit the unit for the result (MS, SECOND, MINUTE, HOUR, DAY, WEEK)
* @return difference in specified unit
*/
public static long between(Date beginDate, Date endDate, DateUnit unit);
/**
* Add specified amount to date
* @param date base date
* @param amount amount to add (can be negative for subtraction)
* @param dateField Calendar field constant (Calendar.YEAR, Calendar.MONTH, etc.)
* @return new Date with added amount
*/
public static Date offset(Date date, int amount, int dateField);
/**
* Add days to date
* @param date base date
* @param numberOfDays number of days to add
* @return new Date with added days
*/
public static Date offsetDay(Date date, int numberOfDays);
/**
* Add hours to date
* @param date base date
* @param numberOfHours number of hours to add
* @return new Date with added hours
*/
public static Date offsetHour(Date date, int numberOfHours);Usage Examples:
Date startDate = DateUtil.parse("2023-01-01", "yyyy-MM-dd");
Date endDate = DateUtil.parse("2023-01-15", "yyyy-MM-dd");
// Calculate differences
long days = DateUtil.between(startDate, endDate, DateUnit.DAY); // 14
long hours = DateUtil.between(startDate, endDate, DateUnit.HOUR); // 336
long minutes = DateUtil.between(startDate, endDate, DateUnit.MINUTE); // 20160
// Date arithmetic
Date tomorrow = DateUtil.offsetDay(startDate, 1); // 2023-01-02
Date nextWeek = DateUtil.offsetDay(startDate, 7); // 2023-01-08
Date nextHour = DateUtil.offsetHour(startDate, 1); // Add 1 hour
Date lastMonth = DateUtil.offset(startDate, -1, Calendar.MONTH); // Subtract 1 monthWork with date ranges and check if dates fall within specific periods.
/**
* Check if date is in specified range (inclusive)
* @param date date to check
* @param beginDate range start
* @param endDate range end
* @return true if date is within range
*/
public static boolean isIn(Date date, Date beginDate, Date endDate);
/**
* Get start of day (00:00:00.000)
* @param date the date
* @return new Date representing start of day
*/
public static Date beginOfDay(Date date);
/**
* Get end of day (23:59:59.999)
* @param date the date
* @return new Date representing end of day
*/
public static Date endOfDay(Date date);
/**
* Get start of month
* @param date the date
* @return new Date representing start of month
*/
public static Date beginOfMonth(Date date);
/**
* Get end of month
* @param date the date
* @return new Date representing end of month
*/
public static Date endOfMonth(Date date);Compare dates and determine relationships between them.
/**
* Check if two dates are on the same day
* @param date1 first date
* @param date2 second date
* @return true if dates are on the same day
*/
public static boolean isSameDay(Date date1, Date date2);
/**
* Check if date is today
* @param date date to check
* @return true if date is today
*/
public static boolean isToday(Date date);
/**
* Check if date is yesterday
* @param date date to check
* @return true if date is yesterday
*/
public static boolean isYesterday(Date date);
/**
* Compare two dates
* @param date1 first date
* @param date2 second date
* @return negative if date1 < date2, zero if equal, positive if date1 > date2
*/
public static int compare(Date date1, Date date2);Calculate ages and work with birthday-related operations.
/**
* Calculate age from birthday to current date
* @param birthday the birthday
* @return age in years
*/
public static int age(Date birthday);
/**
* Calculate age from birthday to specific date
* @param birthday the birthday
* @param dateToCompare date to compare against
* @return age in years
*/
public static int age(Date birthday, Date dateToCompare);
/**
* Check if current year is leap year
* @return true if current year is leap year
*/
public static boolean isLeapYear();
/**
* Check if specified year is leap year
* @param year the year to check
* @return true if specified year is leap year
*/
public static boolean isLeapYear(int year);Usage Examples:
Date birthday = DateUtil.parse("1990-05-15", "yyyy-MM-dd");
Date today = new Date();
// Age calculation
int currentAge = DateUtil.age(birthday); // Current age
int ageAtDate = DateUtil.age(birthday, DateUtil.parse("2020-01-01", "yyyy-MM-dd"));
// Date comparisons
boolean isToday = DateUtil.isToday(today); // true
boolean sameDay = DateUtil.isSameDay(today, new Date()); // true
// Date ranges
Date startOfDay = DateUtil.beginOfDay(today); // Today at 00:00:00.000
Date endOfDay = DateUtil.endOfDay(today); // Today at 23:59:59.999
Date startOfMonth = DateUtil.beginOfMonth(today); // First day of current month
// Range checking
boolean inRange = DateUtil.isIn(today, startOfMonth, DateUtil.endOfMonth(today)); // true
// Leap year
boolean isLeap = DateUtil.isLeapYear(2024); // true
boolean currentLeap = DateUtil.isLeapYear(); // depends on current yearDateUtil also provides integration with Java 8+ time classes through LocalDateTimeUtil:
public class LocalDateTimeUtil {
public static String format(LocalDateTime localDateTime, String format);
public static LocalDateTime parse(String dateStr, String format);
public static long between(LocalDateTime startTime, LocalDateTime endTime, ChronoUnit unit);
public static LocalDateTime offset(LocalDateTime time, long number, TemporalUnit field);
}public enum DateUnit {
MS(1), // Milliseconds
SECOND(1000), // Seconds
MINUTE(60 * 1000), // Minutes
HOUR(60 * 60 * 1000), // Hours
DAY(24 * 60 * 60 * 1000), // Days
WEEK(7 * 24 * 60 * 60 * 1000); // Weeks
}ZoneUtil provides time zone related utilities:
public class ZoneUtil {
public static ZoneId getZoneId(String zoneId);
public static ZoneOffset getZoneOffset(int offsetHours);
public static Set<String> getAvailableZoneIds();
}Install with Tessl CLI
npx tessl i tessl/maven-cn-hutool--hutool-core