winterisComing
winterisComing

Reputation: 233

How to convert java.util.Date to soap suppported date format "yyyy-MM-dd'T'HH:mm:ss" with zone id

I am dealing with a situation where I want to convert java.util date into soap supported format with specific zone (Europe/Brussels)

I tried using Java 8 zone id feature but it seems it works well with instant dates only.

    ZoneId zoneId = ZoneId.of("Europe/Brussels");
    ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.now(), 
    zoneId);
  GregorianCalendar calendar = GregorianCalendar.from(zonedDateTime);
    String xmlNow = convertToSoapDateFormat(calendar);
    calendar.add(Calendar.MINUTE, 61);
    String xmlLater = convertToSoapDateFormat(calendar);
    //Method for soap conversion

    private String convertToSoapDateFormat(GregorianCalendar cal) throws DatatypeConfigurationException {
    XMLGregorianCalendar gDateFormatted2 = DatatypeFactory.newInstance().newXMLGregorianCalendar(
            cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH),
            cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND),
            DatatypeConstants.FIELD_UNDEFINED, DatatypeConstants.FIELD_UNDEFINED);
    return gDateFormatted2.toString();// + "Z";
}

I want lets say this date (2002-02-06) converted to this SOAP format 2002-02-06T08:00:00

Upvotes: 0

Views: 985

Answers (3)

Anonymous
Anonymous

Reputation: 86359

java.time

I tried using Java 8 zone id feature but it seems it works well with instant dates only.

Using java.time, the modern Java date and time API that came out with Java 8 and includes ZoneId, certainly is the recommended approach. And it works nicely. I cannot tell from your question what has hit you. If I understand correctly, DateTimeFormatter.ISO_OFFSET_DATE_TIME will give you the format you are after for your SOAP XML.

        ZoneId zoneId = ZoneId.of("Europe/Brussels");

        ZonedDateTime zdtNow = ZonedDateTime.now(zoneId);
        String xmlNow = zdtNow.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        ZonedDateTime zdtLater = zdtNow.plusMinutes(61);
        String xmlLater = zdtLater.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);

        System.out.println("Now:   " + xmlNow);
        System.out.println("Later: " + xmlLater);

When I ran this code just now, the output was:

Now:   2019-10-21T13:56:37.771+02:00
Later: 2019-10-21T14:57:37.771+02:00

Upvotes: 0

nagendra
nagendra

Reputation: 11

You can use the SimpleDateFormat class with setTimezone method

   public class Main {
      public static void main(String[] args) {
        SimpleDateFormat sdfAmerica = new SimpleDateFormat("dd-MM-yyyy'T'HH:mm:ss");
        sdfAmerica.setTimeZone(TimeZone.getTimeZone("America/Chicago"));
        String sDateInAmerica = sdfAmerica.format(new Date());
        System.out.println(sDateInAmerica);
      }
    }

Upvotes: 1

Jannik
Jannik

Reputation: 1623

I'm not sure if this is the answer you are looking for, so tell me if I misunderstood your question. Then I'll delete the answer.

You can use the SimpleDateFormat class to achieve your goal. Create the format with by

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

And then format the date with the following call:

df.format(date);

Upvotes: 0

Related Questions