DragonDancing
DragonDancing

Reputation: 54

How to achive this date format in calendar

I have to create the XMLGregorianCalendar object with this date format "YYYY-MM-DDTHH:MI:SS±TZ" e.g. "2015-07-01T17:42:49+04"

I have no idea how to do this. I've used a number of ways on how to convert date, but this pattern doesn't seem to work.

After some experiments I found that "YYYY-MM-dd'T'HH:mm:ssX" will give me the desired output. But it's a string and I can't achieve the same format with XMLGregorianCalendar.

It gives me "2015-07-01T17:42:4234+05:00", as you see there're additional symbols that i don't need.

Upvotes: 0

Views: 122

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 338785

Date-time objects have no format

XMLGregorianCalendar object with this date format

Date-time objects such as XMLGregorianCalendar do not have a “format”. They internally represent the date-time value in some manner, though not likely to be in text.

You can instantiate date-time objects by parsing text. And your date-time objects can generate text representing their internal value. But the text and the date-time object are distinct and separate from one another.

java.time

The XMLGregorianCalendar class is now obsolete. Supplanted by the modern java.time classes defined in JSR 310.

Parse your input string as a OffsetDateTime as in includes an offset-from-UTC but not a time zone.

OffsetDateTime odt = OffsetDateTime.parse( "2015-07-01T17:42:49+04" );

Generate text in standard ISO 8601 format.

String output = odt.toString() ;  // Generates text in ISO 8601 format.

2015-07-01T17:42:49+04:00

Parts of an offset

The part at the end is the offset-from-UTC, a number of hours-minutes-seconds ahead or behind the prime meridian. In ISO 8601, the Plus sign is a positive number that means ahead of UTC. A Minus sign is a negative number that means behind UTC.

Suppressing parts of an offset

Some people may drop the seconds when zero, or drop the minutes when zero. But suppressing those digits does not change the meaning. These three strings all represent the very same moment:

  • 2015-07-01T17:42:49+04
  • 2015-07-01T17:42:49+04:00
  • 2015-07-01T17:42:49+04:00:00

You said:

"2015-07-01T17:42:4234+05:00", as you see there're additional symbols that i don't need.

[I assume you really meant "2015-07-01T17:42:49+04:00" but made typos.]

You really should not care about this. Indeed, I recommend you always include both the hours and the minutes as I have seen multiple libraries/protocols that expect both hours and minutes, breaking if omitted. While the minutes and seconds are indeed optional in ISO 8601 when their value is zero, I suggest you always include the minutes when zero.

DateTimeFormatter

If you insist otherwise, you will need to use DateTimeFormatter class, and possibly DateTimeFormatterBuilder, to suppress the minutes when zero. Perhaps this:

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-dd'T'HH:mm:ssx" );
String output = odt.format( f );

2015-07-01T17:42:49+04

The x code in that formatting pattern suppressed the minutes, and seconds, if their value is zero.

If doing your own formatting, be sure to not truncate when non-zero, or your result will be a falsehood (a different moment). Take for example, representing this moment as seen in India where current the offset in use is five and half hours (an offset that includes 30 minutes rather than zero).

ZoneId z = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = odt.atZoneSameInstant( z );
OffsetDateTime odtKolkata = zdt.toOffsetDateTime();

Dump to console.

System.out.println( "odtKolkata = " + odtKolkata );

2015-07-01T19:12:49+05:30

XMLGregorianCalendar

If you absolutely must use the old legacy class XMLGregorianCalendar, you can create one from the ISO 8601 output of our OffsetDateTime object seen in code above. See this Answer on another Question.

XMLGregorianCalendar xgc = null;
try
{
    xgc = DatatypeFactory.newInstance().newXMLGregorianCalendar( odt.toString() );
}
catch ( DatatypeConfigurationException e )
{
    e.printStackTrace();
}
System.out.println( "xgc.toString(): " + xgc );

xgc.toString(): 2015-07-01T17:42:49+04:00


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.

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

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

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

Upvotes: 2

Related Questions