常用api
* now():静态方法,返回当前时间。
* of():静态方法,用于创建一个指定日期时间的对象。
* parse():静态方法,将字符串解析为日期时间对象。
* get():返回指定字段的值。
* with():返回一个被指定修改后的副本对象。
* plus():返回增加指定数量的时间后的副本对象。
* minus():返回减去指定数量的时间后的副本对象。
* isBefore():判断当前时间是否在指定时间之前。
* isAfter():判断当前时间是否在指定时间之后。
* isEqual():判断当前时间是否等于指定时间。
* toLocalDate():将当前日期时间转换为LocalDate类型。
* toLocalTime():将当前日期时间转换为LocalTime类型。
* toLocalDateTime():将当前日期时间转换为LocalDateTime类型。
* toInstant():将当前日期时间转换为格林威治时间(Instant类型)。
* format():将当前日期时间转换为指定格式的字符串。
* getMonth():返回月份信息。
* getDayOfWeek():返回星期信息。
* getDayOfMonth():返回月份中的第几天。
* getDayOfYear():返回年份中的第几天。
* withMonth():修改月份信息,返回一个新的日期时间对象。
* withDayOfMonth():修改月份中的天数信息,返回一个新的日期时间对象。
* withYear():修改年份信息,返回一个新的日期时间对象。
* toEpochSecond():将日期时间转换成秒数。
* from():从其他日期时间对象中获取当前对象的值。
* compareTo():用于比较两个日期时间对象的大小。
示例:
//获取当前时间
System.out.println(LocalDateTime.now());
//获取当前时间时分秒
System.out.println(LocalDateTime.now().toLocalTime());
//获取当前时间年月日
System.out.println(LocalDateTime.now().toLocalDate());
//获取当前时间20天之前
System.out.println(LocalDateTime.now().minusDays(20));
//获取当前时间20天之后
System.out.println(LocalDateTime.now().plusDays(20));
//比较大小
System.out.println(LocalDateTime.now().compareTo(LocalDateTime.now().plusDays(2))); //-2
//获取当天的开始时间
LocalDateTime.now().toLocalDate().atStartOfDay()
//获取次日的开始时间
LocalDateTime.now().plusDays(1L).toLocalDate().atStartOfDay()
//获取当天日期
String toDay = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
获取当日到次日的时间
LocalDateTime now = LocalDateTime . now ( ) ;
LocalDateTime next = LocalDateTime . of ( LocalDate . now ( ) . plusDays ( 1L ) , LocalTime . MIN) ;
System . out. println ( "now:" + now) ;
System . out. println ( "next:" + next) ;
System . out. println ( ChronoUnit . SECONDS. between ( now, next) ) ;
LocalDateTime和时间戳互转
public static long toEpochMilli ( LocalDateTime localDateTime) {
return localDateTime. toInstant ( ZoneOffset . of ( "+8" ) ) . toEpochMilli ( ) ;
}
public static LocalDateTime ofEpochMilli ( long epochMilli) {
return LocalDateTime . ofInstant ( Instant . ofEpochMilli ( epochMilli) , ZoneOffset . of ( "+8" ) ) ;
}
public static long toEpochSecond ( LocalDateTime localDateTime) {
return localDateTime. toEpochSecond ( ZoneOffset . of ( "+8" ) ) ;
}
public static LocalDateTime ofEpochSecond ( long epochSecond) {
return LocalDateTime . ofEpochSecond ( epochSecond, 0 , ZoneOffset . of ( "+8" ) ) ;
}
LocalDateTime和Date互转
public static LocalDateTime ofDate ( Date date) {
return date. toInstant ( ) . atOffset ( ZoneOffset . of ( "+8" ) ) . toLocalDateTime ( ) ;
}
public static Date toDate ( LocalDateTime localDateTime) {
return Date . from ( localDateTime. atZone ( ZoneOffset . of ( "+8" ) ) . toInstant ( ) ) ;
}
LocalDateTime和字符串互转
public static String formatToString ( LocalDateTime localDateTime) {
String format = "yyyy:MM:dd HH:mm:ss" ;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter . ofPattern ( format) ;
return localDateTime. format ( dateTimeFormatter) ;
}
public static LocalDateTime stringToFormat ( String localDateTime) {
String format = "yyyy:MM:dd HH:mm:ss" ;
DateTimeFormatter dateTimeFormatter = DateTimeFormatter . ofPattern ( format) ;
return LocalDateTime . parse ( localDateTime, dateTimeFormatter) ;
}