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 universal 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. Remember that zone offset can change for zone id during the DST changes.

2. Parse String to OffsetDateTime in UTC

Date time with offset information is represented in any pattern. For example, if we use the pattern "03/08/2019T16:20:17:717+05:30" then this 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. It uses the function withOffsetSameInstant(ZoneOffset.UTC) to convert a given instant to UTC instant.

‘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 can be 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. It uses the withZoneSameInstant(ZoneOffset.UTC) method for getting the instant in UTC zone id.

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

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode