1

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;
if(hour<0){
    hour += 24
}
minutes -= minutesOffset;
if(minutes<0){
    minutes += 60
}
gaseda4
  • 23
  • 3
  • 2
    What have you tried so far? Where are you stuck? – Nico Haase Dec 06 '19 at 09:00
  • At this point I'm trying to do it manually, however it gets complicated once I get to times and minutes like 00 which also change the day and, in some case the month. I was wondering if there was a way to to this automatically – gaseda4 Dec 06 '19 at 09:02
  • 1
    Do you really mean "01000" at the end? I had expected a "0100" (one less zero), which corresponds to the usual "+01:00"! – Seelenvirtuose Dec 06 '19 at 09:09
  • No, just edited, sorry – gaseda4 Dec 06 '19 at 09:09
  • 1
    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? – Seelenvirtuose Dec 06 '19 at 09:11
  • 1
    the + or - is given before, so i know if that 01:00 is a plus or minus – gaseda4 Dec 06 '19 at 09:13
  • 1
    ??? In your string example ("2019120610000100"), there is no plus or minus sign! – Seelenvirtuose Dec 06 '19 at 09:15
  • there's another input for it, once i get that string in another string variable i have the + or - sign. the string with the time,date and timezone must be converted to utc, and the timezone is + o - depending on another variable. i know it's confusing but i have no other choice! – gaseda4 Dec 06 '19 at 09:18
  • the only this i could do, if it helps, is place that sign in the time and date string so it becomes 201912061000+0100 – gaseda4 Dec 06 '19 at 09:19
  • 1
    It is not only confusing. It is an important information that you left out in your question! Please edit your question accordingly, and also add the programming attempts you made (as you are already asked for). As it is, your question is not suitable for SO! – Seelenvirtuose Dec 06 '19 at 09:19
  • i now fixed my question, thanks! – gaseda4 Dec 06 '19 at 09:42
  • @Seelenvirtuose I suggest you educate the publisher of that date-time string about the standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formats for transmitting date-time values as text. It is just silly of them to invent their own clumsy format. – Basil Bourque Dec 06 '19 at 19:32

3 Answers3

2

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")); 
2

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

System.out.println(datetimeString);
System.out.println(odt);
System.out.println(odtUTC);

will get you the following output:

201912061000+0100

2019-12-06T10:00+01:00

2019-12-06T09:00Z

Community
  • 1
  • 1
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
  • It’s an extraordinarily good answer. The explanations and suggestions are to the point and terse. – Ole V.V. Dec 07 '19 at 08:44
0
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"));

System.out.println(dtfOutput.format(timezoneAdjustedDate));

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.

SteffenJacobs
  • 402
  • 2
  • 10
  • 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 – gaseda4 Dec 06 '19 at 09:24