user12272269
user12272269

Reputation: 67

How to convert a date with time zone offset to another format

I tried reading a lot about the date formatting with time zone, but it doesn't make sense to me.

My DB shows this datetime: 2022-12-01 04:00:00.000 +08:00
My UI shows it as: Thu 01/12/2022 12:00

I need to compare between them to verify they are the same. I tried to convert the DB time like this:

String dbDate = "2022-12-01 04:00:00.000 +08:00";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS XXX");
Date date = sf.parse(dbDate)

sf = new SimpleDateFormat("EEE dd/MM/yyy HH:mm");  
String uiDate = sf.format(date);

The results received is a completely different date: 'Wed 30/11/2022 22:02'.

I don't understand the logic here and would appreciate help in converting it correctly.

Upvotes: 0

Views: 1333

Answers (1)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 78945

As already commented by Yonatan Karp-Rudin, you can not compare a date-time with time-zone offset with another without time-zone offset. A clear way to compare the two date-times is to bring them to a single time-zone e.g. you can apply the same time-zone offset to the UI date-time as of the DB date-time.

In March 2014, java.time API supplanted the error-prone legacy date-time API. Since then, it has been strongly recommended to use this modern date-time API.

Demo using java.time API:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String dt1 = "2022-12-01 04:00:00.000 +08:00";
        String dt2 = "Thu 01/12/2022 12:00";

        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS XXX", Locale.ENGLISH);
        OffsetDateTime odt1 = OffsetDateTime.parse(dt1, dtf1);
        System.out.println(odt1);

        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("EEE dd/MM/uuuu HH:mm", Locale.ENGLISH)
                .withZone(odt1.getOffset());
        OffsetDateTime odt2 = OffsetDateTime.parse(dt2, dtf2);
        System.out.println(odt2);

        System.out.println(odt1.equals(odt2));
    }
}

Output:

2022-12-01T04:00+08:00
2022-12-01T12:00+08:00
false

Assuming both the date-times belong to the same time-zone offset, another way to compare them would be compare them without time-zone i.e. comparing their date-time part only (LocalDateTime).

Demo:

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        String dt1 = "2022-12-01 04:00:00.000 +08:00";
        String dt2 = "Thu 01/12/2022 12:00";

        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS XXX", Locale.ENGLISH);
        LocalDateTime ldt1 = OffsetDateTime.parse(dt1, dtf1).toLocalDateTime();
        System.out.println(ldt1);

        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("EEE dd/MM/uuuu HH:mm", Locale.ENGLISH);
        LocalDateTime ldt2 = LocalDateTime.parse(dt2, dtf2);
        System.out.println(ldt2);

        System.out.println(ldt1.equals(ldt2));
    }
}

Output:

2022-12-01T04:00
2022-12-01T12:00
false

The modern date-time API (java.time API) provides you with tools to do the same thing in many ways e.g. in the 1st demo, we could obtain the OffsetDateTime for your UI date-time string by parsing it into a LocalDateTime as shown in the 2nd demo and then using one of the ways shown in this answer where ZoneOffset offset = odt1.getOffset().

By the way, here is an example of how you format a date-time with time-zone offset to another format:

public class Main {
    public static void main(String[] args) {
        String dtDb = "2022-12-01 04:00:00.000 +08:00";
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS XXX", Locale.ENGLISH);
        OffsetDateTime odtDb = OffsetDateTime.parse(dtDb, parser);
        System.out.println(odtDb);

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE dd/MM/uuuu HH:mm", Locale.ENGLISH);
        String strDtUi = odtDb.format(formatter);
        System.out.println(strDtUi);
    }
}

Output:

2022-12-01T04:00+08:00
Thu 01/12/2022 04:00

Learn more about the modern Date-Time API from Trail: Date Time.

Upvotes: 4

Related Questions