How to get local time of different time zones?

前端 未结 7 1185
青春惊慌失措
青春惊慌失措 2020-12-18 19:08

I want to get local time of different time zones using Java code. Based on the time zone passed to the function I need that time zone\'s local time. How to achieve this?

7条回答
  •  余生分开走
    2020-12-18 19:56

    I wrote the following program to get time for all the Timezones available, see if this helps...

    String[] zoneIds = TimeZone.getAvailableIDs();
        for (int i = 0; i < zoneIds.length; i++) {
        TimeZone tz = TimeZone.getTimeZone(zoneIds[i]);
        System.out.print(tz.getID() + " " + tz.getDisplayName());
            Calendar calTZ = new GregorianCalendar(tz);
            calTZ.setTimeInMillis(new Date().getTime());
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.YEAR, calTZ.get(Calendar.YEAR));
            cal.set(Calendar.MONTH, calTZ.get(Calendar.MONTH));
            cal.set(Calendar.DAY_OF_MONTH, calTZ.get(Calendar.DAY_OF_MONTH));
            cal.set(Calendar.HOUR_OF_DAY, calTZ.get(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, calTZ.get(Calendar.MINUTE));
            cal.set(Calendar.SECOND, calTZ.get(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, calTZ.get(Calendar.MILLISECOND));
        System.out.println( "  "+cal.getTime());
    

提交回复
热议问题