Convert Time to Any TimeZone

For converting one timezone to another, I have used the following code -
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TimeZoneMain {
 public static void main(String args[]) throws ParseException {
     
  DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z");   
  Date date = new Date();
  String dateString = "14 Jul 2014 00:11:04 CEST";
  date = formatter.parse(dateString);
  System.out.println(formatter.format(date));
  
  // Set the formatter to use a different timezone  - Indian Standard Time
  formatter.setTimeZone(TimeZone.getTimeZone("IST"));  
  System.out.println("IST time : "+formatter.format(date));
  
  
  // Set the formatter to use a different timezone - Indochina Time
  formatter.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
  System.out.println("ICT time : "+formatter.format(date));
 }
}











But, if you don't want to use String as input date, you can create object of Calender Class, assign value in it & then use it.

Happy Learning!And, don't forget to share!


No comments :

Post a Comment