1

In order to process an OffsetTime, I would like to convert it to an local OffsetTime. For example convert 14:03:04.708-01:00 to 16:03:04.708+01:00, if the systems default offset is +1:00.

How do I do this?

Stefan K.
  • 7,701
  • 6
  • 52
  • 64

3 Answers3

3

Use OffsetTime.withOffsetSameInstant:

OffsetTime input = OffsetTime.of(14, 3, 4, 708000000, ZoneOffset.ofHours(-1));
System.out.println(input);

OffsetTime output = input.withOffsetSameInstant(ZoneOffset.ofHours(1));
System.out.println(output);
Jesper
  • 202,709
  • 46
  • 318
  • 350
3

As @Jesper's answer correctly states the way to change the offset on an OffsetTime instance, without changing the Instant it points to is by invoking withOffsetSameInstant.

The hard part is determining the system's default offset. A system does not have a default offset, it has a default ZoneId. A ZoneId has an offset, but that may change throughout the year, if DST is applied.

It's unclear from your question whether we can determine which time of the year is relevant for your use case. It may be that now is valid for your use case. It may be that you have a LocalDate on which the OffsetTime is to be evaluated against. If that is the case you can get the system default like this :

    LocalDateTime localDateTime = LocalDateTime.of(localDate, offsetTime.toLocalTime());
    ZoneOffset offset = ZoneId.systemDefault().getRules().getOffset(localDateTime);
bowmore
  • 10,842
  • 1
  • 35
  • 43
0
  1. The modern Date-Time API is based on ISO 8601 and does not require using a DateTimeFormatter object explicitly as long as the Date-Time string conforms to the ISO 8601 standards.
  2. The timezone offset of place observing DST changes as per the summer/winter time. Therefore, if your place observes DST, you should not use a fixed timezone offset e.g. +01:00 hours; rather, you should get it from the API.

Demo:

import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        String strTime = "14:03:04.708-01:00";

        OffsetTime parsedOffsetTime = OffsetTime.parse(strTime);
        System.out.println(parsedOffsetTime);

        OffsetTime offsetTimeAtOffset0100 = parsedOffsetTime.withOffsetSameInstant(ZoneOffset.of("+01:00"));
        System.out.println(offsetTimeAtOffset0100);

        // Time at JVM's default timezone offset
        ZoneOffset jvmTzOffset = ZonedDateTime.now(ZoneId.systemDefault()).getOffset();
        OffsetTime offsetTimeAtJvmTzOffset = parsedOffsetTime.withOffsetSameInstant(jvmTzOffset);
        System.out.println(offsetTimeAtJvmTzOffset);
    }
}

Output:

14:03:04.708-01:00
16:03:04.708+01:00
16:03:04.708+01:00

Note: My timezone is Europe/London which has an offset of 01:00 hours currently.

Learn more about java.time, the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110