PNS
PNS

Reputation: 19905

Java regular expression to detect timezone in SimpleDateFormat pattern

Assume an application that parses thousands of date strings per second using some already known SimpleDateFormat patterns. The application needs to decide dynamically whether each such date pattern has a timezone, i.e. whether the date pattern string contains, anywhere in it and unquoted, the letters Z or X, in either upper case or lower case (i.e., 4 symbols in total).

Examples of such date patterns are:

One way of doing this is by using the indexOf() method of the String class, but this means running the method 4 times on each date pattern (or 2, if the date pattern string is converted to upper or lower case beforehand), plus checking whether the timezone symbol is quoted (in which case the pattern does not actually have a timezone).

The other choice is using a java regular expression, like

String date = ... // Get a SimpleDateFormat pattern

Pattern timezone = Pattern.compile("[^ZzXx]+[^'\"][ZzXx]{1}.*");

if (timezone.matcher(date).matches())
{
    // The date pattern does have a timezone
}

Is there a faster version for the above regular expression, i.e., for

or any other faster way, in general, for checking whether a SimpleDateFormat pattern does contain a timezone symbol?

Upvotes: 1

Views: 3658

Answers (1)

Ωmega
Ωmega

Reputation: 43673

I suggest do it as simple as possible:

String string = "...";

Pattern p = Pattern.compile("\\b(?<!')([xXzZ])(?!')\\b");
Matcher m = p.matcher(string);
if (m.find()) {
  String timeZone = m.group(1);
}

Upvotes: 1

Related Questions