Wednesday, May 17, 2017

Java 8 Date and Time API: LocalDateTime


Java 8 date and time API introduces a new class LocalDateTime under java.time package which represents a local date and time without a timezone. LocalDateTime instances are immutable (cannot be changed) so any operations performed on LocalDateTime will always return a new instance.

In this tutorial, you will learn:
  • How to create a LocalDateTime instance representing the current date and time
  • How to create a LocalDateTime instance from a given year, month, day, hour, minute and second
  • How to create a LocalDateTime instance from a given LocalDate and LocalTime
  • How to access the LocalDateTime properties (e.g. month, year, day of week, etc.)
  • How to perform plus and minus operations on LocalDateTime
  • How to convert LocalDateTime to ZonedDateTime (date and time with timezone)
Example
package com.melody.datetime;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.TemporalAdjusters;

public class LocalDateTimeDemo {

 public static void main(String[] args) {
  LocalDateTime currentLocalDateTime = LocalDateTime.now();
  System.out.println("Current local date and time: " + currentLocalDateTime);

  // Create a LocalDateTime instance from a given year, month,
  // day, hour, minute and second
  LocalDateTime localDateTime = LocalDateTime.of(2017, 11, 14, 13, 45, 30);
  System.out.println("Local date & time from a given year, month, day, hour, minute & second: "
    + localDateTime);

  // Create a LocalDateTime instance from date and time
  LocalDate localDate = LocalDate.of(2017, 11, 14);
  LocalTime localTime = LocalTime.of(13, 30, 44);
  LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime);
  System.out.println("LocalDateTime instance from date and time: " + localDateTime2);

  // Access the LocalDateTime properties
  System.out.println("Get date: " + localDateTime.toLocalDate());
  System.out.println("Get time: " + localDateTime.toLocalTime());
  System.out.println("Get year: " + localDateTime.getYear());
  System.out.println("Get month (enum): " + localDateTime.getMonth());
  System.out.println("Get month: " + localDateTime.getMonthValue());
  System.out.println("Get day of month: " + localDateTime.getDayOfMonth());
  System.out.println("Get hour: " + localDateTime.getHour());
  System.out.println("Get minute: " + localDateTime.getMinute());
  System.out.println("Get second: " + localDateTime.getSecond());

  // Add and minus operations on LocalDateTime
  System.out.println("Add 1 yr, 1 month & 1 day to 2017-11-14: " + localDateTime.plusYears(1).plusMonths(1).plusDays(1));
  System.out.println("Minus 1 yr, 1 month & 1 day from 2017-11-14: " + localDateTime.minusYears(1).minusMonths(1).minusDays(1));
  System.out.println("Add 1 hour, 1 minute & 1 second to 13:45:30 = " + localDateTime.plusHours(1).plusMinutes(1).plusSeconds(1));
  System.out.println("Minus 1 hour, 1 minute & 1 second to 13:45:30 = " + localDateTime.minusHours(1).minusMinutes(1).minusSeconds(1));

  // Adjust the LocalDateTime
  System.out.println("Adjust 2017-11-14 into 2019-12-14: " + localDateTime.withYear(2019).withMonth(12));
  System.out.println("Adjust 13:45:30 into 15:30:30 = " + localDateTime.withHour(15).withMinute(30));

  // Adjust the LocalDateTime using TemporalAdjusters
  System.out.println("Adjust 2017-11-14 into first day of the month: " + localDateTime.with(TemporalAdjusters.firstDayOfMonth()));

  // Convert LocalDateTime to ZonedDateTime (date & time with timezone)
  ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
  System.out.println("Convert LocalDateTime to ZonedDateTime: " + zonedDateTime);
 }
}
Output

Current local date and time: 2017-05-17T20:23:56.230
Local date & time from a given year, month, day, hour, minute & second: 2017-11-14T13:45:30
LocalDateTime instance from date and time: 2017-11-14T13:30:44
Get date: 2017-11-14
Get time: 13:45:30
Get year: 2017
Get month (enum): NOVEMBER
Get month: 11
Get day of month: 14
Get hour: 13
Get minute: 45
Get second: 30
Add 1 yr, 1 month & 1 day to 2017-11-14: 2018-12-15T13:45:30
Minus 1 yr, 1 month & 1 day from 2017-11-14: 2016-10-13T13:45:30
Add 1 hour, 1 minute & 1 second to 13:45:30 = 2017-11-14T14:46:31
Minus 1 hour, 1 minute & 1 second to 13:45:30 = 2017-11-14T12:44:29
Adjust 2017-11-14 into 2019-12-14: 2019-12-14T13:45:30
Adjust 13:45:30 into 15:30:30 = 2017-11-14T15:30:30
Adjust 2017-11-14 into first day of the month: 2017-11-01T13:45:30
Convert LocalDateTime to ZonedDateTime: 2017-11-14T13:45:30+08:00[Asia/Shanghai]



No comments:

Post a Comment