jsr310 - java 8 date and time api

30
JSR 310 Date and Time API ©2015 Ady -- everything about date and time in Java 8

Upload: ady-liu

Post on 12-Jan-2017

299 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Jsr310 - Java 8 Date and Time API

JSR 310Date and Time API

©2015 Ady

-- everything about date and time in Java 8

Page 2: Jsr310 - Java 8 Date and Time API

JSR 310

https://jcp.org/en/jsr/detail?id=310

Page 3: Jsr310 - Java 8 Date and Time API

ClassesClass/Enum Y M D H m s Z toString()Instant √ 2014-12-16T09:42:57.054ZLocalDate √ √ √ 2014-12-16LocateDateTime √ √ √ √ √ √ 2014-12-16T17:42:57.196ZonedDateTime √ √ √ √ √ √ √ 2014-12-16T17:42:57.197+08:00[Asia/Chongqing]LocalTime √ √ √ 17:42:57.197MonthDay √ √ --12-16Year √ 2014YearMonth √ √ 2014-12Month √ DECEMBEROffsetDateTime √ √ √ √ √ √ √ 2014-12-16T17:42:57.231+08:00OffsetTime √ √ √ √ 17:42:57.232+08:00Duration √ PT3HPeriod √ √ √ P2014Y12M31D

Page 4: Jsr310 - Java 8 Date and Time API

LocalDate today = LocalDate.now();

Page 5: Jsr310 - Java 8 Date and Time API

LocalDate today = LocalDate.now();

int year = today.getYear();

int month = today.getMonthValue();

int day = today.getDayOfMonth();

Page 6: Jsr310 - Java 8 Date and Time API

LocalDate dayOfBirth = LocalDate.of(2015,10,14);

Page 7: Jsr310 - Java 8 Date and Time API

LocalDate date1 = LocalDate.of(2015,10,14);

if( date1.equals(today) ){

}

Page 8: Jsr310 - Java 8 Date and Time API

MonthDay birthday = MonthDay.of(12,4);

Page 9: Jsr310 - Java 8 Date and Time API

LocalTime = LocalTime.now();

Page 10: Jsr310 - Java 8 Date and Time API

LocalDateTime now = LocalDateTime.now();

System.out.println(now); //2015-10-14T11:23:50.083

Page 11: Jsr310 - Java 8 Date and Time API

LocalTime now = LocalTime.now();

LocalTime oneHour = now.plusHours(1);

Page 12: Jsr310 - Java 8 Date and Time API

LocalDate now = LocalDate.now();

System.out.println(now);

LocalDate nextMonth = now.plus(1, ChronoUnit.MONTHS);

System.out.println(nextMonth);

Page 13: Jsr310 - Java 8 Date and Time API

LocalDate now = LocalDate.now();

LocalDate next = LocalDate.of(2015,10,14);

boolean r = now.isBefore(next);

boolean r2 = now.isAfter(next);

Page 14: Jsr310 - Java 8 Date and Time API

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

.

Page 15: Jsr310 - Java 8 Date and Time API

LocalDate today = LocalDate.now();

LocalDate birthday = LocalDate.of(2012,10,15);

Period p = Period.between(birthday, today);

System.out.println(p); // P2Y11M29D

Page 16: Jsr310 - Java 8 Date and Time API

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

Page 17: Jsr310 - Java 8 Date and Time API

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

Page 18: Jsr310 - Java 8 Date and Time API

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]

Page 19: Jsr310 - Java 8 Date and Time API

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

Page 20: Jsr310 - Java 8 Date and Time API

System.out.println(Clock.systemUTC().millis()); // 1444792912772

System.out.println(Clock.systemDefaultZone().millis()); // 1444792912772

System.out.println(System.currentTimeMillis()); //1444792912772

Page 21: Jsr310 - Java 8 Date and Time API

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

Page 22: Jsr310 - Java 8 Date and Time API

DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

LocalDateTime dtime = LocalDateTime.parse("2015-10-14 11:23:50", format);

Page 23: Jsr310 - Java 8 Date and Time API

Main Methodnow 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)

Page 24: Jsr310 - Java 8 Date and Time API

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

Page 25: Jsr310 - Java 8 Date and Time API

1

Page 26: Jsr310 - Java 8 Date and Time API

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

Page 27: Jsr310 - Java 8 Date and Time API

3

Instant

LocalDate

LocalTime

LocalDateTime

ZonedDateTime

Page 28: Jsr310 - Java 8 Date and Time API

4ZoneId

ChronoUnit

DateTimeFormatter

Duration

Period

OffsetDateTime

Page 29: Jsr310 - Java 8 Date and Time API

The End

Page 30: Jsr310 - Java 8 Date and Time API

https://github.com/adyliu

http://www.slideshare.net/xylz/