KarlProgramm
KarlProgramm

Reputation: 9

JAVA : Convert hour into UTC+1 during Winter and UTC+2 during Summer

I'm learning Java and I try to understand how the hour system work. The hour format I use is HH:mm:ss. I want to convert this string into UTC+1 hour during winter and UTC+2 during summer. To know the date, I use the format : yyyy/MM/dd. Below, an exemple of the kind of variable I use.

Can someone help me to resolve this problem ?

String Hour = "14:12:13"; 
String Date = "2019:11:12";

Upvotes: 0

Views: 551

Answers (2)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79540

I want to convert this string into UTC+1 hour during winter and UTC+2 during summer.

import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;

public class Main {
    public static void main(String[] args) {
        String timeString = "14:12:13";
        LocalTime time = LocalTime.parse(timeString);
        System.out.println(time);

        // Time at UTC+1
        OffsetTime timeAtUTC1 = time.atOffset(ZoneOffset.UTC).withOffsetSameInstant(ZoneOffset.ofHours(1));
        System.out.println(timeAtUTC1);

        // Time at UTC+2
        OffsetTime timeAtUTC2 = time.atOffset(ZoneOffset.UTC).withOffsetSameInstant(ZoneOffset.ofHours(2));
        System.out.println(timeAtUTC2);
    }
}

Output:

14:12:13
15:12:13+01:00
16:12:13+02:00

Upvotes: 0

Basil Bourque
Basil Bourque

Reputation: 339837

The hour format I use is HH:mm:ss

That is standard ISO 8601 format.

The java.time classes use ISO 8601 formats be default when parsing/generating text.

I want to convert this string into UTC+1 hour during Winter and UTC+2 during Summer.

Java includes the OffsetTime class to represent a time-of-day with an offset-from-UTC. But this concept is faulty. Both my reading and my reasoning fail to make sense of a time with offset yet lacking a date.

I believe this class exists only to match the same idea defined by the SQL spec. Again, senseless as far as I can tell. Not the only senseless thing in the SQL spec.

To know the Date, i use the format : yyyy/MM/dd.

For data exchange, logging, and debugging, I suggest you stick with ISO 8601 format which is YYYY-MM-DD. That is like your format but using hyphen rather than slash as delimiter.

For presentation to the user, let Java automatically localize using DateTimeFormatter.ofLocalized… methods. No point in hard-coding a format for your users.

Behind, an exemple of the kind of variable i use.

For time-of-day, use LocalTime.

For date, use LocalDate.

For offset, use ZoneOffset.

For time zone, use ZoneId.

For a moment, use the combination of LocalDate, LocalTime, and ZoneOffset to get a OffsetDateTime. Generally better to switch out offset for ZoneId to get a ZonedDateTime.

"14:12:13"; String Date = "2019:11:12"

LocalTime lt = LocalDate.parse( "14:12:13" ) ;
LocalDate ld = LocalDate.parse( "2019-11-12" ) ; 
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;

ZonedDateTime zdt = ZonedDateTime.of( ld , lt , z ) ;

This has all been covered many many many times already on Stack Overflow. So, I am being brief here. Search to learn more.

Upvotes: 2

Related Questions