2

My java timestamp has the following format:

YYYY-MM-DD hh:mm:ss.ms
2016-01-08 15:16:44.554

I got it using the following method:

private String getCurrentTimeStamp() {

    Date date= new java.util.Date();        
    return((new Timestamp(date.getTime())).toString());

}

Is there a standardized xml date and Time format for timestamp? The xs: dateTime has the following format: "YYYY-MM-DDThh: mm: SS" And it is not taking into consideration milliseconds.

TastyCat
  • 190
  • 1
  • 16
sabrina2020
  • 2,102
  • 3
  • 25
  • 54
  • 3
    [dateTime](http://www.w3.org/TR/xmlschema11-2/#dateTime) does in fact allow milliseconds. In fact, it permits [an infinite number of decimal places for seconds](http://www.w3.org/TR/xmlschema11-2/#nt-seFrag). – VGR Jan 20 '16 at 15:16

5 Answers5

3

XML itself does not define any timestamp formats.

XML Schema Part 2: Datatypes Second Edition incorporates ISO 8601 formats by reference. The dateTime format allows but does not require a decimal point followed by arbitrary fractions of a second. For example, 2016-01-08T15:16:44.554

2

In XML Schema (XSD), all formats of dates and times are well defined.

The java.time framework built into Java 8 and later supports these formats. See Tutorial.

Here are some examples:

// Dates in XML: YYYY-MM-DD
DateTimeFormatter.ISO_LOCAL_DATE.parse("2002-09-24");

// Dates with TimeZone in XML: YYYY-MM-DDZ
DateTimeFormatter.ISO_DATE.parse("2002-09-24Z");

// Dates with TimeZone in XML: YYYY-MM-DD-06:00 or YYYY-MM-DD+06:00
DateTimeFormatter.ISO_DATE.parse("2002-09-24-06:00");

// Times in XML: hh:mm:ss
DateTimeFormatter.ISO_TIME.parse("09:00:00");
DateTimeFormatter.ISO_TIME.parse("09:00:00.5");

// DateTimes in XML: YYYY-MM-DDThh:mm:ss (with an optional TimeZone)
DateTimeFormatter.ISO_DATE_TIME.parse("2002-05-30T09:00:00");
DateTimeFormatter.ISO_DATE_TIME.parse("2002-05-30T09:30:10.5");
DateTimeFormatter.ISO_DATE_TIME.parse("2002-05-30T09:00:00Z");
DateTimeFormatter.ISO_DATE_TIME.parse("2002-05-30T09:30:10.5-06:00");

Durations and Periods however are not perfectly compatible, because they are split in Durations and Periods in Java. Here are however some examples:

Period.parse("P5Y");
Period.parse("P5Y2M10D");
Duration.parse("PT15H");
Duration.parse("-P10D");
Community
  • 1
  • 1
steffen
  • 16,138
  • 4
  • 42
  • 81
  • 1
    FYI, these formats are defined by the ISO 8601 standard. The [Wikipedia page on ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) is a must-read. – Basil Bourque Jan 20 '16 at 21:12
1

If you can't change the schema, you have to change your function

private String getCurrentTimeStamp() {
    Date date = new java.util.Date();        
    return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(date);
}
Gianni B.
  • 2,691
  • 18
  • 31
0

Maybe you mean the standart ISO for Date.

On this thread

Convert Java Date...

See also:

What is Jaxb and why would i use it

Community
  • 1
  • 1
Rafik Bex
  • 51
  • 7
0

There seems to be a format as part of XSD 1.1:

The type xsd:dateTimeStamp represents a specific date
and time in the format CCYY-MM-DDThh:mm:ss.sss.

http://www.datypic.com/sc/xsd11/t-xsd_dateTimeStamp.html