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
?
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:");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With