-2

Sorry to ask if this is a duplicate question as I try to find solutions online but couldn't get a confirmed answer.

I would like to know the possible values that can be used to parse using SimpleDateFormat method in Java. For examples, the minimum and maximum values that can be parsed without error.

My date format is in yyyy-MM-dd

DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

1900-01-01 seems to be a valid one, but not 9999-12-31. The maximum date "valid" for me is 4637-11-25, any possible reason for this?

7
  • Have you tired it ? I would expect it to work. See stackoverflow.com/questions/18534343/… for some discussion. Commented Jun 4, 2024 at 3:07
  • The maximum date "valid" for me is 4637-11-25 , any possible reason for this? Commented Jun 4, 2024 at 4:25
  • 1
    Could you provide the code example that date "9999-12-31" would be parsed as an error? Commented Jun 4, 2024 at 6:21
  • 3
    For your sanity use java.time, the modern Java date and time API, for your date work. The class you need is LocalDate. See the answer. DateFormat and SimpleDateFormat were notorious troublemakers and have fortunately been outdated for 10 years now. I recommend you forget about them. Commented Jun 4, 2024 at 9:16
  • 1
    What happens when you try to parse 9999-12-31 or any date later than 4637-11-25? Exception? If so, paste the stack trace into your question, formatted as code. Wrong result? If so, which result? Without such debugging details I can’t see we have a chance to guess what goes on. Commented Jun 4, 2024 at 9:30

1 Answer 1

2

Avoid legacy date time classes

You are using terribly flawed legacy date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

java.time.LocalDate

For a date-only value, use LocalDate class.

ISO 8601

Your desired format of YYYY-MM-DD complies with the ISO 8601 standard for textual date-time formats. The java.time classes use these formats by default when parsing/generating text. So no need for you to define a custom formatting pattern.

LocalDate ld = LocalDate.parse( "9999-12-31" ) ;

See this code run at Ideone.com.

From -999999999-01-01 to +999999999-12-31

The minimum & maximum values are defined as constants:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.