ADVERTISEMENT
ADVERTISEMENT

Java Instant Class | with() Method with Example

Instant Class with() method: Here, we are going to learn about the with() method of Instant Class with its syntax and example.
Submitted by Preeti Jain, on May 28, 2020

Instant Class with() method

Syntax:

    public Instant with(TemporalAdjuster t_adjuster);
    public Instant with(TemporalField t_field, long new_val);
  • with() method is available in java.time package.
  • with(TemporalAdjuster t_adjuster) method is used to represent this Instant with the given adjuster.
  • with(TemporalField t_field, long new_val) method is used to set the given value for the given field.
  • These methods may throw an exception at the time of representing Instant with the given adjustment.
    • DateTimeException: This exception may throw when this Instant couldn't adjust with the given adjuster.
    • ArithmeticException: This exception may throw when the calculated result exceeds the limit to represent this object.
  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, "with(TemporalAdjuster t_adjuster)",
    • TemporalAdjuster t_adjuster – represents the field in which to set the value the given value.
  • In the second case, "with(TemporalField t_field, long new_val)",
    • TemporalField t_field – represents the field in which to set the value the given value.
    • long new_val – represents new value to set for the given field.

Return value:

In both the cases, the return type of the method is Instant, it returns the Instant that holds the value of this Instant with the given object.

Example:

// Java program to demonstrate the example 
// of with() method of Instant

import java.time.*;
import java.time.temporal.*;

public class WithOfInstant {
    public static void main(String args[]) {
        long field_val = 10;

        // Instantiates two Instant 
        Instant ins1 = Instant.parse("2006-04-03T05:10:15.00Z");
        Instant ins2 = Instant.now();

        // Display ins1,ins2
        System.out.println("Instant ins1 and ins2: ");
        System.out.println("ins1: " + ins1);
        System.out.println("ins2: " + ins2);
        System.out.println("field_val to set: " + field_val);

        System.out.println();

        // Here, this method adjusts this Instant 
        // with the given adjuster
        Instant with_val = ins1.with(ins2);

        // Display with_val
        System.out.println("ins1.with(ins2): " + with_val);

        // Here, this method sets the new value 
        // for the given field in this Instant
        with_val = ins1.with(ChronoField.MICRO_OF_SECOND, field_val);

        // Display with_val
        System.out.println("ins1.with(ChronoField.MICRO_OF_SECOND,field_val): " + with_val);
    }
}

Output

Instant ins1 and ins2: 
ins1: 2006-04-03T05:10:15Z
ins2: 2020-05-28T07:42:39.007362Z
field_val to set: 10

ins1.with(ins2): 2020-05-28T07:42:39.007362Z
ins1.with(ChronoField.MICRO_OF_SECOND,field_val): 2006-04-03T05:10:15.000010Z
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.