4

I would like to send a Java Calendar object over a web service (soap). I notice that this kind of object is too complex and there should be a better method to send the same information.

What are the basic attributes that should be sended over the web service, so the client can create a Java Calendar out of this attributes?

I'm guessing: TimeZone, Date, and Time?

Also, how can the client recreate the Calendar based on those attributes?

Thanks!

Hectoret
  • 3,553
  • 13
  • 49
  • 56

4 Answers4

3

In fact I would go for Timezone tz (timezone the calendar was expressed in), Locale loc (used for data representation purpose) and long time (UTC time) if you want exactly the same object.

In most uses the time is enough though, the receiver will express it with his own timezone and locale.

Guillaume
  • 5,535
  • 1
  • 24
  • 30
1

I suppose the Calendar instance that you would like to send is of type java.util.GregorianCalendar. In that case, you could just use xsd:dateTime. For SOAP, Java will usually bind that to a javax.xml.datatype.XMLGregorianCalendar instance.

Translating between GregorianCalendarand XMLGregorianCalendar:

  • GregorianCalendar -> XMLGregorianCalendar: javax.xml.datatype.DatatypeFactory.newXMLGregorianCalendar(GregorianCalendar)
  • XMLGregorianCalendar -> GregorianCalendar: XMLGregorianCalendar.toGregorianCalendar()
nd.
  • 8,699
  • 2
  • 32
  • 42
  • FYI, the troublesome old date-time classes such as `java.util.GregorianCalendar`, [`java.util.Calendar`](https://docs.oracle.com/javase/9/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) classes built into Java 8 & Java 9. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jan 14 '18 at 03:51
0

The easiest way is to use a long value.

java.util.Calendar.getInstance().getTimeInMillis()

This returns the long value for the date. That value can be used to construct java.util.Date or a Calendar.

sproketboy
  • 8,967
  • 18
  • 65
  • 95
  • Using a count-from-epoch is clumsy, risky, and ambiguous. What resolution - whole seconds, millis, microseconds, nanoseconds? Which of [the eighteen epoch reference dates](https://en.wikipedia.org/wiki/Epoch_(reference_date)#Notable_epoch_dates_in_computing) used by notable systems? Using java.time classes and standard ISO 8601 formats is simpler, easier to debug, and less error-prone. – Basil Bourque Jan 14 '18 at 04:17
0

tl;dr

Use plain text, in UTC, in standard ISO 8601 format.

Instant.now().toString()

2018-01-23T01:23:45.123456Z

Instant.parse( "2018-01-23T01:23:45.123456Z" ) 

ISO 8601

The ISO 8601 standard is a well-designed practical set of textual formats for representing date-time values.

2018-01-14T03:57:05.744850Z

The java.time classes use these standard formats by default when parsing/generating strings. The ZonedDateTime class wisely extends the standard to append the name of a time zone in square brackets.

ZoneId z = ZoneId.of( "America/Los_Angeles" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
String output = zdt.toString() ;

2018-01-13T19:56:26.318984-08:00[America/Los_Angeles]

java.time

The java.util.Calendar class is part of the troublesome old date-time classes bundled with the earliest versions of Java. These legacy classes are an awful mess, and should be avoided.

Now supplanted by the modern industry-leading java.time classes.

UTC

Generally best to communicate a moment using UTC rather than a particular time zone.

The standard format for a UTC moment is YYYY-MM-DDTHH:MM:SS.SSSSSSSSSZ where the T separates the year-month-day from the hour-minute-second. The Z on the end is short for Zulu and means UTC.

Instant

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.now() ;
String output = instant.toString() ;

2018-01-14T03:57:05.744850Z

If a particular time zone is crucial, use a ZonedDateTime as shown above.

Parsing

These strings in standard format can be parsed to instantiate java.time objects.

Instant instant = Instant.parse( "2018-01-14T03:57:05.744850Z" ) ;
ZonedDateTime zdt = ZonedDateTime.parse( "2018-01-13T19:56:26.318984-08:00[America/Los_Angeles]" ) ; 

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154