Wednesday, June 8, 2011

GMT/IST convertor in java


How to Convert GMT time into IST time using java

 

package com.vels.test;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;


/**
 * @author velmurugan pousel
 * created on Jun 8, 2011 for GMT Convertor
 */


public class GMTConvertor {

 public static void main(String args[]) {

  Calendar cal = Calendar.getInstance();
  cal.set(2011, 06, 8, 12, 0);//year,month,day,hour,minute format


  System.out.println(" IST time" + convertIST(cal.getTime()));


  System.out.println("current GMT time" + convertGMT(new Date()));

 }

 /**
  *  method to covert IST time to GMT
  * @param date is in IST format
  * @return
  */
 public static String convertGMT(Date date) {


 DateFormat gmtFormat = new SimpleDateFormat();
   TimeZone gmtTime = TimeZone.getTimeZone("GMT");
   gmtFormat.setTimeZone(gmtTime);

   return gmtFormat.format(date);

 }

 /**
  * method to convert GMT to IST time
  * @param date is GMT format
  * @return
  */
 public static String convertIST(Date date) {

   DateFormat istFormat = new SimpleDateFormat();
   TimeZone istTime = TimeZone.getTimeZone("Asia/Kolkata");
   istFormat.setTimeZone(istTime);

   return istFormat.format(date);

 }

}
 
 

No comments:

Post a Comment