Java 8中获取今天的日期
LocalDate today = LocalDate . now ( ) ;
System . out. println ( today) ;
Java 8中获取年、月、日信息
int year = today. getYear ( ) ;
Month month = today. getMonth ( ) ; java
int dayOfMonth = today. getDayOfMonth ( ) ;
DayOfWeek dayOfWeek = today. getDayOfWeek ( ) ;
System . out. println ( year) ;
System . out. println ( month) ;
System . out. println ( dayOfMonth) ;
System . out. println ( dayOfWeek) ;
Java 8中处理特定日期
LocalDate date = LocalDate . of ( 2018 , 2 , 6 ) ;
System . out. println ( "自定义日期:" + date) ;
Java 8中判断两个日期是否相等
LocalDate date1 = LocalDate . now ( ) ;
LocalDate date2 = LocalDate . of ( 2018 , 2 , 5 ) ;
if ( date1. equals ( date2) ) {
System . out. println ( "时间相等" ) ;
} else {
System . out. println ( "时间不等" ) ;
}
Java 8中检查像生日这种周期性事件
LocalDate date1 = LocalDate . now ( ) ;
LocalDate date2 = LocalDate . of ( 2018 , 4 , 10 ) ;
MonthDay birthday = MonthDay . of ( date2. getMonth ( ) , date2. getDayOfMonth ( ) ) ;
MonthDay currentMonthDay = MonthDay . from ( date1) ;
if ( currentMonthDay. equals ( birthday) ) {
System . out. println ( "是你的生日" ) ;
} else {
System . out. println ( "你的生日还没有到" ) ;
}
Java 8中获取当前时间
LocalTime time = LocalTime . now ( ) ;
System . out. println ( "获取当前的时间,不含有日期:" + time) ;
时间的加减法
LocalTime now = LocalTime . now ( ) ;
LocalTime pre20 = now. minus ( 20 , ChronoUnit . MINUTES) ;
System . out. println ( pre20) ;
LocalTime time = LocalTime . now ( ) ;
LocalTime newTime = time. plusHours ( 3 ) ;
System . out. println ( "三个小时后的时间为:" + newTime) ;
LocalDate now = LocalDate . now ( ) ;
LocalDate nextWeek = now. plus ( 1 , ChronoUnit . WEEKS) ;
System . out. println ( nextWeek) ;
LocalDate now = LocalDate . now ( ) ;
LocalDate lastYear = now. plus ( - 1 , ChronoUnit . YEARS) ;
System . out. println ( lastYear) ;
或者
LocalDate now = LocalDate . now ( ) ;
LocalDate lastYear = now. minus ( 1 , ChronoUnit . YEARS) ;
System . out. println ( lastYear) ;
LocalDate now = LocalDate . now ( ) ;
LocalDate lastDay = now. minusDays ( 1 ) ;
System . out. println ( lastDay) ;
Java 8的Clock时钟类
Clock时钟类用于获取当时的时间戳,或当前时区下的日期时间信息
Clock clock = Clock . systemUTC ( ) ;
System . out. println ( clock. millis ( ) ) ;
System . out. println ( Clock . systemDefaultZone ( ) . getZone ( ) ) ;
判断日期是早于还是晚于另一个日期
LocalDate today = LocalDate . now ( ) ;
LocalDate tomorrow = LocalDate . of ( 2021 , 5 , 6 ) ;
if ( tomorrow. isAfter ( today) ) {
System . out. println ( "之后的日期:" + tomorrow) ;
}
LocalDate yesterday = today. minus ( 1 , ChronoUnit . DAYS) ;
if ( yesterday. isBefore ( today) ) {
System . out. println ( "之前的日期:" + yesterday) ;
}
Java 8中处理时区
ZoneId america = ZoneId . of ( "America/New_York" ) ;
LocalDateTime localDateAndTime = LocalDateTime . now ( ) ;
ZonedDateTime dateAndTimeInNewYork = ZonedDateTime . of ( localDateAndTime, america ) ;
System . out. println ( "Current date and time in a particular timezone : " + dateAndTimeInNewYork) ;
显示固定日期 信用卡到期日,当月有多少天
YearMonth currentYearMonth = YearMonth . now ( ) ;
System . out. println ( currentYearMonth) ;
System . out. println ( "这个月有: " + currentYearMonth. lengthOfMonth ( ) + "天" ) ;
YearMonth creditCardExpiry = YearMonth . of ( 2022 , Month . FEBRUARY) ;
System . out. printf ( "Your credit card expires on %s %n" , creditCardExpiry) ;
Java 8中检查闰年
LocalDate today = LocalDate . now ( ) ;
if ( today. isLeapYear ( ) ) {
System . out. println ( "This year is Leap year" ) ;
} else {
System . out. println ( "2021 is not a Leap year" ) ;
}
计算两个日期之间的天数和月数
LocalDate today = LocalDate . now ( ) ;
LocalDate java8Release = LocalDate . of ( 2018 , 12 , 14 ) ;
Period periodToNextJavaRelease = Period . between ( java8Release, today) ;
int years = periodToNextJavaRelease. getYears ( ) ;
int months = periodToNextJavaRelease. getMonths ( ) ;
int days = periodToNextJavaRelease. getDays ( ) ;
System . out. printf ( "间隔为: %d年 %d 月 %d 天 " , years, months, days) ;
在Java 8中获取当前的时间戳
Instant timestamp = Instant . now ( ) ;
System . out. println ( "What is value of this instant " + timestamp. toEpochMilli ( ) ) ;
System . out. println ( timestamp. minus ( 12 , ChronoUnit . DAYS) . toEpochMilli ( ) ) ;
解析或格式化日期
String dayAfterTommorrow = "20180205" ;
LocalDate formatted = LocalDate . parse ( dayAfterTommorrow,
DateTimeFormatter . BASIC_ISO_DATE) ;
System . out. println ( dayAfterTommorrow+ " 格式化后的日期为: " + formatted) ;
字符串和日期互转
LocalDateTime date = LocalDateTime . now ( ) ;
DateTimeFormatter format1 = DateTimeFormatter . ofPattern ( "yyyy/MM/dd HH:mm:ss" ) ;
String str = date. format ( format1) ;
System . out. println ( "日期转换为字符串:" + str) ;
DateTimeFormatter format2 = DateTimeFormatter . ofPattern ( "yyyy/MM/dd HH:mm:ss" ) ;
LocalDate date2 = LocalDate . parse ( str, format2) ;
System . out. println ( "日期类型:" + date2) ;