Minimum or maximum date-time although does not much require in software development but in some scenarios, it is really beneficial.
To find the minimum or maximum date-time, you can use constants of the LocalDateTime class of Java 8.
The Java 8 date-time API works in the min and max range, and it throws DateTImeException if you ever go out of this date-time range.
Let's understand with the running examples.
To find the max date-time in Java, you can use the MAX constant of LocalDateTime class. It will return the date in the positive range. See the below java code.
/*
* Code example to get min and max range of local date time 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
LocalDateTime newDate = date.MAX; // max localdatetime
// Display new date
System.out.println("New date : "+newDate);
}
}
Output:
Date : 2022-03-17T13:32:57.222715015
New date : +999999999-12-31T23:59:59.999999999
Let's see the signature of this constant field.
public static final LocalDateTime MAX
Package Name: java.time;
Class Name: LocalDateTime
Return Value: It returns the local date-time just before midnight at the end of the max date.
Version: Since 1.8
Let's see some more examples.
Similarly to find min local date time, just use the MIN constant. See the below signature:
public static final LocalDateTime MIN
It returns a local date-time of midnight at the start of the minimum date. The minimum supported LocalDateTime is '-999999999-01-01T00:00:00'. See the below java code example.
/*
* Code example to get min range of local date time 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
LocalDateTime newDate = date.MIN; // min localdatetime
// Display new date
System.out.println("New date : "+newDate);
}
}
Output:
Date : 2022-03-17T13:33:34.472379743
New date : -999999999-01-01T00:00
Use this code to get results in a single line of code. If you are a beginner, skip this code.
LocalDate.MAX; // to get max date-time
LocalDate.MIN; // to get min date-tim
This code will return min and max local date-time object. You just need to copy and paste this code to your java source file, and get the result instantly.