Get Current Date and Time across TimeZone in Java - Java @ Desk

Wednesday, March 26, 2014

Get Current Date and Time across TimeZone in Java

Get Current Date and Time across TimeZone in Java

Current Date and Time in Java can be retrieved using Calendar class and the date returned can be formatted using DateFormat class.

The date returned using the DateFormat class belongs to TimeZone in which the application is running.

In all there are 604 Timezones across the world. Below are few of them :

Pacific/Samoa
America/New_York
Canada/Eastern
Asia/Kolkata

To get the time across all timezones use the below code

Calendar calendar = Calendar.getInstance();
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println("Get the Time across all TimeZones");
Iterator<String> list = Arrays.asList(TimeZone.getAvailableIDs()).iterator();

while (list.hasNext()) {
 dateFormat.setTimeZone(TimeZone.getTimeZone(list.next()));
 System.out.println("Current Date - "
  + dateFormat.format(calendar.getTime()) + " in TimeZone - "
  + dateFormat.getTimeZone().getID());
}


To get the date and time in local timezone - In this case, no need to set any TimeZone in DateFormat object

dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
System.out.println("\n\n\nGet the Time for Local TimeZone");
System.out.println("Current Date - "
 + dateFormat.format(calendar.getTime()) + " in TimeZone - "
 + dateFormat.getTimeZone().getID());


To get the time in a particular timezone - America/New_York

dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
System.out.println("Current Date - "
 + dateFormat.format(calendar.getTime()) + " in TimeZone - "
 + dateFormat.getTimeZone().getID());


Output : It includes first few timezones
Current Date - 26-03-2014 02:19:57 in TimeZone - Etc/GMT+12
Current Date - 26-03-2014 03:19:57 in TimeZone - Etc/GMT+11
Current Date - 26-03-2014 03:19:57 in TimeZone - MIT
Current Date - 26-03-2014 03:19:57 in TimeZone - Pacific/Apia
Current Date - 26-03-2014 03:19:57 in TimeZone - Pacific/Midway
Current Date - 26-03-2014 03:19:57 in TimeZone - Pacific/Niue
Current Date - 26-03-2014 03:19:57 in TimeZone - Pacific/Pago_Pago
Current Date - 26-03-2014 03:19:57 in TimeZone - Pacific/Samoa
Current Date - 26-03-2014 03:19:57 in TimeZone - US/Samoa



Complete Client File

package test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;

public class GetCurrentDateAndTime {

 public static void main(String args[]) {
  Calendar calendar = Calendar.getInstance();
  DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

  System.out.println("Get the Time across all TimeZones");
  Iterator<String> list = Arrays.asList(TimeZone.getAvailableIDs()).iterator();
  while (list.hasNext()) {
   dateFormat
     .setTimeZone(TimeZone.getTimeZone(list.next()));
   System.out.println("Current Date - "
     + dateFormat.format(calendar.getTime()) + " in TimeZone - "
     + dateFormat.getTimeZone().getID());
  }

  dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  System.out.println("\n\n\nGet the Time for Local TimeZone");
  System.out.println("Current Date - "
    + dateFormat.format(calendar.getTime()) + " in TimeZone - "
    + dateFormat.getTimeZone().getID());
  
  System.out.println("\n\n\nGet the Time for New York Time Zone");
  dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
  System.out.println("Current Date - "
    + dateFormat.format(calendar.getTime()) + " in TimeZone - "
    + dateFormat.getTimeZone().getID());

 }
}






No comments:

Post a Comment