Java LocalDate Class | with() Method with Example

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

LocalDate Class with() method

Syntax:

public LocalDate with(TemporalAdjusters t_adjuster);
public LocalDate with(TemporalField t_field, long new_val);
  • with() method is available in java.time package.
  • with(TemporalAdjusters t_adjuster) method is used to represent this date with the given adjustment.
  • with(TemporalField t_field, long new_val) method is used to set the new value for the given field.
  • These methods may throw an exception at the time of representing LocalDate with the given adjustment.
    • DateTimeException: This exception may throw when this LocalDate 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 cases, with(TemporalAdjusters t_adjuster),
    • TemporalAdjusters t_adjuster – represents the adjuster to adjust with this LocalDate.
  • In the second cases, 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 LocalDate,

  • In the first case, it returns adjusted instance of this date.
  • In the second case, it returns an instance of this date with the given field value set.

Example:

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

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

public class WithOfLocalDate {
    public static void main(String args[]) {
        long month_of_year = 5;

        // Instantiates two LocalDate
        LocalDate l_da1 = LocalDate.parse("2007-04-04");
        LocalDate l_da2 = LocalDate.of(2008, Month.FEBRUARY, 06);

        // Display l_da1,l_da2 
        System.out.println("LocalDate l_da1,l_da2 : ");
        System.out.println("l_da1: " + l_da1);
        System.out.println("l_da2: " + l_da2);
        System.out.println("month_of_year to update: " + month_of_year);

        System.out.println();

        // Here, with(TemporalField,long)
        // updates the field month-of-year
        // with the given value in this LocalDate
        // l_da1
        LocalDate l_date = l_da1.with(ChronoField.MONTH_OF_YEAR, month_of_year);

        // Display l_date
        System.out.print("l_da1.with(ChronoField.MONTH_OF_YEAR,month_of_year): ");
        System.out.println(l_date);

        // Here, with(TemporalAdjusters)
        // returns an adjusted instance
        // of this date l_da2
        l_date = l_da2.with(Month.JUNE).with(TemporalAdjusters.firstDayOfMonth());

        // Display l_date
        System.out.print("l_da2.with(Month.JUNE).with(TemporalAdjusters.firstDayOfMonth()): ");
        System.out.println(l_date);
    }
}

Output

LocalDate l_da1,l_da2 : 
l_da1: 2007-04-04
l_da2: 2008-02-06
month_of_year to update: 5

l_da1.with(ChronoField.MONTH_OF_YEAR,month_of_year): 2007-05-04
l_da2.with(Month.JUNE).with(TemporalAdjusters.firstDayOfMonth()): 2008-06-01


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.