0

I'm receiving an invoice with a date (TaxPointDate) which is in the format of "2012-10-31T00:00:00+01:00"

However I need to replace the "+01:00" portion to "+00:00" as I have experienced difficulties where the value has changed from "2012-10-31T00:00:00+01:00" in the original to "2012-10-30T23:00:00.000Z" when I reformat which is not what I want.

How would I go about this?

Thanks

Safi
  • 131
  • 1
  • 2
  • 8
  • What is the object class? `java.util.Date`? – KidTempo Aug 23 '13 at 10:51
  • 1
    What do you mean with _replace the "+01:00" portion to "+00:00"_? The "+01:00" indicates the timezone offset. Both dates you have mentioned represent the same point in time. – Moritz Petersen Aug 23 '13 at 10:56
  • yes but I dont want it to. The value is supplied by another company and cannot be changed at their end but the fact that it is set to +01:00 is breaking the code when I try to run it in our processes. – Safi Aug 23 '13 at 11:00
  • It would make life a lot easier if I could just grab the value, change the +01:00 to +00:00 and then run our processes. – Safi Aug 23 '13 at 11:01
  • Have a look http://stackoverflow.com/questions/1516213/java-util-date-is-using-timezone – Flo Aug 23 '13 at 11:26

2 Answers2

1

Mayby this will work for you. But I think you wont use this in your app. The problem is, that I change the DefaultTimeZone, so the output of every Date will be as 'GMT'. I also add the offset between the to TimeZones so your output is the your original time with +0000 at the end.

But be aware that this will changes your Date. It adds the timeoffset to hours and your original date and your new date are not equal.

"2012-10-31T00:00:00+01:00" and "2012-10-30T23:00:00+00:00" are equal dates. The output varies just because of different TimeZones.

    SimpleDateFormat originalDateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    Date date = originalDateParser.parse("2012-10-31T00:00:00+0100");
    System.out.println("Date Input" + originalDateParser.format(date));

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    TimeZone timeZoneInputDate = cal.getTimeZone();

    TimeZone.setDefault(TimeZone.getTimeZone("GMT"));

    System.out.println("TimeZone InputDate: " + timeZoneInputDate.getDisplayName());
    cal.add(Calendar.MILLISECOND, (int) timeZoneInputDate.getOffset(date.getTime()));

    SimpleDateFormat newDateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    System.out.println(newDateParser.format(cal.getTime()));

    TimeZone.setDefault(null);
Flo
  • 411
  • 4
  • 11
0

This will replace the "+01:00" portion to "+00:00"

String s = "2012-10-31T00:00:00+01:00".replaceAll("\\+.*", "+00:00")
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275