简体   繁体   中英

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, ie whether the date pattern string contains, anywhere in it and unquoted, the letters Z or X , in either upper case or lower case (ie, 4 symbols in total).

Examples of such date patterns are:

  • yyyy/MM/dd HH:mm:ss.SSS Z // Timezone (not quoted Z symbol)
  • yyyy/MM/dd HH:mm:ss.SSS'Z' // No timezone (quoted Z symbol)
  • EEE MMM dd HH:mm:ss x yyyy // Timezone (not quoted x symbol)
  • EEE MMM dd HH:mm:ss'x'yyyy // No timezone (quoted x symbol)

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, ie, for

  • "[^ZzXx]+[^'\\"][ZzXx]{1}.*"

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM