OffsetDateTime format() method in Java with examples Last Updated : 13 Aug, 2021 Comments Improve Suggest changes Like Article Like Report The format() method of OffsetDateTime class in Java formats this date-time using the specified formatter. Syntax : public String format(DateTimeFormatter formatter) Parameter : This method accepts a single parameter formatter which specifies the formatter to use, not null.Return Value: It returns the formatted date string, not null.Exceptions: The function throws a DateTimeException that is when an error occurs during printing. Below programs illustrate the format() method:Program 1 : Java // Java program to demonstrate the format() method import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; public class GFG { public static void main(String[] args) { // Parses the date1 OffsetDateTime date1 = OffsetDateTime.parse("2018-12-12T13:30:30+05:00"); // Prints the date System.out.println("Date1: " + date1); DateTimeFormatter formatter = DateTimeFormatter.ISO_TIME; System.out.println(formatter.format(date1)); } } Output: Date1: 2018-12-12T13:30:30+05:00 13:30:30+05:00 Program 2 : Java // Java program to demonstrate the format() method // Exceptions import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; public class GFG { public static void main(String[] args) { try { // Parses the date1 OffsetDateTime date1 = OffsetDateTime.parse("2018-13-12T13:30:30+05:00"); // Prints the date System.out.println("Date1: " + date1); DateTimeFormatter formatter = DateTimeFormatter.ISO_TIME; System.out.println(formatter.format(date1)); } catch (Exception e) { System.out.println(e); } } } Output: java.time.format.DateTimeParseException: Text '2018-13-12T13:30:30+05:00' could not be parsed: Invalid value for MonthOfYear (valid values 1 - 12): 13 Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html#format-java.time.format.DateTimeFormatter- Comment More infoAdvertise with us Next Article OffsetDateTime format() method in Java with examples gopaldave Follow Improve Article Tags : Java Java-Functions Java-time package Java-OffsetDateTime Practice Tags : Java Similar Reads OffsetTime format() method in Java with examples The format() method of OffsetTime class in Java formats this time using the specified formatter which is passed in the parameter of the function. Syntax: public String format(DateTimeFormatter formatter) Parameter: This method accepts a single mandatory parameter formatter which specifies the format 2 min read OffsetDateTime from() method in Java with examples The from() method of OffsetDateTime class in Java obtains an instance of OffsetDateTime from a temporal object.Syntax : public static OffsetDateTime from(TemporalAccessor temporal) Parameter : This method accepts a single parameter temporal which specifies the temporal object to convert, not null.Re 1 min read OffsetDateTime get() method in Java with examples The get() method of OffsetDateTime class in Java gets the value of the specified field from this date as an int. Syntax : public int get(TemporalField field) Parameter : This method accepts a single parameter field which specifies the field to get, not null.Return Value: It returns the value for the 2 min read OffsetDateTime getHour() method in Java with examples The getHour() method of OffsetDateTime class in Java gets the hour-of-day field. Syntax : public int getHour() Parameter : This method accepts does not accepts any parameter. Return Value: It returns the hour-of-day which ranges from 0 to 23. Below programs illustrate the getHour() method: Program 1 1 min read OffsetTime from() method in Java with examples The from() method of OffsetTime class in Java obtains an instance of OffsetTime from a temporal object which is passed in the parameter of the method. Syntax: public static OffsetTime from(TemporalAccessor temporal) Parameter: This method accepts a single mandatory parameter temporal which specifies 1 min read OffsetDateTime of() method in Java with Examples The of(int year, int month, int day, int hour, int minute, int second, int nanosecond, ZoneOffset offset) method of the OffsetDateTime class in Java is used to create an instance of OffsetDateTime from the passed values of year, month, day, hour, minute, second, nanosecond and offset. In this method 3 min read OffsetDateTime getMonth() method in Java with examples The getMonth() method of OffsetDateTime class in Java gets the month-of-year field using the Month enum. Syntax : public Month getMonth() Parameter : This method accepts does not accepts any parameter. Return Value: It returns the month-of-year and not null.. Below programs illustrate the getMonth() 1 min read OffsetDateTime getMinute() method in Java with examples The getMinute() method of OffsetDateTime class in Java gets the minute-of-hour field. Syntax : public int getMinute() Parameter : This method accepts does not accepts any parameter. Return Value: It returns the minute-of-hour which can range from 0 to 59. Below programs illustrate the getMinute() me 1 min read OffsetDateTime getNano() method in Java with examples The getNano() method of OffsetDateTime class in Java gets the nano-of-second field. Syntax : public int getNano() Parameter : This method does not accepts any parameter. Return Value: It returns the nano-of-second which ranges from 0 to 999, 999, 999. Below programs illustrate the getNano() method: 1 min read OffsetDateTime getLong() method in Java with examples The getLong() method of OffsetDateTime class in Java gets the value of the specified field from this date-time as an long. Syntax : public long getLong(TemporalField field) Parameter: This method accepts a single parameter field which specifies the field to get, not null. Return Value: It returns th 2 min read Like