Wednesday, 10 August 2011

Convert date/time from specified time zone to local time

In order to convert date/time from specified time zone to local time, you can use following snippet.

	private Date toLocalTime(Date src, TimeZone zone) {
		Calendar srcCal = new GregorianCalendar();
		srcCal.setTime(src);
		Calendar gmtCal = new GregorianCalendar(zone);
		gmtCal.set(Calendar.YEAR, srcCal.get(Calendar.YEAR));
		gmtCal.set(Calendar.MONTH, srcCal.get(Calendar.MONTH));
		gmtCal.set(Calendar.DATE, srcCal.get(Calendar.DATE));
		gmtCal.set(Calendar.HOUR_OF_DAY, srcCal.get(Calendar.HOUR_OF_DAY));
		gmtCal.set(Calendar.MINUTE, srcCal.get(Calendar.MINUTE));
		gmtCal.set(Calendar.SECOND, srcCal.get(Calendar.SECOND));
		gmtCal.set(Calendar.MILLISECOND, srcCal.get(Calendar.MILLISECOND));
		Calendar tagCal = new GregorianCalendar();
		tagCal.setTime(gmtCal.getTime());
		return tagCal.getTime();
	}

No comments:

Post a Comment