- Parse a String to UTC Date Time
- Convert String to DateTime Object in Java
- Use SimpleDateFormat and java.util.Date to Convert a Date String Into DateTime Object in Java
- Use Java 8 Date and Time Library to Convert a Date String Into Date Time Format
- Use joda-time to Convert a Date String Into Date Time Format
- Related Article — Java DateTime
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()); >>
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()); >>
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
Convert String to DateTime Object in Java
- Use SimpleDateFormat and java.util.Date to Convert a Date String Into DateTime Object in Java
- Use Java 8 Date and Time Library to Convert a Date String Into Date Time Format
- Use joda-time to Convert a Date String Into Date Time Format
This article will introduce how we can convert a given string into a DateTime Object using different approaches and examples.
Use SimpleDateFormat and java.util.Date to Convert a Date String Into DateTime Object in Java
SimpleDateFormat is a class used to parse and format a date in locale-sensitive-manner. It allows us to convert a string to DateTime and convert a DateTime object to a string.
In the below code, we use the string date_time , which is parsed by calling the parse method on the SimpleDateFormat instance dateParser . The format in which we want the string date_time to be parsed is specified inside the SimpleDateFormat constructor.
We have also created a new instance of the SimpleDateFormat class with a different format. The parsed date is then formatted and printed.
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateTime public static void main(String [] args) String date_time = "11/27/2020 05:35:00"; SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yy HH:mm:ss"); try Date date = dateParser.parse(date_time); System.out.println(date); SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yy"); System.out.println(dateFormatter.format(date)); > catch (ParseException e) e.printStackTrace(); > > > >
Fri Nov 27 05:35:00 UTC 2020 11/27/20
Use Java 8 Date and Time Library to Convert a Date String Into Date Time Format
The LocaleDate class represents a date in ISO format without time. The DateTimeFormatter class is used for parsing dates in different formats. We can provide custom patterns by calling ofPattern() method on DateTimeFormatter .
LocaleDate has a parse() method, which takes the custom text string and a specific formatter inputFormat to parse and obtain a LocalDate instance. The obtained LocaleDate instance date is then formatted and printed as output.
import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Main public static void main(String [] args) DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss"); LocalDate date = LocalDate.parse("11/27/2020 05:35:00", inputFormat); DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy"); System.out.println(date.format(outputFormat)); > >
Use joda-time to Convert a Date String Into Date Time Format
joda-time is a standard DateTime library that provides a rich set of methods to perform date and time calculations. The maven dependency to include the functionality of this library is given below.
joda-time joda-time 2.10.8
DateTimeFormatter can parse custom representations of date and time.
We create a formatter instance datetimeformat with a custom pattern. Calling parseDateTime on the formatter instance gives a DateTime object using the custom string dateTime .
Here we have created a new DateTimeFormatter instance dateTimeFormatOut with a different custom pattern. The print() method is called on the new instance which prints the DateTime object joda_time in a new format.
import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; public class StringToDateTime public static void main(String [] args) String dateTime = "11/27/2020 05:35:00"; DateTimeFormatter datetimeformat = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss"); DateTime joda_time = datetimeformat.parseDateTime(dateTime); System.out.println("joda_time : "+joda_time); DateTimeFormatter dateTimeFormatOut = DateTimeFormat.forPattern("MM/dd/yyyy"); System.out.println("date time format out: "+dateTimeFormatOut.print(joda_time)); > >
joda_time : 2020-11-27T05:35:00.000+05:30 date time format out: 11/27/2020
Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.
Related Article — Java DateTime
Copyright © 2023. All right reserved