Search code examples
javadatetimetimezonedstcountry-codes

epoch timestamp to country local TZD format


I have epoch timestamp and country. I want convert that timestamp into given country local timestamp with time zone region with Daylight Saving (TZD) format as below

yyyy-mm-ddThh:MM:ssTZD
e.g. 2018-02-25T22:36:23+0100

Timestamp : 1525370235
Country : NLD (ISO alpha-3 code)

How I can achieve this in Java?

Update 1 :

I am using this kind of conversion but not sure is it correct or wrong. Can somebody let me know is this the corect way to do conversion

Locale locale = new Locale("NLD");
Date date = new Date(1525381190 * 1000L);
    SimpleDateFormat dateFormatCN = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ", locale);

 //Output : 2018-05-03T01:59:50-0700

Solution

  • is this the corect way to do conversion

    No it is not. I see the following issues.

    • The whole thing is impossible because many countries span several time zones. This means, from a country code you cannot know which time zone to choose. Since the local time varies by time zone, you cannot print the correct time in these cases. Countries that span more than one time zone include but are not limited to Canada, USA, Mexico, Greenland, Brazil, Ecuador, Russia, Indonesia, Kiribati — and even the Netherlands, your example country. Note that some of the countries with the largest populations are in the list, so if your data are representative of the world population you may more often than not encounter this problem. Please see this list: List of time zones by country.
    • Locale and time zone are two different things and though both more or less tied to places in no way directly related. So obtaining the correct locale will not help you. You can also see that your printed output has UTC offset -0700, where the correct offset for Amsterdam in summer would have been +0200.
    • new Locale("NLD") will not give you a valid locale for the Netherlands. The argument to the Locale(String) constructor is a language tag like nl for Dutch or en for English. A three-letter country code will not work.
    • Therefore, if you wanted to do just some of the work you are asking about, you would need access to some database or databases that can convert country code to country and country to time zones. I don’t know of a good choice here, but I would expect that some such databases are available.

    java.time

    Finally, don’t use SimpleDateFormat and Date. Those classes are long outdated and poorly designed. The modern correct way to format your epoch timestamp value for mainland Netherlands is:

        ZoneId zone = ZoneId.of("Europe/Amsterdam");
        String nldTime = Instant.ofEpochSecond(1_525_370_235L)
                .atZone(zone)
                .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
        System.out.println(nldTime);
    

    This prints:

    2018-05-03T19:57:15+02:00

    If you need the offset without colon as in +0200, use DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssXX").

    If instead of Amsterdam you wanted the time in the Caribbean municipalities of the Netherlands, use America/Kralendijk time zone. Then the result is 2018-05-03T13:57:15-04:00.