7

I wish to construct a date format that will optionally have a time argument.

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd [hh:mm]");

Is it also possible to construct a date format object that is capable of parsing different formats? Such as try the current locale but then fall back to ISO-8601 or should I just write multiple date formats if one fails?

UPDATE: Looking back at this question I can see I didn't specify that the reason for multiple date formats was for parsing strings, not for formatting a date, thus ambiguity for formatting date objects wasn't a concern for me. If you take this into account the time portion is or is not included in the parsing string.

3
  • 1
    I don't know if there's a canonical answer, but you could always use multiple DateFormat objects, even perhaps a list of them, and use one that doesn't throw a ParseException. Commented Dec 23, 2010 at 1:02
  • 1
    Thanks, I've started doing that using 4 different formats, two for default instance with/without time and again two from the ISO formats with/without time. I'm thinking if I should test length of string to give precedence to date+time formats. Commented Dec 23, 2010 at 1:10
  • 1
    You cannot format a date optionally with time because all dates have time. How would it optionally display? You can parse a date with many patterns however which you could use apache common lang's DateUtils and pass it parse patterns. Commented Dec 23, 2010 at 1:28

1 Answer 1

5

SimpleDateFormat won't let you do that. It doesn't support alternatives within a (single) format.

Even if it did, there would a problem. Consider using this

new SimpleDateFormat("yyyy-MM-dd [hh:mm]");

versus using

new SimpleDateFormat("yyyy-MM-dd hh:mm");
new SimpleDateFormat("yyyy-MM-dd ");

In the first case, when I parsed a date against the format, I couldn't tell the difference between "2010-01-01" and "2010-01-01 00:00" by looking at the Date delivered to me. In the 2nd case, I can.

In the first case, when I format a Date with zero in the minutes and seconds fields, it is not clear whether the result should end with "00:00" ... or not. In the second case, this is entirely in the hands of the application.

I guess that what I'm really doing here is raising the issue that dates and date/times mean different things to different people and in different contexts. Sometimes they mean instants and sometimes intervals. Sometimes a lack of expressed precision means imprecision, and sometimes that the precision is implied.

As developers we have to run the line between writing software that is annoyingly picky, and software that makes incorrect assumptions about what the user actually means by a date / time value. The first step in getting it right for the user is understanding the complexity of the problem. Overloading variations into a single format string is (would be) sweeping the problem under the carpet.

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

2 Comments

Thanks Stephen, I agree with your reasoning, however if a format is documented for the end user then there should be no reason why they can't follow that format. The problem arises when one is writing an extension to software that a situation may only accept a single argument whereby that argument must then be implicitly determined at runtime.
@Brett Ryan - "... if a format is documented for the end user then there should be no reason why they can't follow that format". Your users pay attention to documentation? Lucky you :-)

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.