Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to display local date/time for different countries in java

I have to display local date time for different countries in java.Currently I am setting zoneId for "America/New_York" and so only getting EST time for all the places on the server.How to achieve local date/time dynamically.

DateTimeFormatter globalFormat = DateTimeFormatter.ofPattern("dd-MMM-yyyy hh:mm a z");
DateTimeFormatter estFormat = DateTimeFormatter.ofPattern("dd-MMM-yyyy hh:mm a 'EST'");
ZoneId istZoneId = ZoneId.of("Asia/Calcutta");
ZoneId estZoneId = ZoneId.of("America/New_York");

Instant instant = Instant.now() ;
ZonedDateTime zdt = ZonedDateTime.now( estZoneId) ;
ZonedDateTime currentISTime = instant.atZone(istZoneId);                //India time
ZonedDateTime currentESTime = zdt.withZoneSameInstant(estZoneId);       //EST Time         

System.out.println("est time.............."+estFormat.format(currentESTime)); 
like image 372
Anjali Singh Avatar asked May 19 '26 12:05

Anjali Singh


2 Answers

Make use of ZoneId.systemDefault():

public static void main(String[] args) {
    // take the instant
    Instant instant = Instant.now();
    // use it in system default time zone
    ZonedDateTime zdt = instant.atZone(ZoneId.systemDefault());
    // then in Asia/Calcutta
    ZonedDateTime currentISTime = instant.atZone(ZoneId.of("Asia/Calcutta"));
    // and in America/New York
    ZonedDateTime currentESTime = zdt.withZoneSameInstant(ZoneId.of("America/New_York"));
    // then print them all using the ISO format for zoned date times
    System.out.println("System default:\t" + zdt.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    System.out.println("EST:\t\t" + currentISTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
    System.out.println("IST:\t\t" + currentESTime.format(DateTimeFormatter.ISO_ZONED_DATE_TIME));
}

On my system, this prints

System default: 2019-11-26T12:19:18.865+01:00[Europe/Berlin]
EST:            2019-11-26T16:49:18.696+05:30[Asia/Calcutta]
IST:            2019-11-26T06:19:18.865-05:00[America/New_York]

Find out what it does on yours and the remaining related ones. That's quite dynamical by relying on the system default (which may be manipulated under circumstances).

like image 106
deHaar Avatar answered May 22 '26 01:05

deHaar


ZoneId with ZonedDateTime is what you want.

ZoneId zoneId = ZoneId.systemDefault();
Locale locale = Locale.getDefault();

You could offer a switch, which is useful for development too:

Set<String> availableZoneIds = ZoneId.getAvailableZoneIds();
Locale[] availabelLocales = Locale.getAvailableLocales();

Adding your own Locale might sometimes be required, especially for small countries or Esperanto. It requires some effort.

Usage of ZonedDateTime for the default:

ZonedDateTime zdt = ZonedDateTime.now();

A localized date/time/date-time formatting: DateTimeFormatter:

DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG).withLocale(Locale.FRANCE);

(withLocale should you want to use a specific locale.)

Needless to say store all as universal coordinated time, Z, or ZoneId.of("UTC").

like image 43
Joop Eggen Avatar answered May 22 '26 00:05

Joop Eggen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!