Safi
Safi

Reputation: 141

Re-formatting date/time while converting values

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

Upvotes: 0

Views: 85

Answers (2)

Flo
Flo

Reputation: 411

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

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136012

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

String s = "2012-10-31T00:00:00+01:00".replaceAll("\\+.*", "+00:00")

Upvotes: 0

Related Questions