LocalTime with() Method in Java with Examples
In LocalTime class, there are two types of with() method depending upon the parameters passed to it.
with(TemporalAdjuster adjuster)
with(TemporalAdjuster adjuster) method of the LocalTime class used to adjusted this time using TemporalAdjuster and after adjustment returns the copy of adjusted time.The adjustment takes place using the specified adjuster strategy object. This instance of LocalTime is immutable and unaffected by this method call. A simple adjuster uses to set one of the fields, such as the hour field where A more complex adjuster might set the time to the last hour of the day.
Syntax:
public LocalTime with(TemporalAdjuster adjuster)
Parameters: This method accepts adjuster as parameter which is the adjuster to use.
Return value: This method returns a LocalTime based on this with the adjustment made.
Exception: This method throws following Exceptions:
- DateTimeException – if the adjustment cannot be made.
- ArithmeticException – if numeric overflow occurs.
Below programs illustrate the with() method:
Program 1:
// Java program to demonstrate // LocalTime.with() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create a LocalTime object LocalTime local = LocalTime.parse( "13:08:56"); // print instance System.out.println("LocalTime before" + " adjustment: " + local); // apply with method of LocalTime class LocalTime updatedlocal = local.with(LocalTime.MIDNIGHT); // print instance System.out.println("LocalTime after" + " adjustment: " + updatedlocal); } } |
LocalTime before adjustment: 13:08:56 LocalTime after adjustment: 00:00
with(TemporalField field, long newValue)
with(TemporalField field, long newValue) method of the LocalTime class used to set the specified field of LocalTime to a new value and returns the copy of new time.This method can be used to change any supported field, such as hour, minute or second. An exception is thrown If setting the new value is not possible due to the field is not supported or for some other reason. This instance of LocalTime is immutable and unaffected by this method call.
Syntax:
public LocalTime with(TemporalField field, long newValue)
Parameters: This method accepts field which is the field to set in the result and newValue which the new value of the field in the result as parameters.
Return value: This method returns a LocalTime based on this with the specified field set.
Exception: This method throws following Exceptions:
- DateTimeException – if the adjustment cannot be made.
- UnsupportedTemporalTypeException – if the field is not supported.
- ArithmeticException – if numeric overflow occurs.
Below programs illustrate the with() method:
Program 1:
// Java program to demonstrate // LocalTime.with() method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create a LocalTime object LocalTime local = LocalTime.parse( "13:12:23"); // print instance System.out.println("LocalTime before" + " applying method: " + local); // apply with method of LocalTime class LocalTime updatedlocal = local.with( ChronoField.SECOND_OF_MINUTE, 55); // print instance System.out.println("LocalTime after" + " applying method: " + updatedlocal); } } |
LocalTime before applying method: 13:12:23 LocalTime after applying method: 13:12:55
References:
https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#with(java.time.temporal.TemporalAdjuster)
https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#with(java.time.temporal.TemporalField, long)
Recommended Posts:
- LocalTime from() method in Java with Examples
- LocalTime get() method in Java with Examples
- LocalTime plus() method in Java with Examples
- LocalTime until() Method in Java with Examples
- LocalTime minusHours() method in Java with Examples
- LocalTime plusNanos() method in Java with Examples
- LocalTime plusHours() method in Java with Examples
- LocalTime minusSeconds() method in Java with Examples
- LocalTime query() Method in Java with Examples
- LocalTime minusNanos() method in Java with Examples
- LocalTime minusMinutes() method in Java with Examples
- LocalTime plusSeconds() method in Java with Examples
- LocalTime withMinute() method in Java with Examples
- LocalTime withSecond() method in Java with Examples
- LocalTime withNano() 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.

