Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
How to set timestamp with zone in XMLGregorianCalendar

I have DateTime in this format '2020-11-08T13:05:46.000-07:00' and trying to set the same to XMLGregorianCalendar. it gets automatically converted to 2020-11-08T20:05:46.000+0000.

Anyway to save it as UTC?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

tl;dr

OffsetDateTime
.parse( "2020-11-08T13:05:46.000-07:00" )
.toInstant() 

java.time

XMLGregorianCalendar is a legacy class, supplanted years ago by ZonedDateTime.

Your input has no time zone, only an offset-from-UTC. So parse that string as an OffsetDateTime.

OffsetDateTime odt = OffsetDateTime.parse( "2020-11-08T13:05:46.000-07:00" ) ;

Easiest way to adjust to UTC (an offset of zero hours-minutes-seconds) is to extract an Instant.

Instant instant = odt.toInstant() ;
over 4 years ago · Santiago Trujillo Report

0

You can do it as follows:

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Main {
    public static void main(String[] args) throws DatatypeConfigurationException {
        String givenDateTimeString = "2020-11-08T13:05:46.000-07:00";
        XMLGregorianCalendar xmlGregorianCalendar = DatatypeFactory.newInstance()
                .newXMLGregorianCalendar(givenDateTimeString);
        System.out.println(xmlGregorianCalendar);
    }
}

Output:

2020-11-08T13:05:46.000-07:00

However, I suggest you switch to the modern date-time API.

Using the modern date-time API:

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String givenDateTimeString = "2020-11-08T13:05:46.000-07:00";
        OffsetDateTime odt = OffsetDateTime.parse(givenDateTimeString);
        // Default format i.e. OffsetDateTime#toString
        System.out.println(odt);

        // Custom format
        System.out.println(odt.format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSXXX", Locale.ENGLISH)));

        // Convert it to date-time at UTC
        OffsetDateTime odtUTC = odt.withOffsetSameInstant(ZoneOffset.UTC);
        System.out.println(odtUTC);
        System.out.println(odtUTC.format(DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX", Locale.ENGLISH)));
    }
}

Output:

2020-11-08T13:05:46-07:00
2020-11-08T13:05:46.000-07:00
2020-11-08T20:05:46Z
2020-11-08T20:05:46.000Z

Learn more about the modern date-time API at Trail: Date Time.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!