OffsetTime with() Method in Java with Examples
Last Updated :
15 Jan, 2019
In OffsetTime class, there are two types of with() method depending upon the parameters passed to it.
with(TemporalAdjuster adjuster)
with(TemporalAdjuster adjuster) method of the
OffsetTime class used to adjusted this OffsetTime using TemporalAdjuster and after adjustment returns the copy of adjusted OffsetTime.The adjustment takes place using the specified adjuster strategy object. This instance of OffsetTime is immutable and unaffected by this method call.
Syntax:
public OffsetTime with(TemporalAdjuster adjuster)
Parameters: This method accepts
adjuster as parameter which is the adjuster to use.
Return value: This method returns a OffsetTime 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
// Java program to demonstrate
// OffsetTime.with() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create a OffsetTime object
OffsetTime off
= OffsetTime.parse("19:19:50+06:00");
// print instance
System.out.println("OffsetTime before"
+ " adjustment: "
+ off);
// apply with method of OffsetTime class
OffsetTime updatedlocal = off.with(LocalTime.NOON);
// print instance
System.out.println("OffsetTime after"
+ " adjustment: "
+ updatedlocal);
}
}
Output:
OffsetTime before adjustment: 19:19:50+06:00
OffsetTime after adjustment: 12:00+06:00
with(TemporalField field, long newValue)
with(TemporalField field, long newValue) method of the
OffsetTime class used to set the specified field of OffsetTime to a new value and returns the copy of new time.This method can be used to change any supported field, such as the 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 OffsetTime is immutable and unaffected by this method call.
Syntax:
public OffsetTime 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 OffsetTime 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
// Java program to demonstrate
// OffsetTime.with() method
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
// create a OffsetTime object
OffsetTime off
= OffsetTime.parse("19:19:50+06:00");
// print instance
System.out.println("OffsetTime before"
+ " applying method: "
+ off);
// apply with method of OffsetTime class
OffsetTime updatedlocal
= off.with(
ChronoField.HOUR_OF_DAY, 23);
// print instance
System.out.println("OffsetTime after"
+ " applying method: "
+ updatedlocal);
}
}
Output:
OffsetTime before applying method: 19:19:50+06:00
OffsetTime after applying method: 23:19:50+06:00
References:
https://docs.oracle.com/javase/10/docs/api/java/time/OffsetTime.html#with(java.time.temporal.TemporalAdjuster)
https://docs.oracle.com/javase/10/docs/api/java/time/OffsetTime.html#with(java.time.temporal.TemporalField, long)
Similar Reads
OffsetDateTime with() Method in Java with Examples In OffsetDateTime class, there are two types of with() method depending upon the parameters passed to it. with(TemporalAdjuster adjuster) with(TemporalAdjuster adjuster) method of the OffsetDateTime class used to adjusted this OffsetDateTime using TemporalAdjuster and after adjustment returns the co
3 min read
OffsetDateTime withYear() method in Java with examples The withYear() method of OffsetDateTime class in Java returns a copy of this OffsetDateTime with the year altered as specified in the parameter. Syntax: public OffsetDateTime withYear(int year) Parameter: This method accepts a single parameter year which specifies the year to be set in the result wh
2 min read
OffsetDateTime withNano() method in Java with examples The withNano() method of OffsetDateTime class in Java returns a copy of this OffsetDateTime with the nano-of-second altered as specified in the parameter. Syntax: public OffsetDateTime withNano(int nanoOfSecond) Parameter: This method accepts a single parameter nanaOfSecond which specifies the nano-
2 min read
OffsetDateTime withHour() method in Java with examples The withHour() method of OffsetDateTime class in Java returns a copy of this OffsetDateTime with the hour of the day altered as specified in the parameter. Syntax: public OffsetDateTime withHour(int hour) Parameter: This method accepts a single parameter hour which specifies the hour of the day to b
2 min read
OffsetTime until() Method in Java with Examples until() method of the OffsetTime class used to calculate the amount of time between two OffsetTime objects using TemporalUnit. The start and end points are this and the specified OffsetTime passed as a parameter. The result will be negative if the end is before the start. The calculation returns a w
2 min read
OffsetTime of() method in Java with Examples The of(int hour, int minute, int second, int nanosecond, ZoneOffset offset) method of the OffsetTime class in Java is used to create an instance of OffsetTime from the passed values of hour, minute, second and nanosecond. In this method, we pass the value of hour, minute, second and nanosecond in in
2 min read
OffsetDateTime withMonth() method in Java with examples The withMonth() method of OffsetDateTime class in Java returns a copy of this OffsetDateTime with the month-of-year altered as specified in the parameter. Syntax: public OffsetDateTime withMonth(int month) Parameter: This method accepts a single parameter month which specifies the month-of-year to b
2 min read
OffsetTime plus() method in Java with examples In OffsetTime class, there are two types of plus() method depending upon the parameters passed to it. plus(long amountToAdd, TemporalUnit unit) plus() method of a OffsetTime class used to return a copy of this OffsetTime with the specified amount of unit added.If it is not possible to add the amount
2 min read
OffsetTime query() method in Java with examples The Query() method of OffsetTime class in Java queries this time using the specified query. Syntax : public R query(TemporalQuery query) Parameter: This method accepts a single parameter query that specifies the query to be invoked and not null. Return Value: It returns the query result, null may be
1 min read
OffsetDateTime withSecond() method in Java with examples The withSecond() method of OffsetDateTime class in Java returns a copy of this OffsetDateTime with the second-of-minute altered as specified in the parameter. Syntax: public OffsetDateTime withSecond(int second) Parameter: This method accepts a single parameter second which specifies the second-of-m
2 min read