Java8 时间处理举例2
代码:
// 示例1: Java 8中获取今天的日期 @Test public void test01() { LocalTime localTime = LocalTime.now(); System.out.println("当前时间:" + localTime); LocalDate localDate = LocalDate.now(); System.out.println("当前日期:" + localDate); } // 示例2:Java 8中获取年、月、日信息 @Test public void test02() { LocalDate today = LocalDate.now(); int year = today.getYear(); int month = today.getMonthValue(); int day = today.getDayOfMonth(); System.out.println("year:" + year); System.out.println("month:" + month); System.out.println("day:" + day); } // 示例3:Java 8中处理特定日期 @Test public void test03() { LocalDate date = LocalDate.of(2018, 2, 6); System.out.println("自定义日期:" + date); } // 示例4:Java 8中判断两个日期是否相等 @Test public void test04() { LocalDate date1 = LocalDate.now(); LocalDate date2 = LocalDate.of(2018, 2, 5); if (date1.equals(date2)) { System.out.println("时间相等"); } else { System.out.println("时间不等"); } } // 示例5:Java 8中检查像生日这种周期性事件 @Test public void test05() { LocalDate date1 = LocalDate.now(); LocalDate date2 = LocalDate.of(2018, 2, 6); MonthDay birthday = MonthDay.of(date2.getMonth(), date2.getDayOfMonth()); MonthDay currentMonthDay = MonthDay.from(date1); if (currentMonthDay.equals(birthday)) { System.out.println("是你的生日"); } else { System.out.println("你的生日还没有到"); } } // 示例6:Java 8中获取当前时间 @Test public void test06() { LocalTime time = LocalTime.now(); System.out.println("获取当前的时间,不含有日期:" + time); } // 示例7:Java 8中获取当前时间 @Test public void test07() { LocalTime time = LocalTime.now(); LocalTime newTime = time.plusHours(3); System.out.println("三个小时后的时间为:" + newTime); } // 示例8:Java 8如何计算一周后的日期 @Test public void test08() { LocalDate today = LocalDate.now(); System.out.println("今天的日期为:" + today); LocalDate nextWeek = today.plus(1, ChronoUnit.WEEKS); System.out.println("一周后的日期为:" + nextWeek); } // 示例9:Java 8计算一年前或一年后的日期 @Test public void test09() { LocalDate today = LocalDate.now(); LocalDate previousYear = today.minus(1, ChronoUnit.YEARS); System.out.println("一年前的日期 : " + previousYear); LocalDate nextYear = today.plus(1, ChronoUnit.YEARS); System.out.println("一年后的日期:" + nextYear); } // 示例10:Java 8的Clock时钟类 @Test public void test10() { // Returns the current time based on your system clock and set to UTC. Clock clock = Clock.systemUTC(); System.out.println("Clock : " + clock.millis()); // Returns time based on system clock zone Clock defaultClock = Clock.systemDefaultZone(); System.out.println("Clock : " + defaultClock.millis()); } // 示例11:如何用Java判断日期是早于还是晚于另一个日期 @Test public void test11() { LocalDate today = LocalDate.now(); LocalDate tomorrow = LocalDate.of(2018, 2, 6); if (tomorrow.isAfter(today)) { System.out.println("之后的日期:" + tomorrow); } LocalDate yesterday = today.minus(1, ChronoUnit.DAYS); if (yesterday.isBefore(today)) { System.out.println("之前的日期:" + yesterday); } } // 示例16:在Java 8中获取当前的时间戳 @Test public void test16() { Instant timestamp = Instant.now(); System.out.println("What is value of this instant " + timestamp.toEpochMilli()); } // 示例17:Java 8中如何使用预定义的格式化工具去解析或格式化日期 @Test public void test17() { String dayAfterTommorrow = "20180205"; LocalDate formatted = LocalDate.parse(dayAfterTommorrow, DateTimeFormatter.BASIC_ISO_DATE); System.out.println(dayAfterTommorrow + " 格式化后的日期为: " + formatted); } // 示例18:字符串互转日期类型 @Test public void test18() { 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); }
参考文章:
https://mp.weixin.qq.com/s/jb3GTiQUeGRq0rF_gfVbIQ