Java8的日期-时间学习

Java 8中获取今天的日期

  • 静态工厂方法now()非常容易地创建了当天日期
LocalDate today = LocalDate.now();
System.out.println(today);

// 2021-04-10

Java 8中获取年、月、日信息

int year = today.getYear();
Month month = today.getMonth();java
int dayOfMonth = today.getDayOfMonth();
DayOfWeek dayOfWeek = today.getDayOfWeek();

System.out.println(year); // 2021
System.out.println(month); // APRIL
System.out.println(dayOfMonth); // 10
System.out.println(dayOfWeek); // SATURDAY

Java 8中处理特定日期

  • 工厂方法LocalDate.of()创建任意日期
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);

时间的加减法

// 20分钟前
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()); // 系统默认时区 Asia/Shanghai

判断日期是早于还是晚于另一个日期

  • isAfter() isBefore()
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); // 当月 2021-04
System.out.println("这个月有: " + currentYearMonth.lengthOfMonth() + "天"); // 这个月有: 30天
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()); // 12 天前的时间戳

解析或格式化日期

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值