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?

Community
  • 1
  • 1
PNS
  • 19,295
  • 32
  • 96
  • 143
  • 1
    That's a `SimpleDateFormat`pattern, not a `DateFormat` pattern. Try subclassing `SimpleDateFormat` to append `00` when needed. – Christoffer Hammarström Oct 18 '12 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. – PNS Oct 18 '12 at 10:26

1 Answers1

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).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    The timezone can start with a `` too. – Christoffer Hammarström Oct 18 '12 at 10:04
  • 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 Oct 18 '12 at 10:17
  • 1
    @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 ;-) – Sergey Kalinichenko Oct 18 '12 at 10:17
  • I prefer elegance, but no worries. It will do just fine! :-) – PNS Oct 18 '12 at 10:18