Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract Time Zone from String

I am aware that there are several questions out there regarding this. But I have a problem with a specific date pattern. Here's an example:

20130401100000[-03:EST]

I'm having troble with the -03 because according to the SimpleDateFormat documentation it should have 4 digits. The closest pattern I've got to it was this:

yyyyMMddHHmmss'['Z':'z']'

But because of the 2 digits it just won't work. Here's a code sample I'm using for tests:

public static void main(String[] args) throws ParseException {
    String input = "20130401100000[-03:EST]";

    DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss'['Z':'z']'");
    df.setTimeZone(TimeZone.getTimeZone("GMT"));    

    System.out.println(df.format(new Date()));
    System.out.println(df.parse(input));
}

When I print the new Date() I get this:

20130528155734[+0000:GMT]

But when I try parsing the input I get the following exception:

Exception in thread "main" java.text.ParseException: Unparseable date: "20130401100000[-03:EST]"
at java.text.DateFormat.parse(DateFormat.java:337)
at Teste.main(Teste.java:17)

Does anyone know how I can parse the input into a Date with the correct TimeZone?

like image 632
Rodrigo Sasaki Avatar asked May 28 '13 16:05

Rodrigo Sasaki


1 Answers

You can correct the formatting with:

public static void main(String[] args) throws ParseException {
    String input = "20130401100000[-03:EST]";

    DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss'['Z':'z']'");
    df.setTimeZone(TimeZone.getTimeZone("GMT"));    

    System.out.println(df.format(new Date()));
    input = input.replace(":","00:");
    System.out.println(df.parse(input));
}

If you'll have correctly formatted dates mixed in with incorrectly formatted ones, you could use:

    input = input.replaceAll("(\\[[+-]\\d{2}):","$100:");
like image 122
maybeWeCouldStealAVan Avatar answered Nov 07 '22 12:11

maybeWeCouldStealAVan