Java 8 – Parse string to date-time in UTC

Learn to parse a given date time string to instance e.g. ZonedDateTime or OffsetDateTime classes, using DateTimeFormatter class in Java 8.

1. Instant, OffsetDateTime and ZonedDateTime

In Java 8, OffsetDateTime, ZonedDateTime and Instant – all store an instant on the time-line to nanosecond precision. Instant is the simplest, simply representing the epoch-seconds (without offset information). OffsetDateTime adds to the instant the offset from UTC, which allows the local date-time to be obtained. ZonedDateTime uses full time-zone rules while handling dates.

  • Use Instant to record event time-stamps in the application.
  • Use OffsetDateTime when modeling date-time concepts in more detail, or when communicating to a database or in a network protocol.
  • Use ZonedDateTime for displaying the time in UI. It honors DST (Daylight Saving Time) rules.

2. Parse string to OffsetDateTime in UTC

Date time with offset information is represented in dd/MM/uuuu’T’HH:mm:ss:SSSXXXXX pattern. e.g. "03/08/2019T16:20:17:717+05:30". In this example, timestamp represents one instant at "+05:30" offset.

Java program to convert string to OffsetDateTime and get equivalent instant in UTC.

‘Z’ in date string represents the UTC timezone. It is short form of Zulu and can be written as UTC +0:00.

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class Main {

	public static void main(String[] args) throws Exception 
	{
		DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
								.ofPattern("dd/MM/uuuu'T'HH:mm:ss:SSSXXXXX");
		
		//Date string with offset information
		String dateString = "03/08/2019T16:20:17:717+05:30";

		//Instance with given offset
		OffsetDateTime odtInstanceAtOffset = OffsetDateTime.parse(dateString, DATE_TIME_FORMATTER);

		//Instance in UTC
		OffsetDateTime odtInstanceAtUTC = odtInstanceAtOffset.withOffsetSameInstant(ZoneOffset.UTC);
		
		//Formatting to string
		String dateStringInUTC = odtInstanceAtUTC.format(DATE_TIME_FORMATTER);
		
		System.out.println(odtInstanceAtOffset);
		System.out.println(odtInstanceAtUTC);
		System.out.println(dateStringInUTC);

		//Convert OffsetDateTime to instant which is in UTC
		System.out.println(odtInstanceAtOffset.toInstant());
	}
}

Program output.

2019-08-03T16:20:17.717+05:30
2019-08-03T10:50:17.717Z
03/08/2019T10:50:17:717Z
2019-08-03T10:50:17.717Z

3. Parse string to ZonedDateTime in UTC

Date time with full zone information is represented in following formats.

dd/MM/uuuu’T’HH:mm:ss:SSSXXXXX pattern. e.g. "03/08/2019T16:20:17:717+05:30".
MM/dd/yyyy’T’HH:mm:ss:SSS z pattern. e.g. "08/03/2019T16:20:17:717 UTC+05:30".

In this example, timestamp represents one instant at "+05:30" offset i.e. IST.

Java program to convert string to ZonedDateTime and get equivalent instant in UTC.

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {

	public static void main(String[] args) throws Exception 
	{
		DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter
								.ofPattern("MM/dd/yyyy'T'HH:mm:ss:SSS z");
		
		//Date string with zone information
		String dateString = "08/03/2019T16:20:17:717 UTC+05:30";

		//Instance with given zone
		ZonedDateTime zdtInstanceAtOffset = ZonedDateTime.parse(dateString, DATE_TIME_FORMATTER);

		//Instance in UTC
		ZonedDateTime zdtInstanceAtUTC = zdtInstanceAtOffset.withZoneSameInstant(ZoneOffset.UTC);
		
		//Formatting to string
		String dateStringInUTC = zdtInstanceAtUTC.format(DATE_TIME_FORMATTER);
		
		System.out.println(zdtInstanceAtOffset);
		System.out.println(zdtInstanceAtUTC);
		System.out.println(dateStringInUTC);
		
		//Convert ZonedDateTime to instant which is in UTC
		System.out.println(zdtInstanceAtOffset.toInstant());
	}
}

Program output.

2019-08-03T16:20:17.717+05:30[UTC+05:30]
2019-08-03T10:50:17.717Z
08/03/2019T10:50:17:717 Z
2019-08-03T10:50:17.717Z

Drop me your questions related parsing string to ZonedDateTime or OffsetDateTime in UTC.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

1 thought on “Java 8 – Parse string to date-time in UTC”

  1. Hi Lokesh,
    Thanks for giving this option here.
    I have datetime as String “2020-02-02T09:01:21.507Z” and want to convert into java.util.Date.
    So, Could you help me on that.

    Thanks,
    Ravindra

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.