JSR 310
Date and Time API
©2015 Ady
-- everything about date and time in Java 8
JSR 310
https://jcp.org/en/jsr/detail?id=310
Classes
Class/Enum Y M D H m s Z toString()
Instant √ 2014-12-16T09:42:57.054Z
LocalDate √ √ √ 2014-12-16
LocateDateTime √ √ √ √ √ √ 2014-12-16T17:42:57.196
ZonedDateTime √ √ √ √ √ √ √ 2014-12-16T17:42:57.197+08:00[Asia/Chongqing]
LocalTime √ √ √ 17:42:57.197
MonthDay √ √ --12-16
Year √ 2014
YearMonth √ √ 2014-12
Month √ DECEMBER
OffsetDateTime √ √ √ √ √ √ √ 2014-12-16T17:42:57.231+08:00
OffsetTime √ √ √ √ 17:42:57.232+08:00
Duration √ PT3H
Period √ √ √ P2014Y12M31D
LocalDate today = LocalDate.now();
LocalDate today = LocalDate.now();
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
LocalDate dayOfBirth = LocalDate.of(2015,10,14);
LocalDate date1 = LocalDate.of(2015,10,14);
if( date1.equals(today) ){
}
MonthDay birthday = MonthDay.of(12,4);
LocalTime = LocalTime.now();
LocalDateTime now = LocalDateTime.now();
System.out.println(now); //2015-10-14T11:23:50.083
LocalTime now = LocalTime.now();
LocalTime oneHour = now.plusHours(1);
LocalDate now = LocalDate.now();
System.out.println(now);
LocalDate nextMonth = now.plus(1, ChronoUnit.MONTHS);
System.out.println(nextMonth);
LocalDate now = LocalDate.now();
LocalDate next = LocalDate.of(2015,10,14);
boolean r = now.isBefore(next);
boolean r2 = now.isAfter(next);
YearMonth ym = YearMonth.of(2016, Month.FEBRUARY);
System.out.println(ym.isLeapYear()); // true
System.out.println(ym.lengthOfMonth()); // 29
System.out.println(ym.lengthOfYear()); // 366
.
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(2012,10,15);
Period p = Period.between(birthday, today);
System.out.println(p); // P2Y11M29D
LocalDate birthday = LocalDate.of(2012, 10, 15);
LocalDate today = LocalDate.now();
System.out.println(ChronoUnit.DAYS.between(birthday, today)); //1094
System.out.println(ChronoUnit.MONTHS.between(birthday, today)); //35
LocalDateTime now = LocalDateTime.now();
LocalDateTime birthDay = LocalDateTime.of(2012,10,15,9,30);
Duration d = Duration.between(birthDay,now);
System.out.println(d); //PT26258H17M34.68S
System.out.println(d.toDays()); //1094
ZonedDateTime now = ZonedDateTime.now();
ZoneId parisZoneId = ZoneId.of("Europe/Paris");
ZonedDateTime parisNow = now.withZoneSameInstant(parisZoneId);
System.out.println("Beijing: " + now); //Beijing: 2015-10-14T10:46:23.397+08:00[Asia/Shanghai]
System.out.println("Paris: " + parisNow);//Paris: 2015-10-14T04:46:23.397+02:00[Europe/Paris]
Instant now = Instant.now();
System.out.println(now.toString()); // 2015-10-14T03:18:52.324Z
System.out.println(now.toEpochMilli()); // 1444792732324
System.out.println(now.getEpochSecond()); // 1444792732
System.out.println(Clock.systemUTC().millis()); // 1444792912772
System.out.println(Clock.systemDefaultZone().millis()); // 1444792912772
System.out.println(System.currentTimeMillis()); //1444792912772
LocalDateTime now = LocalDateTime.now();
System.out.println(now); //2015-10-14T11:23:50.083
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(now.format(format)); //2015-10-14 11:23:50
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dtime = LocalDateTime.parse("2015-10-14 11:23:50", format);
Main Method
now static factory instance of now LocalDateTime.now()
of static factory create instance LocalDate.of(2014,12,16)
from static factory convert input to target YearMonth.from(localDate)
parse static factory parse string to target YearMonth.parse("2014-12")
format instance format instance to string LocalDate.format(DateTimeFormatter.ISO_DATE)
get instance get a part LocalDate.get(ChronoField.DAY_OF_YEAR)
is instance compare with LocalDate.isBefore/isAfter/isEqual
with instance return copy with changed LocalDate.with(ChronoField.YEAR,2016)
plus instance return copy with added LocalDate.plusYears(1)
minus instance return copy with subtracted LocalDateTime.minusMonths(1)
to instance convert instance to target LocalDateTime.toLocalTime()
at instance combine two instance LocalDate.atTime(12,30,0)
Date now = new Date();
Instant ins = now.toInstant();
Date oldDate = new Date(ins.toEpochMilli());
Date oldDate2 = Date.from(ins);
Instant ins2 = LocalDateTime.now().toInstant(ZoneOffset.UTC);
Timestamp t = Timestamp.valueOf(LocalDateTime.now());
Instant ins3 = t.toInstant();
Instant
1
2
/Instants 1444792732324
/Periods P2Y11M29D
/Dates 2015-10-14
/Times 11:42:45
/Timezones +08:00/Asia/Shanghai
/Durations PT26258H17M34.68S
https://zh.wikipedia.org/wiki/ISO_8601
3
Instant
LocalDate
LocalTime
LocalDateTime
ZonedDateTime
4
ZoneId
ChronoUnit
DateTimeFormatter
Duration
Period
OffsetDateTime
The End
https://github.com/adyliu
http://www.slideshare.net/xylz/

Jsr310 - Java 8 Date and Time API

  • 1.
    JSR 310 Date andTime API ©2015 Ady -- everything about date and time in Java 8
  • 2.
  • 3.
    Classes Class/Enum Y MD H m s Z toString() Instant √ 2014-12-16T09:42:57.054Z LocalDate √ √ √ 2014-12-16 LocateDateTime √ √ √ √ √ √ 2014-12-16T17:42:57.196 ZonedDateTime √ √ √ √ √ √ √ 2014-12-16T17:42:57.197+08:00[Asia/Chongqing] LocalTime √ √ √ 17:42:57.197 MonthDay √ √ --12-16 Year √ 2014 YearMonth √ √ 2014-12 Month √ DECEMBER OffsetDateTime √ √ √ √ √ √ √ 2014-12-16T17:42:57.231+08:00 OffsetTime √ √ √ √ 17:42:57.232+08:00 Duration √ PT3H Period √ √ √ P2014Y12M31D
  • 4.
    LocalDate today =LocalDate.now();
  • 5.
    LocalDate today =LocalDate.now(); int year = today.getYear(); int month = today.getMonthValue(); int day = today.getDayOfMonth();
  • 6.
    LocalDate dayOfBirth =LocalDate.of(2015,10,14);
  • 7.
    LocalDate date1 =LocalDate.of(2015,10,14); if( date1.equals(today) ){ }
  • 8.
    MonthDay birthday =MonthDay.of(12,4);
  • 9.
  • 10.
    LocalDateTime now =LocalDateTime.now(); System.out.println(now); //2015-10-14T11:23:50.083
  • 11.
    LocalTime now =LocalTime.now(); LocalTime oneHour = now.plusHours(1);
  • 12.
    LocalDate now =LocalDate.now(); System.out.println(now); LocalDate nextMonth = now.plus(1, ChronoUnit.MONTHS); System.out.println(nextMonth);
  • 13.
    LocalDate now =LocalDate.now(); LocalDate next = LocalDate.of(2015,10,14); boolean r = now.isBefore(next); boolean r2 = now.isAfter(next);
  • 14.
    YearMonth ym =YearMonth.of(2016, Month.FEBRUARY); System.out.println(ym.isLeapYear()); // true System.out.println(ym.lengthOfMonth()); // 29 System.out.println(ym.lengthOfYear()); // 366 .
  • 15.
    LocalDate today =LocalDate.now(); LocalDate birthday = LocalDate.of(2012,10,15); Period p = Period.between(birthday, today); System.out.println(p); // P2Y11M29D
  • 16.
    LocalDate birthday =LocalDate.of(2012, 10, 15); LocalDate today = LocalDate.now(); System.out.println(ChronoUnit.DAYS.between(birthday, today)); //1094 System.out.println(ChronoUnit.MONTHS.between(birthday, today)); //35
  • 17.
    LocalDateTime now =LocalDateTime.now(); LocalDateTime birthDay = LocalDateTime.of(2012,10,15,9,30); Duration d = Duration.between(birthDay,now); System.out.println(d); //PT26258H17M34.68S System.out.println(d.toDays()); //1094
  • 18.
    ZonedDateTime now =ZonedDateTime.now(); ZoneId parisZoneId = ZoneId.of("Europe/Paris"); ZonedDateTime parisNow = now.withZoneSameInstant(parisZoneId); System.out.println("Beijing: " + now); //Beijing: 2015-10-14T10:46:23.397+08:00[Asia/Shanghai] System.out.println("Paris: " + parisNow);//Paris: 2015-10-14T04:46:23.397+02:00[Europe/Paris]
  • 19.
    Instant now =Instant.now(); System.out.println(now.toString()); // 2015-10-14T03:18:52.324Z System.out.println(now.toEpochMilli()); // 1444792732324 System.out.println(now.getEpochSecond()); // 1444792732
  • 20.
    System.out.println(Clock.systemUTC().millis()); // 1444792912772 System.out.println(Clock.systemDefaultZone().millis());// 1444792912772 System.out.println(System.currentTimeMillis()); //1444792912772
  • 21.
    LocalDateTime now =LocalDateTime.now(); System.out.println(now); //2015-10-14T11:23:50.083 DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(now.format(format)); //2015-10-14 11:23:50
  • 22.
    DateTimeFormatter format =DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dtime = LocalDateTime.parse("2015-10-14 11:23:50", format);
  • 23.
    Main Method now staticfactory instance of now LocalDateTime.now() of static factory create instance LocalDate.of(2014,12,16) from static factory convert input to target YearMonth.from(localDate) parse static factory parse string to target YearMonth.parse("2014-12") format instance format instance to string LocalDate.format(DateTimeFormatter.ISO_DATE) get instance get a part LocalDate.get(ChronoField.DAY_OF_YEAR) is instance compare with LocalDate.isBefore/isAfter/isEqual with instance return copy with changed LocalDate.with(ChronoField.YEAR,2016) plus instance return copy with added LocalDate.plusYears(1) minus instance return copy with subtracted LocalDateTime.minusMonths(1) to instance convert instance to target LocalDateTime.toLocalTime() at instance combine two instance LocalDate.atTime(12,30,0)
  • 24.
    Date now =new Date(); Instant ins = now.toInstant(); Date oldDate = new Date(ins.toEpochMilli()); Date oldDate2 = Date.from(ins); Instant ins2 = LocalDateTime.now().toInstant(ZoneOffset.UTC); Timestamp t = Timestamp.valueOf(LocalDateTime.now()); Instant ins3 = t.toInstant(); Instant
  • 25.
  • 26.
    2 /Instants 1444792732324 /Periods P2Y11M29D /Dates2015-10-14 /Times 11:42:45 /Timezones +08:00/Asia/Shanghai /Durations PT26258H17M34.68S https://zh.wikipedia.org/wiki/ISO_8601
  • 27.
  • 28.
  • 29.
  • 30.