欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java 8 之 新日期时间API

程序员文章站 2022-05-15 11:41:19
...
Joda-Time作者Stephen Colebourne领导完成的JSR-310已经随着Java 8的Release正式发布。JSR-310的API和Joda-Time是兼容的,但经过了重新设计。Google一下网上已经有很多介绍,有人说JSR-310有些过度设计,“不管你喜不喜欢他,他都带着诚意扑面而来了”。

Joda-Time的介绍:http://rensanning.iteye.com/blog/1546652

关于JSR-310 和 Joda-Time
http://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html
https://www.infoq.com/news/2010/03/jsr-310
http://blog.joda.org/2010/12/what-about-jsr-310_153.html
http://blog.joda.org/2014/11/converting-from-joda-time-to-javatime.html

(1)系统时间
LocalDate date = LocalDate.now();
System.out.println(date.getYear() + "/" + date.getMonthValue() + "/" + date.getDayOfMonth()); // 2014/3/21

LocalTime time = LocalTime.now();
System.out.println(time.getHour() + ":" + time.getMinute() + ":" + time.getSecond()); // 16:39:22
// ***没有提供  getMillis() 方法
System.out.println(time.get(ChronoField.MILLI_OF_SECOND)); // 859

LocalDateTime dateTime = LocalDateTime.now();
System.out.println(dateTime.getYear() + "/" + dateTime.getMonthValue() + "/" + dateTime.getDayOfMonth()
		   + " " + dateTime.getHour() + ":" + dateTime.getMinute() + ":" + dateTime.getSecond()); // 2014/3/21 16:39:22

Clock clock = Clock.systemDefaultZone();
System.out.println(clock.millis()); // 1395391162859


(2)特定日期
LocalDate myDate = LocalDate.of(2013, 12, 4);
System.out.println(myDate.getYear() + "/" + myDate.getMonthValue() + "/" + myDate.getDayOfMonth()); // 2013/12/4

LocalDate independenceDay = LocalDate.of(2014, Month.JULY, 4);
DayOfWeek dayOfWeek = independenceDay.getDayOfWeek();
System.out.println(dayOfWeek);    // FRIDAY

LocalTime myTime = LocalTime.of(10, 30, 45);
System.out.println(myTime.getHour() + ":" + myTime.getMinute() + ":" + myTime.getSecond()); // 10:30:45

LocalDateTime myDateTime1 = LocalDateTime.of(2013, 12, 4, 10, 30, 45);
System.out.println(myDateTime1.getYear() + "/" + myDateTime1.getMonthValue() + "/" + myDateTime1.getDayOfMonth()
		   + " " + myDateTime1.getHour() + ":" + myDateTime1.getMinute() + ":" + myDateTime1.getSecond()); // 2013/12/4 10:30:45

LocalDateTime myDateTime2 = LocalDateTime.of(myDate, myTime);
System.out.println(myDateTime2.getYear() + "/" + myDateTime2.getMonthValue() + "/" + myDateTime2.getDayOfMonth()
		   + " " + myDateTime2.getHour() + ":" + myDateTime2.getMinute() + ":" + myDateTime2.getSecond()); // 2013/12/4 10:30:45


(3)格式化
// Date -> String
LocalDate formatDate1 = LocalDate.of(2013, 12, 4);
String dateString = formatDate1.format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));
System.out.println(dateString); // 2013/12/04

// String -> Date
LocalDate formatDate2 = LocalDate.parse("2013/12/04", DateTimeFormatter.ofPattern("yyyy/MM/dd"));
System.out.println(formatDate2); // 2013-12-04


(4)日期转换
// LocalDate -> LocalDateTime
LocalDate changeDate1 = LocalDate.of(2013, 12, 4);
LocalDateTime changeDateTime1 = changeDate1.atTime(10, 30, 45);
System.out.println(changeDateTime1); // 2013-12-04T10:30:45
 
// LocalTime -> LocalDateTime
LocalTime time2 = LocalTime.of(10, 30, 45);
LocalDateTime dateTime2 = time2.atDate(LocalDate.of(2013, 12, 4));
System.out.println(dateTime2); // 2013-12-04T10:30:45
 
// LocalDateTime -> LocalDate, LocalTime
LocalDateTime dateTime3 = LocalDateTime.of(2013, 12, 4, 10, 30, 45);
LocalDate date3 = dateTime3.toLocalDate();
System.out.println(date3); // 2013-12-04
LocalTime time3 = dateTime3.toLocalTime();
System.out.println(time3); // 10:30:45


(5)日期加减
LocalDate today = LocalDate.now();
LocalDate twoDaysAfter = today.plusDays(2L); // 2天后
LocalDate threeDaysBefore = today.minusDays(3L); // 3天前
LocalDate oneYearsAfter = today.plus(1L, ChronoUnit.YEARS);// 1年后
LocalDate twoWeeksBefore = today.minus(2L, ChronoUnit.WEEKS); // 2周前
LocalDate newDate = LocalDate.now().plus(Period.of(3, 2, 1)); // 3年2月1天后


(6)计算间隔
LocalDateTime d1 = LocalDateTime.of(2013, 11, 4, 10, 30, 45);
LocalDateTime d2 = LocalDateTime.of(2013, 12, 4, 10, 30, 45);
Duration duration = Duration.between(d1, d2);
System.out.println(duration.toDays()); // 30


(7)日期比较
LocalDate compDate1 = LocalDate.of(2013, 12, 4);
LocalDate compDate2 = LocalDate.of(2013, 11, 4);
System.out.println(compDate1.isBefore(compDate2)); // false
System.out.println(compDate1.compareTo(compDate2)); // 1

       
(8)和java.util.Date的转换
// LocalDateTime -> Date
LocalDateTime cDateTime = LocalDateTime.now();
Instant instant = cDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date cDate = Date.from(instant);
System.out.println(cDate); // Fri Mar 21 16:39:22 CST 2014
 
// Date -> LocalDateTime
Date date2 = new Date();
Instant instant2 = date2.toInstant();
LocalDateTime dateTimeFromDate = LocalDateTime.ofInstant(instant2, ZoneOffset.systemDefault());
System.out.println(dateTimeFromDate); // 2014-03-21T16:39:22.890

// Calendar -> LocalDateTime
Calendar cal = Calendar.getInstance();
LocalDateTime dateTimeFromCal = LocalDateTime.ofInstant(cal.toInstant(), ZoneOffset.systemDefault());
System.out.println(dateTimeFromCal); // 2014-03-21T16:39:22.890


关于java.util.Date这篇文章说的很详细:
https://codeblog.jonskeet.uk/2017/04/23/all-about-java-util-date/

(9)时区
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId.toString()); // Asia/Shanghai

ZoneOffset zoneOffset = ZoneOffset.ofHours(1);
System.out.println(zoneOffset.toString()); // +01:00

ZoneId zone1 = ZoneId.of("Asia/Tokyo");
System.out.println(zone1.getRules()); // ZoneRules[currentStandardOffset=+09:00]
LocalTime now1 = LocalTime.now(zone1);
System.out.println(now1); // 17:39:22.890

ZonedDateTime jaTime = ZonedDateTime.of(2014, 1, 31, 10, 0, 0, 0, zone1);
System.out.println(jaTime); // 2014-01-31T10:00+09:00[Asia/Tokyo]


(10)日本时间
JapaneseDate jdate1 = JapaneseDate.of(2013, 12, 4);
System.out.println(jdate1); // Japanese Heisei 25-12-04    
JapaneseDate jdate2 = JapaneseDate.of(JapaneseEra.HEISEI, 25, 12, 4);
System.out.println(jdate2); // Japanese Heisei 25-12-04


http://blog.joda.org/2009/11/why-jsr-310-isn-joda-time_4941.html
http://java.dzone.com/articles/deeper-look-java-8-date-and
http://www.coppermine.jp/docs/programming/2013/12/jsr310-tips.html
相关标签: Java 8