To change or alter the day of year in date-time, you can use the withDayOfYear() method of LocalDateTime class.
This method was added to Java 8 date-time API. So, can be used in Java 8 and higher versions such as Java 11, Java 17, etc.
The withDayOfYear() method returns a copy of the LocalDateTime object after altering the day field(1 to 365-366).
For example, if you want to alter a day of the "2022-03-14T17:28:13.048999208" to "2022-01-10T17:28:13.048999208" then just pass the 10 to the withDayOfYear() method.
Since the year starts on 1 January that's why the result is 10 January of the same year.
Let's understand with the running examples.
/*
* Code example to get local date time with the specified day of year in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// String date is given
String strDate = "2022-03-14T17:28:13.048999208";
// parse the date into date time
LocalDateTime date = LocalDateTime.parse(strDate);
// Displaying date and time
System.out.println("Date : "+date);
// Get new datetime with altered day
LocalDateTime newDate = date.withDayOfYear(10);
// Display new date
System.out.println("New date : "+newDate);
}
}
Output:
Date : 2022-03-14T17:28:13.048999208
New date : 2022-01-10T17:28:13.048999208
now, let's have a look at its signature:
public LocalDateTime withDayOfYear(int dayOfYear)
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns a copy of LocalDateTime after altering day-of-year.
Parameters: It takes a single int type value.
Exceptions: It throws a DateTimeException if day-of-year value or range(1 to 365-366) is invalid.
Version: Since 1.8
Let's see some more examples.
Here, we are altering day of the current date-time. We used the now() method of LocalDateTime class to get the current date-time and then called the withDayOfYear() method.
We passed the 55 as a value and see it returns date-time of February.
/*
* Code example to get local date time with the specified day of year in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// Current date and time
LocalDateTime date = LocalDateTime.now();
// Displaying date and time
System.out.println("Date : "+date);
// Get new datetime with altered day
LocalDateTime newDate = date.withDayOfYear(55);
// Display new date
System.out.println("New date : "+newDate);
}
}
Output:
Date : 2022-03-17T12:10:46.069425754
New date : 2022-02-24T12:10:46.069425754
If we pass an invalid range of day-of-year then get the exception like below.
Here, we passed 555 which does not lie in the range(1 to 365-366) that's why the compiler throws an exception.
/*
* Code example to get local date time with the specified day of year in Java
*/
import java.time.LocalDateTime;
public class JExercise {
public static void main(String[] args) {
// Current date and time
LocalDateTime date = LocalDateTime.now();
// Displaying date and time
System.out.println("Date : "+date);
// Get new datetime with altered day
LocalDateTime newDate = date.withDayOfYear(555); // invalid range
// Display new date
System.out.println("New date : "+newDate);
}
}
Output:
Date : 2022-03-17T12:11:38.175302293
Exception in thread "main" java.time.DateTimeException: Invalid value for DayOfYear (valid values 1 - 365/366): 555