Friday 7 June 2019

Java: Get date and time for given time zone


Below snippet return time in given time zone.

public static Date getDate(TimeZone timeZone) throws ParseException {
         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
         dateFormat.setTimeZone(timeZone);
         SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
         Date date =  dateFormatLocal.parse(dateFormat.format(new Date()));
         return date;
}

App.java
package com.sample.app;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

public class App {

 public static Date getDate(TimeZone timeZone) throws ParseException {
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
  dateFormat.setTimeZone(timeZone);
  SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
  Date date = dateFormatLocal.parse(dateFormat.format(new Date()));
  return date;
 }

 public static void main(String args[]) throws IOException, ParseException {

  Map<String, String> zoneIds = ZoneId.SHORT_IDS;

  for (String key : zoneIds.keySet()) {
   TimeZone timeZone = TimeZone.getTimeZone(key);
   Date date = getDate(timeZone);
   System.out.println("For [" + key + ", " + zoneIds.get(key) + "] -> " + date);
  }

 }
}


Sample Output
For [CTT, Asia/Shanghai] -> Sat Jun 08 13:13:21 IST 2019
For [ART, Africa/Cairo] -> Sat Jun 08 07:13:21 IST 2019
For [CNT, America/St_Johns] -> Sat Jun 08 02:43:21 IST 2019
For [PRT, America/Puerto_Rico] -> Sat Jun 08 01:13:21 IST 2019
For [PNT, America/Phoenix] -> Fri Jun 07 22:13:21 IST 2019
For [PLT, Asia/Karachi] -> Sat Jun 08 10:13:21 IST 2019
For [AST, America/Anchorage] -> Fri Jun 07 21:13:21 IST 2019
For [BST, Asia/Dhaka] -> Sat Jun 08 11:13:21 IST 2019
For [CST, America/Chicago] -> Sat Jun 08 00:13:21 IST 2019
For [EST, -05:00] -> Sat Jun 08 00:13:21 IST 2019
For [HST, -10:00] -> Fri Jun 07 19:13:21 IST 2019
For [JST, Asia/Tokyo] -> Sat Jun 08 14:13:21 IST 2019
For [IST, Asia/Kolkata] -> Sat Jun 08 10:43:21 IST 2019
For [AGT, America/Argentina/Buenos_Aires] -> Sat Jun 08 02:13:21 IST 2019
For [NST, Pacific/Auckland] -> Sat Jun 08 17:13:21 IST 2019
For [MST, -07:00] -> Fri Jun 07 22:13:21 IST 2019
For [AET, Australia/Sydney] -> Sat Jun 08 15:13:21 IST 2019
For [BET, America/Sao_Paulo] -> Sat Jun 08 02:13:21 IST 2019
For [PST, America/Los_Angeles] -> Fri Jun 07 22:13:21 IST 2019
For [ACT, Australia/Darwin] -> Sat Jun 08 14:43:21 IST 2019
For [SST, Pacific/Guadalcanal] -> Sat Jun 08 16:13:21 IST 2019
For [VST, Asia/Ho_Chi_Minh] -> Sat Jun 08 12:13:21 IST 2019
For [CAT, Africa/Harare] -> Sat Jun 08 07:13:21 IST 2019
For [ECT, Europe/Paris] -> Sat Jun 08 07:13:21 IST 2019
For [EAT, Africa/Addis_Ababa] -> Sat Jun 08 08:13:21 IST 2019
For [IET, America/Indiana/Indianapolis] -> Sat Jun 08 01:13:21 IST 2019
For [MIT, Pacific/Apia] -> Sat Jun 08 18:13:21 IST 2019
For [NET, Asia/Yerevan] -> Sat Jun 08 09:13:21 IST 2019


You may like


Previous                                                    Next                                                    Home

No comments:

Post a Comment