Calculation of time differences in Java

0

I have a BDD that has a field in Timestamp, the record creates it in the BDD according to the time zone of the server which I can know.

On the other hand, the GUI is in another time zone (I can also know it). Someone can help me to convert the time coming from the BDD (with its time zone) to the one that will be displayed in the GUI (in its time zone) or which API I can use more efficiently.

Thank you.

    
asked by LearningJava2018 01.09.2018 в 18:52
source

1 answer

0

Taken from here link

Translated basically what it says is that java.sql.TimeStamp has no time zones, they are instants in time as java.util.Date.

What you can do is change using the LocalDateTime class, (which exists from JAVA 1.8), so that you apply the offset of your time zone.

 Timestamp tiempoUTC= new Timestamp(
                ZonedDateTime.of(2018, 8, 25, 11, 23, 46, 0,
                        ZoneId.of("UTC")
                ).toInstant().toEpochMilli());
        LocalDateTime momento=tiempoUTC.toLocalDateTime();

        System.out.println("Momento: "+momento); 

The result of this code is:

Momento: 2018-08-25T13:23:46

As you can see, when I had a Locale with an offset of +2, I changed the time I had, adding 2 hours

    
answered by 01.09.2018 / 19:18
source