Is there an xml timestamp format like the java timestamp?

11,542

Solution 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);
}

Solution 2

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

Solution 3

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");
Share:
11,542
sabrina2020
Author by

sabrina2020

Software engineer

Updated on June 05, 2022

Comments

  • sabrina2020
    sabrina2020 almost 2 years

    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.