LocalDateTime ZoneId example

LocalDateTime ZoneId example

LocalDateTime class added in JDK 8 and here it gives date time without time zone in ISO-8601 calendar system and if you want to specify explicitly ZoneId to get the time please see example below:

  • LocalDateTimeEuropeParis.java:
package com.javahonk.dateformatter;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeEuropeParis {

	public static void main(String[] args) {
		
		String validPatterns1 = "EEE, MMM d, ''yy";
		String validPatterns2 = "h:mm a";
		String validPatterns3 = "YYYY-'W'ww-u";
		String validPatterns4 = "yyyy-MM-dd";
		String validPatterns5 = "yyyy-MM-dd HH:mm:ss";
		
		System.out.println("LocalDateTime in Europe/Pari format with different pattern: \n");
		
		LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("Europe/Paris"));
		
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern(validPatterns1);
		System.out.println("EEE, MMM d, ''yy:--> "+localDateTime.format(formatter));
		
		formatter = DateTimeFormatter.ofPattern(validPatterns2);
		System.out.println("h:mm a:--> "+localDateTime.format(formatter));
		
		formatter = DateTimeFormatter.ofPattern(validPatterns3);
		System.out.println("YYYY-'W'ww-u:--> "+localDateTime.format(formatter));
		
		formatter = DateTimeFormatter.ofPattern(validPatterns4);
		System.out.println("yyyy-MM-dd:--> "+localDateTime.format(formatter));
		
		formatter = DateTimeFormatter.ofPattern(validPatterns5);
		System.out.println("yyyy-MM-dd HH:mm:ss:--> "+localDateTime.format(formatter));
		
		System.out.println("BASIC_ISO_DATE:--> "+localDateTime.format(DateTimeFormatter.BASIC_ISO_DATE));
		
		System.out.println("ISO_DATE:--> "+localDateTime.format(DateTimeFormatter.ISO_DATE));
		
		System.out.println("ISO_DATE_TIME:--> "+localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));

	}

}
  • Output:

LocalDateTime ZoneId example

  • For more information take look at Oracle API documentation here

Leave a Reply

Your email address will not be published. Required fields are marked *