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
is this the corect way to do conversion
No it is not. I see the following issues.
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.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
.