[Solved] java.time.DateTimeException: Invalid date ‘February 29’ as ‘2014’ is not a leap year


java.time.DateTimeException occurs while calculating Date and Time in java.time classes. This exception is used to indicate problems with creating, querying and manipulating date-time objects.

Constructors

  • DateTimeException(String message) : Constructs a new date-time exception with the specified message.
  • DateTimeException(String message, Throwable cause) : Constructs a new date-time exception with the specified message and cause.

Sample Code

 try {
// Here with throw DateTimeException if pass as Invalidate Date
// format
LocalDate lastDay_2014 = LocalDate.of(2014, Month.FEBRUARY, 29);
System.out.println("Specific Date=" + lastDay_2014);<span id="mce_SELREST_start" style="overflow:hidden;line-height:0;"></span>
} catch (DateTimeException ex) {
ex.printStackTrace();
}
 

Output Message


java.time.DateTimeException: Invalid date 'February 29' as '2014' is not a leap year
    at java.time.LocalDate.create(Unknown Source)
    at java.time.LocalDate.of(Unknown Source)
    at datetime.Java8DateTime.localDateAPI(Java8DateTime.java:157)
    at datetime.Java8DateTime.main(Java8DateTime.java:32)

Issue

Here with throw DateTimeException if pass as Invalidate Date. For above example passing date as 29 as we know February month only have 29 days in month of leap year. Here year as we passing is 2014 which is not leap year.

Solutions

To solve this issue pass max date for february month after validating leap year from below API.


LocalDate today = LocalDate.now();
//LocalDate.isLeapYear()this api will return true or false after checking for leap year
today.isLeapYear();

boolean LocalDate.isLeapYear() : Checks if the year is a leap year, according to the ISO proleptic calendar system rules.

More Issues Solution

For more other JAVA/JDBC issues solution follow link JAVA/JDBC Issues.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s