[Solved] java.time.DateTimeException: Invalid value for HourOfDay (valid values 0 – 23): 24


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 {
// Try creating time by providing invalid inputs
LocalTime invalidTime = LocalTime.of(24,18);
// Exception in thread "main" java.time.DateTimeException:
// Invalid value for HourOfDay (valid values 0 - 23): 24
} catch (DateTimeException ex) {
ex.printStackTrace();
}
 

Output Message

 


Exception in thread "main" java.time.DateTimeException: Invalid value for HourOfDay (valid values 0 - 23): 24
    at java.time.temporal.ValueRange.checkValidValue(Unknown Source)
    at java.time.temporal.ChronoField.checkValidValue(Unknown Source)
    at java.time.LocalTime.of(Unknown Source)
    at datetime.Java8DateTime.main(Java8DateTime.java:69)

Issue

Here with throw DateTimeException if pass as Invalidate Date and Time. For above example passing hour as 24 which causing this issue because allowed values for hours is between range 0-23.

Solutions

To solve this issue pass hour value in method as between range 0-23. Below are some methods for LocalTime.of() and corresponding values range for each parameters.

  • LocalTime localTime= LocalTime.of(int hour, int minute) : Obtains an instance of LocalTime from an hour and minute.
  • LocalTime localTime= LocalTime.of(int hour, int minute, int second): Obtains an instance of LocalTime from an hour, minute and second.
  • LocalTime localTime= LocalTime.of(int hour, int minute, int second, int nanoOfSecond): Obtains an instance of LocalTime from an hour, minute, second and nanosecond.

Date and Time Allowed range:

  • Month : 0-12
  • Day : 

1-31 : (January, March, May, July, August, October, December)

1-28/29 : (February) Leap year have 29 days in February and Non leap year have 28 days

1-30: (April, June, September, November)

  • Hour: 0-23
  • Minute: 0-59
  • Second : 0 – 59
  • NanoSecond: 0 – 999999999

Issues Solution

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

Advertisements

About Saurabh Gupta

My Name is Saurabh Gupta, I have approx. 11 Year of experience in Information Technology World manly in Java/J2EE. During this time I have worked with multiple organization with different client, so many technology, frameworks etc.
This entry was posted in Date, Java and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

w

Connecting to %s