0

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";
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
  • 2
    When you say "convert to UTC+2" which time offset does your time instance currently have? Can you provide a MWE with code? – knittl Aug 06 '20 at 13:12
  • 1
    Your example date and your date format do not match. `2019:11:12` *is not* in the format `yyyy/MM/dd`. – Polygnome Aug 06 '20 at 13:20
  • 4
    Don't do this! Java has some classes that already have time. Check out `LocalTime` and `ZoneDateTime`. It would probably help if you step back and explain what problem you're trying to solve. – Matthew Aug 06 '20 at 13:40
  • A problem with this is that different countries use different definitions of "summer". I (German) once had a regular Skype chat with a Texan, and it turned out that, while we both had daylight saving time, our DST periods were different so there was a two-week period every spring and fall where the difference between our timezones was just wrong. So, I agree exactly with @Matthew, you should not even *attempt* to implement this yourself, and rely on pre-existing implementations. (Incidentally, even Sun Microsystems' first attempt at this was horrible and had to be replaced in Java 8.) –  Aug 06 '20 at 14:26
  • 1
    FYI: No need to apologize here for you English, which is actually just fine. This site sees many non-native English speakers. And no need for “thanks in advance”, as this site is designed to be terse, focused, and to avoid chit-chat and extraneous discussion. I edited your Question for these points. Your only fault here is not searching Stack Overflow thoroughly before posting. Basic date-time questions have been asked and answered many times already. – Basil Bourque Aug 06 '20 at 14:36
  • Tip: To search Stack Overflow, use a search engine like DuckDuckGo, Bing, Google with `site:stackoverflow.com` rather than this site’s weak built-in Search feature. – Basil Bourque Aug 06 '20 at 14:42
  • Use of `GMT+X` or `GMT-X` (or `UTC` which is practically the equivalent of `GMT`) is heavily discouraged. Instead, use a specific timezone (e.g. `Europe/Brussels`, `America/New_York`, `Asia/Hong_Kong`, ...). Some timezones have daylight saving times, some don't, some have it at different times in the year. This knowledge is embedded in a specific timezone. It is not when you work directly with GMT or UTC. – TT. Oct 04 '20 at 12:38

2 Answers2

2

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

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
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110