The adjustInto() method of OffsetDateTime class in Java adjusts the specified temporal object to have the same date and time as this object.
Syntax :
public Temporal adjustInto(Temporal temporal)
Parameter : This method accepts a single parameter temporal which specifies the target object to be adjusted, not null.
Return Value: It returns the adjusted object, not null
Errors and exception : This method throws two exceptions as described below:
- DateTimeException: if unable to make the adjustment.
- ArithmeticException: if numeric overflow occurs.
Below programs illustrate the adjustInto() method:
Program 1 :
// Java program to demonstrate the adjustInto() method import java.time.OffsetDateTime; import java.time.ZonedDateTime; public class GFG { public static void main(String[] args) { // Current time ZonedDateTime date = ZonedDateTime.now(); // Prints the current date System.out.println("Current date is: " + date); // Parses the date OffsetDateTime date1 = OffsetDateTime.parse("2018-12-12T13:30:30+05:00"); // Function used date = (ZonedDateTime)date1.adjustInto(date); // Prints the date System.out.println(date); } } |
Current date is: 2018-12-11T09:53:15.294Z[Etc/UTC] 2018-12-12T13:30:30Z[Etc/UTC]
Program 2 :
// Java program to demonstrate the adjustInto() method // exceptions import java.time.OffsetDateTime; import java.time.ZonedDateTime; public class GFG { public static void main(String[] args) { try { // Current time ZonedDateTime date = ZonedDateTime.now(); // Prints the current date System.out.println("Current date is: " + date); // Parses the date OffsetDateTime date1 = OffsetDateTime.parse("2018-13-12T13:30:30+05:00"); // Function used date = (ZonedDateTime)date1.adjustInto(date); // Prints the date System.out.println(date); } catch (Exception e) { System.out.println(e); } } } |
Current date is: 2018-12-11T09:53:24.660Z[Etc/UTC] 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/temporal/TemporalAdjuster.html
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Year adjustInto() Method in Java with Examples
- LocalTime adjustInto() method in Java with Examples
- ZoneOffset adjustInto(Temporal) method in Java with Examples
- OffsetTime adjustInto() method in Java with examples
- MonthDay adjustInto() method in Java with Examples
- DayOfWeek adjustInto() method in Java with Examples
- ChronoLocalDate adjustInto() method in Java with Examples
- ChronoLocalDateTime adjustInto() method in Java with Examples
- Instant adjustInto() method in Java with Example
- LocalDate adjustInto() method in Java
- Month adjustInto() method in Java
- OffsetDateTime getOffset() method in Java with examples
- OffsetDateTime hashCode() method in Java with examples
- OffsetDateTime getYear() method in Java with examples
- OffsetDateTime getSecond() method in Java with examples
- OffsetDateTime getNano() method in Java with examples
- OffsetDateTime getMonthValue() method in Java with examples
- OffsetDateTime compareTo() method in Java with examples
- OffsetDateTime equals() method in Java with examples
- OffsetDateTime format() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

