Creating a Java calendar, basic options

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

What are the main attributes that should be assigned to a web service, so the client can create a Java calendar from these attributes?

I guess: TimeZone, Date and Time?

Also, how can a customer recreate a calendar based on these attributes?

Thanks!

+4
source share
4 answers

In fact, I would go for Timezone tz (the time zone in which the calendar was expressed), Locale loc (used for data presentation purposes) and long time (UTC time) if you want the exact same object.

In most cases, the time is sufficient, but the recipient expresses it with his time zone and locale.

+3
source

I assume that the Calendar instance you want to send is of type java.util.GregorianCalendar . In this case, you can simply use xsd:dateTime . For SOAP, Java typically associates this with an instance of javax.xml.datatype.XMLGregorianCalendar .

Translation between GregorianCalendarand XMLGregorianCalendar:

  • GregorianCalendar → XMLGregorianCalendar: javax.xml.datatype.DatatypeFactory.newXMLGregorianCalendar(GregorianCalendar)
  • XMLGregorianCalendar → GregorianCalendar: XMLGregorianCalendar.toGregorianCalendar()
+1
source

The easiest way is to use a long value.

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

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

0
source

TL; DR

Use plain text in UTC in the 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 text formats for representing date and time values.

2018-01-14T03: 57: 05.744850Z

The java.time classes use these standard default formats when parsing / generating strings. The ZonedDateTime class intelligently extends the standard to add the time zone name 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 problematic old time classes associated with the earliest versions of Java. These inherited classes are terrifying and should be avoided.

Now superseded by industry-leading java.time classes.

UTC

As a general rule, it is best to report the time using UTC rather than a specific time zone.

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

Instant

Instant class represents a moment on the UTC timeline with a nanosecond resolution (up to nine (9) decimal digits).

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

2018-01-14T03: 57: 05.744850Z

If a specific time zone is critical, use ZonedDateTime as shown above.

Syntactic

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 supersede the nasty old legacy time classes such as java.util.Date , Calendar and SimpleDateFormat .

The Joda-Time project, now in maintenance mode , we recommend switching to the java.time classes.

To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .

Where to get java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval , YearWeek , YearQuarter and more .

0
source

Source: https://habr.com/ru/post/1332757/


All Articles