Date and time to UTC given timezone

i have the following string: 2019120610000100 which corresponds to 2019/12/06 at 10:00 +1. How can I convert this to utc time, in this case 2019/12/06 09:00?

This string could also have a +2, +3 ... -1, -2 ... timezone so I must be able to convert other strings too.

The + or - sign is given in another instance however, if it can be useful, it can be added to the time and date string. (The string could become 201912061000 +0100)

Right now I'm converting it manually splitting the string but I'm trying to find a way to make this safe as it gets tricky with hours and minutes like 00 that have to change the day, possibly the month and year. This is what I have made so far:

hour -= hourOffset;
    hour += 24
minutes -= minutesOffset;
    minutes += 60

When dealing with dates and times, it is usually better to not do string operations but use one of the many classes that extend java.time.temporal.Temporal from the java.time package - introduced with Java 8.

In your case, you want to use an OffsetDateTime, as your string represents exactly that: A date-time with an offset. Note, that a ZonedDateTime is not really appropriate here, because the offset information (e.g. "+01:00") is not enough to represent a whole timezone. Look at this SO question for more information.

To get an OffsetDateTime from a string, you must simply parse it.

Let's do it.

Step 1: Adjust your string to contain the offset sign (plus or minus).

String offsetSign = "+";
String datetimeString = "2019120610000100";
datetimeString = new StringBuilder(datetimeString).insert(datetimeString.length() - 4, offsetSign).toString();

Step 2: Parse that string to an OffsetDateTime object.

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmZ");
OffsetDateTime odt = OffsetDateTime.parse(datetimeString, dtf);

Step 3: Convert that OffsetDateTime to UTC.

OffsetDateTime odtUTC = odt.withOffsetSameInstant(ZoneOffset.UTC);

Printing out those variables


will get you the following output:




TimeZoneInfo.ConvertTimeToUtc Method (System), Converts the specified date and time to Coordinated Universal Time (UTC). ConvertTimeToUtc(DateTime, TimeZoneInfo). Converts the time in a specified time� i have the following string: 2019120610000100 which corresponds to 2019/12/06 at 10:00 +1. How can I convert this to utc time, in this case 2019/12/06 09:00? This string could also have a +2, +3 .

You can directly convert the time to UTC by the following code

 String dateStr = "201912061000+0100";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
    final LocalDateTime parse = LocalDateTime.parse(dateStr.substring(0, dateStr.length()-5), formatter);
    final ZoneId zone = ZoneId.of("GMT"+dateStr.substring(12,15)+":"+dateStr.substring(15));
    final ZonedDateTime given = ZonedDateTime.of(parse, zone);
    final String toUTC = given.withZoneSameInstant(ZoneId.of("UTC"))
        .format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm")); 

Converting times between time zones, ConvertTimeToUtc method to convert Eastern Standard Time to UTC. C# Copy. DateTime easternTime = new DateTime� Separating date and time parts with other characters such as space is not allowed in ISO 8601, but allowed in its profile RFC 3339. If a time zone designator is required, it follows the combined date and time. For example, "2007-04-05T14:30Z" or "2007-04-05T12:30-02:00".

String dateStr = "2019120610000100";
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("yyyyMMddHHmm Z");
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm");

String adjustedDateStr = new StringBuilder(dateStr).insert(dateStr.length() - 4, " +").toString();
ZonedDateTime givenDate = ZonedDateTime.parse(adjustedDateStr, dtfInput);

ZonedDateTime timezoneAdjustedDate = ZonedDateTime.ofInstant(givenDate.toInstant(), ZoneId.of("UTC"));


Since you handle the plus or minus for the timezone offset externally, you can just insert it into the exsample above instead of the plus if need be.

Working with Time Zones, Therefore, quotes or references to specific information in the 6.5 Working with Date and Time Values that Require a Time Zone (and not a zone offset) UTC Offset All time zones have, as their basis, an offset from UTC. UTC / GMT is the basis for local times worldwide. Other names: Universal Time Coordinated / Universal Coordinated Time. Successor to: Greenwich Mean Time (GMT) Military name: “Zulu” Military Time. Longitude: 0° (Prime Meridian) At sea: Longitudes between 7.5° West and 7.5° East. Note: United Kingdom is one hour ahead of UTC / GMT during

Time & date: Essential concepts, Time zone identifiers allow you to refer to a particular difference from UTC that includes both zone offsets and daylight savings time. In the TZ database, time zones are given IDs that typically consist of a� Return the current UTC date and time, with tzinfo None. This is like now(), but returns the current UTC date and time, as a naive datetime object. An aware current UTC datetime can be obtained by calling See also now().

8.1. datetime — Basic date and time types — Python 3.3.7 , An aware object is used to represent a specific moment in time that is not open to interpretation [1]. from UTC time, the time zone name, and whether Daylight Saving Time is in effect. object timedelta tzinfo timezone time date datetime� This article describes how to work with date and time data in Azure Monitor log queries. Date time basics. The Kusto query language has two main data types associated with dates and times: datetime and timespan. All dates are expressed in UTC. While multiple datetime formats are supported, the ISO8601 format is preferred.

Handling Local Timezones, UTC, Daylight Savings Time, and Leap , Because there is no specific transformer to carry out this operation, the user Where time zone has been added to the date as a UTC offset. As Jon Skeet already said, java.util.Date does not have a time zone. A Date object represents a number of milliseconds since January 1, 1970, 12:00 AM, UTC. It does not contain time zone information.

  • What have you tried so far? Where are you stuck?
  • Do you really mean "01000" at the end? I had expected a "0100" (one less zero), which corresponds to the usual "+01:00"!
  • Additionally ... Without any "+" or "-" for the offset, this string representation of a timestamp is ambiguous. It could be "... +01:00" or "... -01:00" !!! How to distinguish?
  • the + or - is given before, so i know if that 01:00 is a plus or minus
  • ??? In your string example ("2019120610000100"), there is no plus or minus sign!
  • It’s an extraordinarily good answer. The explanations and suggestions are to the point and terse.
  • this doesn't convert to utc, i get back the initial date. This is the output 2019/12/06 10:00 but with a +1 timezone difference i shoud get 2019/12/06 09:00