LocalDateTime UTC Example
This is an example of LocalDateTime which got introduced in JDK 1.8 to print time in UTC time zone:
- LocalDateTimeUTC.java:
package com.javahonk.dateformatter; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; public class LocalDateTimeUTC { 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 UTC format with different pattern: \n"); LocalDateTime localDateTime = LocalDateTime.now(ZoneId.of("UTC")); 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)); System.out.println("\nLocalDateTime in GMT format with different pattern: \n"); localDateTime = LocalDateTime.now(ZoneId.of("GMT")); 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)); } }
- Ouput:
- For more information please visit Oracle API here