0

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

winterisComing
  • 203
  • 2
  • 13
  • The outcommented `+ 'Z'` in your code seems to ndicate that you want a UTC offset in your string. Is this the case? The offset for Europe/Brussels? – Ole V.V. Oct 19 '19 at 16:20
  • Possible duplicate of [java.util.Date to XMLGregorianCalendar](https://stackoverflow.com/questions/835889/java-util-date-to-xmlgregoriancalendar) – Ole V.V. Oct 19 '19 at 16:24

3 Answers3

1

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);
      }
    }
nagendra
  • 11
  • 2
  • 1
    You should avoid `SimpleDateFormat`. That class is notoriously troublesome and long outdated. I think you are after `DateTimeFormatter.ISO_OFFSET_DATE_TIME` from java.time, the modern Java date and time API where also `ZoneId` and `ZonedDateTime` come from. – Ole V.V. Oct 19 '19 at 16:16
0

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);
Jannik
  • 1,583
  • 1
  • 14
  • 22
  • Hi @Jannik,thanks for your reply . I already did try the same but it really does not fetch me date in the desired format. actually it does nothing to its syntax. – winterisComing Oct 18 '19 at 08:45
  • It creates the same results as your `convertToSoapDateFormat` function and I thought that was the goal? – Jannik Oct 18 '19 at 08:48
0

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
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161