Parse a String to UTC Date Time

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

1. Instant, OffsetDateTime and ZonedDateTime Classes

In Java 8, OffsetDateTime and ZonedDateTime – both store an instant on the timeline to nanosecond precision.

  • OffsetDateTime adds to the instant the offset from UTC, which allows the local date-time to be obtained. We can use OffsetDateTime when modeling date-time concepts in more detail, or when communicating to a database or in a network protocol.
  • ZonedDateTime uses full time-zone rules while handling dates. We can 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, the timestamp represents one instant at "+05:30" offset.

Given below is a Java program to convert string to OffsetDateTime and get an equivalent instant in UTC.

‘Z’ in 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 the 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.

Given below is a Java program to convert string to ZonedDateTime and get an 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 to parsing a string to ZonedDateTime or OffsetDateTime in UTC.

Happy Learning !!

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

2 thoughts on “Parse a String to UTC Date Time”

  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 related technologies, the best practices, algorithms, and interview questions.