Java 8 Date and Time Usage Summary

Posted by fukas on Mon, 23 Mar 2020 18:40:32 +0100

1. LocalDate

1. LocalDate does not contain information about the time of day or any time zone.

2. Creation Method

(1)Create a static factory method LocalDate Example.
LocalDate date = LocalDate.of(2014,3,8)
(2)LocalDate today = LocalDate.now();
(3)LocalDate date = LocalDate.parse("2014-03-18");

3. Read the value of the LocalDate object

(1) Direct reading

LocalDate date = LocalDate.of(2014,3,18);
int year = date.getYear();//2014
Month month = date.getMonth(); //MARCH
int day = date.getDayOfMonth(); //18
DayOfWeek dow = date.getDayOfWeek();//TUESDAY
int len = date.lengthOfMonth(); //31
boolean leap = date.isLeapYear(); //false

(2) Use Temporal Field

LocalDate date = LocalDate.of(2014,3,18);
int year = date.get(ChronoField.YEAR);//2014
int month = date.get(ChronoField.MONTH_OF_YEAR); // 3
int day = date.get(ChronoField.DAY_OF_MONTH); //18

2. LocalTime

1. Creation Method

(1)LocalTime time1  = LocalTime.of(13,3);//Hours, minutes
(2)LocalTime time2  = LocalTime.of(13,3,18);//Hours, minutes, seconds
(3)LocalTime time3 = LocalTime.parse("13:45:50");

2. Read the value of LocalTime

int hour = time3.getHour();
int minute = time3.getMinute();
int second = time3.getSecond();

3. Merge date and time

LocalDate date = LocalDate.parse("2014-03-18");
LocalTime time  = LocalTime.of(13,3);//Hours, minutes
LocalDateTime dt1 = LocalDateTime.of(2014,Month.AUGUST,18,13,45,20);
LocalDateTime dt2 = LocalDateTime.of(date,time);
LocalDateTime dt3 = date.atTime(time);
LocalDateTime dt4 = date.atTime(13,3);
LocalDateTime dt5 = date.atTime(13,3,30);
LocalDateTime dt6 = time.atDate(date);
LocalDate date1 = dt6.toLocalDate();
LocalTime time3 = dt6.toLocalTime();

4. Date and time format of the machine

1. Creation Method

Instant.ofEpochSecond(3);
Instant.ofEpochSecond(3,0);
Instant.ofEpochSecond(2,1000000000);
Instant.ofEpochSecond(4,-1000000000);
Instant.now();

V. Duration

Used to represent the time between two LocalDateTime, two LocalTime, and two Instant s.

1. Creation Method

Instant instant1 =  Instant.ofEpochSecond(3);
Instant instant2 = Instant.ofEpochSecond(3,0);
LocalTime time1  = LocalTime.of(13,3);//Hours, minutes
LocalTime time2  = LocalTime.of(13,3);//Hours, minutes
LocalDateTime dt1 = LocalDateTime.of(2014,Month.AUGUST,18,13,45,20);
LocalDateTime dt2 = LocalDateTime.of(2014,Month.AUGUST,18,13,45,20);
Duration d1 = Duration.between(instant1,instant2);
Duration d2 = Duration.between(time1,time2);
Duration d3 = Duration.between(dt1,dt2);

2. Create a corresponding instance directly

Duration threeMinutes = Duration.ofMinutes(3);
Duration threeMinutes = Duration.of(3, ChronoUnit.MINUTES);

6. Period

Modeling multiple time units in a yearly, monthly, and daily manner.The factory method of this class, between, gives the time between two LocalDate s

1. Creation Method

LocalDate date1 = LocalDate.parse("2014-03-18");
LocalDate date2 = LocalDate.parse("2014-03-18");
Period period = Period.between(date2,date1);

2. Create a corresponding instance directly

Period tenDays = Period.ofDays(10);
Period tenWeeks = Period.ofWeeks(10);
Period tenYearsSixMonthOneDay = Period.of(10,6,1);

7. Date of operation

After manipulating the date using the following method, an object with modified properties is returned.None of them will modify the original object.

1. Manipulate the properties of LocalDate in a more intuitive way

LocalDate date1 = LocalDate.parse("2014-03-18");
LocalDate date2 = date1.withYear(2011);//2011-03-18
LocalDate date3 = date1.withDayOfMonth(24);//2011-03-24
LocalDate date4 = date1.with(ChronoField.MICRO_OF_DAY,9);//2011-09-24

2. Use the with method, which receives a Temporal Field object. All date and time API s implement the get and with methods, which distinguish between reading and modifying Temporal Field.

LocalDate date1 = LocalDate.parse("2014-03-18");
LocalDate startDate =date1.with(TemporalAdjusters.firstDayOfMonth());
LocalDate endtDate = date1.with(TemporalAdjusters.firstDayOfMonth());

3. Relatively modify the properties of the LocalDate object

LocalDate date1 = LocalDate.parse("2014-03-18");
LocalDate date2 =  date1.plusWeeks(1);//2014-03-25
LocalDate date3 = date1.minusYears(3);//2011-03-25
LocalDate date4 = date1.plus(6,ChronoUnit.MONTHS);//2011-09-25

8. More complex operations

For example, if you adjust the date to next Sunday, next weekday, or the last day of this month, you can use an overloaded version of with to pass it a Temporal Adjuster object that provides more customized choices for more flexible processing dates.

LocalDate date1 = LocalDate.parse("2014-03-18");
LocalDate date2 =  date1.with(nextOrSame(DayOfWeek.SATURDAY));//2014-03-23
LocalDate date3 = date1.with(lastDayOfMonth());//2011-03-31