-1

I try to convert a time with timezone to time but failed to, could you please help to advise?

Time with timezone: 2019-01-31T05:20:18.728+02:00 (as you may notice, there is an offset at the end of the time +02:00. Instead of 05:20:18.728+02:00, I want to print out 07:20:18 which means 05:20:18 + the time offset).

Time needed to show: 07:20:18

...
LocalDateTime ldt = LocalDateTime.now(); //my local time is set to GMT
ZoneOffset offset1 = ZoneOffset.ofHours( (offset%24) );
OffsetDateTime odt = ldt.atOffset( offset1);
System.out.println(odt);
...

My question is updated.

Thanks much, Ivy

Ivy
  • 41
  • 5
  • 1
    You don't seems to put much efforts, Thanks much ,CM – Vishwa Ratna Jan 31 '19 at 05:30
  • 1
    There is almost never a reason to call `LocalDateTime.now()`. To represent the current moment, use `Instant`, `OffsetDateTime`, or `ZonedDateTime`. – Basil Bourque Jan 31 '19 at 06:08
  • Your call to `now` relies implicitly on your JVM’s current default time zone. That zone could vary at runtime, so it is unreliable. Instead specify your desired/expected zone or offset. `OffsetDateTime.now( ZoneOffset.UTC )` – Basil Bourque Jan 31 '19 at 06:09
  • 1
    What exactly is your Question? Are you starting with an input string of `2019-01-31T05:20:18.728+02:00`? Or are you starting with the current moment? – Basil Bourque Jan 31 '19 at 06:10
  • 1
    Capturing the current moment in UTC is simply: `Instant.now()` – Basil Bourque Jan 31 '19 at 06:13
  • @CommonMan thanks all your comments! I updated my question. I struggled with this for 3 hours and decided that I need help. – Ivy Jan 31 '19 at 06:35
  • 1
    @Ivy (A) Your Question is not at all clear as to your inputs, desired outputs, nor your goal. (B) There are many many Questions and Answers already posted here on the subjects of capturing the current moment and adjusting between UTC and time zones. You are expected to search Stack Overflow thoroughly before posting. (C) If you rewrite your Question to be clear and specific, and explain how none of the existing Questions & Answers address it, then your Question can be re-opened. – Basil Bourque Jan 31 '19 at 06:56
  • You’ve misunderstood what the offset means. …`T05:20:18.728+02:00` means that an offset of 2 hours *has* been applied, so this is the same second in time as **03:20:18 UTC**. – Ole V.V. Jan 31 '19 at 10:39
  • The question is thoroughly unclear, I am sorry. I’d so much like to help, but without understanding your situation, I stand no chance. If you need to show the current local time in your time zone, use `LocalTime.now(yourTimeZoneId)`, where `yourTimeZoneId` could for instance be `ZoneId.of("America/Swift_Current")`. Or to display it with offset: `ZonedDateTime.now(ZoneId.of("America/Swift_Current")).format(DateTimeFormatter.ISO_OFFSET_TIME)`. This just yielded `04:49:32.021-06:00`. – Ole V.V. Jan 31 '19 at 10:45

1 Answers1

1

You can use the Java-8 DateTimeFormatter class to format a LocalTime instance to get only the time portion.

public static void main(String[] args) {
        LocalTime localTime = LocalTime.now(ZoneId.of("GMT"));
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
        System.out.println(localTime.format(dtf));

        //From an ISO date string
        LocalTime parseTime = LocalTime.from(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse( "2019-01-31T05:20:18.728+02:00"));
        System.out.println(parseTime.format(dtf));
}

Output:

05:47:57
05:20:72
Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
  • Thanks, my question is to show to the time + the time offset, not to just extract the time. – Ivy Jan 31 '19 at 06:38