3

Consider the following date string

  • 2012-10-01 01:02:03.004+0500

This is recognized in Java using the following SimpleDateFormat pattern:

  • yyyy-MM-dd HH:mm:ss.SSSZ

If, however, the timezone information above is truncated to 2 digits, i.e. like

  • 2012-10-01 01:02:03.004+05

the date string does not comply to any valid format, so there is no SimpleDateFormat pattern that could be used in order to correctly parse it.

Is there any workaround for parsing the truncated timezone correctly without string preprocessing?

If not, which regular expression would be optimal for that preprocessing to be done for a large number of such date strings in 1 round, e.g. using a replaceFirst() call, as in this similar question?

2
  • 1
    That's a SimpleDateFormatpattern, not a DateFormat pattern. Try subclassing SimpleDateFormat to append 00 when needed. Commented Oct 18, 2012 at 10:06
  • You are right in both suggestions. The subclassing approach seems the most plausible at this point, along the lines of the similar question I linked to above. Commented Oct 18, 2012 at 10:26

1 Answer 1

2

I do not know a good solution without string preprocessing, but if replaceFirst is acceptable, you can use this code snippet:

dateStr.replaceFirst("(?<=[+-]\\d\\d)$", "00")

This code appends two zeros to strings ending in <plus|minus><digit><digit> (link to ideone).

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

4 Comments

The timezone can start with a <minus> too.
I was thinking of something like that, but I am concerned about performance. You are right in that there seems to be no ideal solution. Your proposal does work. You may want to change \\d\\d into \\d{2}, for elegance. Thanks! :-)
@PNS On the point of \\d{2}, programmers often count "zero, one, two, many", and I'm one of them. Besides, there may only be so much "elegance" when we are talking regular expressions ;-)
I prefer elegance, but no worries. It will do just fine! :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.