ZonedDateTime parse() method in Java with Examples
Last Updated :
28 Dec, 2022
In ZonedDateTime class, there are two types of parse() method depending upon the parameters passed to it.
parse(CharSequence text)
parse() method of a ZonedDateTime class used to get an instance of ZonedDateTime from a string such as '2018-12-06T19:21:12.123+05:30[Asia/Calcutta]' passed as parameter. The string must have a valid date-time and is parsed using DateTimeFormatter.ISO_ZONED_DATE_TIME. Syntax:
public static ZonedDateTime parse(CharSequence text)
Parameters: This method accepts only one parameter text which is the text to parse in ZonedDateTime. It should not be null. Return value: This method returns ZonedDateTime which is the parsed local date-time. Exception: This method throws DateTimeParseException if the text cannot be parsed. Below programs illustrate the parse() method: Program 1:
Java
// Java program to demonstrate
// ZonedDateTime.parse() method
import java.time.*;
public class GFG {
public static void main(String[] args)
{
// create an ZonedDateTime object
ZonedDateTime zoneddatetime2
= ZonedDateTime
.parse(
"2018-12-26T20:28:33.213+05:30[Asia/Calcutta]");
// print result
System.out.println("ZonedDateTime : "
+ zoneddatetime2);
}
}
Output:ZonedDateTime : 2018-12-26T20:28:33.213+05:30[Asia/Calcutta]
parse(CharSequence text, DateTimeFormatter formatter)
parse() method of a ZonedDateTime class used to get an instance of ZonedDateTime from a string such as '2018-12-06T19:21:12.123+05:30[Asia/Calcutta]' passed as parameter using a specific formatter.The date-time is parsed using a specific formatter. Syntax:
public static ZonedDateTime parse(CharSequence text,
DateTimeFormatter formatter)
Parameters: This method accepts two parameters text which is the text to parse and formatter which is the formatter to use. Return value: This method returns ZonedDateTime which is the parsed local date-time. Exception: This method throws DateTimeParseException if the text cannot be parsed. Below programs illustrate the parse() method: Program 1:
Java
// Java program to demonstrate
// ZonedDateTime.parse() method
import java.time.*;
import java.time.format.*;
public class GFG {
public static void main(String[] args)
{
// create a formatter
DateTimeFormatter formatter
= DateTimeFormatter
.ISO_ZONED_DATE_TIME;
// create an ZonedDateTime object and
ZonedDateTime zdt
= ZonedDateTime
.parse("2018-12-16T20:28:33.213+05:30[Asia/Calcutta]",
formatter);
// print result
System.out.println("ZonedDateTime : "
+ zdt);
}
}
Output:ZonedDateTime : 2018-12-16T20:28:33.213+05:30[Asia/Calcutta]
References: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#parse(java.lang.CharSequence) https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#parse(java.lang.CharSequence, java.time.format.DateTimeFormatter)
Similar Reads
ZonedDateTime range() method in Java with Examples The range() method of a ZonedDateTime class is used to get the range of valid values for the field passes as a parameter. This method returns ValueRange object which contains the minimum and maximum valid values for a field. This ZonedDateTime is helpful to enhance the accuracy of the returned range
2 min read
ZonedDateTime of() Method in Java with Examples In ZonedDateTime class, there are three types of() method depending upon the parameters passed to it. of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond, ZoneId zone) of() method of a ZonedDateTime class used to get an instance of ZonedDateTime from a year, mo
3 min read
ZonedDateTime query() Method in Java with Examples query() method of an ZonedDateTime class used to query this ZonedDateTime using the specified query as parameter.The TemporalQuery object passed as parameter define the logic to be used to obtain the result from this ZonedDateTime. Syntax: public <R> R query(TemporalQuery<R> query) Param
2 min read
ZonedDateTime plusSeconds() method in Java with Examples plusSeconds() method of a ZonedDateTime class used to add the number of seconds in this ZonedDateTime and return a copy of ZonedDateTime after addition.This method works on the instant time-line and adding one-second returns the time-line of one second later. Note that this method is a different app
2 min read
ZonedDateTime plusMinutes() method in Java with Examples plusMinutes() method of a ZonedDateTime class used to add the number of minutes in this ZonedDateTime and return a copy of ZonedDateTime after addition.This method operates on the instant time-line, such that adding one minute will always be a duration of one minute later. This instance is immutable
2 min read