Issues with Old Date API
Date and Time official documentation.
LocalDate is an immutable date-time object that represents a date, often viewed as year-month-day. Other date fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. For example, the value “2nd October 2007” can be stored in a LocalDate
. This class does not store or represent a time or time-zone.
How to create a LocalDate
// use now() method LocalDate now = LocalDate.now(ZoneId.systemDefault());//2019-09-10 // use of() method LocalDate date1 = LocalDate.of(2019, 6, 20);//2019-06-20 // use parse() method LocalDate date2 = LocalDate.parse("2016-08-16").plusDays(2);//2016-08-18
How to manipulate LocalDate
// add days LocalDate date2 = LocalDate.now().plusDays(2);//2016-08-18 // minus date2 = date2.minus(1, ChronoUnit.MONTHS);//2016-07-18 // plus //date2 = date2.plus(3, ChronoUnit.MONTHS);//2016-10-18
LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week, and week-of-year, can also be accessed. Time is represented to nanosecond precision. For example, the value “2nd October 2007 at 13:45.30.123456789” can be stored in a LocalDateTime
. This class does not store or represent a time-zone.
How to create a LocalDateTime
// use now() method LocalDateTime now = LocalDateTime.now(ZoneId.systemDefault()); // use of() method LocalDateTime date1 = LocalDateTime.of(2019, 6, 20, 0, 0).plusDays(2);// 2019-06-22T00:00
How to manipulate LocalDateTime
// add days LocalDateTime date1 = LocalDateTime.of(2019, 6, 20, 0, 0).plusDays(2);//2019-06-22T00:00 // minus date1 = date1.minus(1, ChronoUnit.MONTHS);//2019-05-22T00:00 // plus date1 = date1.plus(3, ChronoUnit.MONTHS);//2019-08-22T00:00
LocalTime is an immutable date-time object that represents a time, often viewed as hour-minute-second. Time is represented to nanosecond precision. For example, the value “13:45.30.123456789” can be stored in a LocalTime
.
LocalTime now = LocalTime.now(Clock.systemDefaultZone()); System.out.println("now = " + now); LocalTime time1 = LocalTime.of(12, 6).plusHours(2); System.out.println("time1 = " + time1); time1 = time1.minus(1, ChronoUnit.MINUTES); System.out.println("time1 = " + time1); time1 = time1.plus(3, ChronoUnit.HOURS); System.out.println("time1 = " + time1);