Home > Uncategorized > Date, TimeZone, Calendar – converter in Java

Date, TimeZone, Calendar – converter in Java

have enough wasted working with java.util.Time(Sun named it as java.util.Date), and SimpleDateFormat(UnSpecifiedTimeZoneAndByDefaultJVMTimeZone).

Whenever dealing with Date object remember that your working with time. If you need to pass different timezones of date then it is named as Calendar in java. So pass Calendar and not date. That is what I learn after struggling with it for hours.

<pre>
package com.nikias.calendar;

import java.util.Calendar;
import java.util.TimeZone;

/*
 * Helps to print current time in GMT, local JVM, Indian Time, Singapore Time, New-york Time
 *
 * America/New_York
 * Asia/Calcutta
 * Asia/Singapore
 * GMT
 *
 */
public class TimeZoneConverter {

 public TimeZoneConverter() {
 }

 public static void main(String[] args) {
 TimeZoneConverter converter = new TimeZoneConverter();
 String[] timeZones = new String[] { "Asia/Calcutta", "America/New_York", "Asia/Singapore", "GMT" };

 for (String tz : timeZones) {
 System.out.printf(tz + " --    %tc\n", converter.fromGMT(Calendar.getInstance(), tz));
 }
 }

 public Calendar fromGMT(Calendar sourceTime, String targetZone) {
 return fromGMT(sourceTime, TimeZone.getTimeZone(targetZone));
 }

 public Calendar fromGMT(Calendar sourceTime, TimeZone targetZone) {
 Calendar target = Calendar.getInstance(targetZone);
 target.setTime(sourceTime.getTime());
 return target;
 }
}

Below is the unit testcase to prove them

package com.nikias.calendar;

import java.util.Calendar;
import java.util.TimeZone;

import junit.framework.Assert;

import org.junit.Test;

public class TimeZoneConverterTest {

	// Sunday, January 31st, 8:00AM

	long day = 24 * 60 * 60 * 1000;
	long year = (long) day * 366;
	long time = (long) year * (2010 - 1970);
	long dsttime = (long) year * (2010 - 1970) + 93*day;
	long dsttime2 = (long) year * (2010 - 1970) + 90*day;

	@Test
	public void testSingaporeTime() {
		TimeZone tzSNG = TimeZone.getTimeZone("Asia/Singapore");
		TimeZone tzNY = TimeZone.getTimeZone("America/New_York");
		Calendar calendarSNG = Calendar.getInstance(tzSNG);
		calendarSNG.setTimeInMillis(time);
		Calendar calendarNY = Calendar.getInstance(tzNY);
		calendarNY.setTimeInMillis(time);

		Assert.assertEquals(calendarNY.get(Calendar.YEAR), calendarSNG.get(Calendar.YEAR));
		Assert.assertEquals(calendarNY.get(Calendar.MONTH), calendarSNG.get(Calendar.MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.DAY_OF_MONTH) + 1, calendarSNG.get(Calendar.DAY_OF_MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.HOUR_OF_DAY) , calendarSNG.get(Calendar.HOUR_OF_DAY)+ 11);
	}

	@Test
	public void testDSTAndSingaporeTime() {
		TimeZone tzSNG = TimeZone.getTimeZone("Asia/Singapore");
		TimeZone tzNY = TimeZone.getTimeZone("America/New_York");
		Calendar calendarSNG = Calendar.getInstance(tzSNG);
		calendarSNG.setTimeInMillis(dsttime);
		Calendar calendarNY = Calendar.getInstance(tzNY);
		calendarNY.setTimeInMillis(dsttime);

		Assert.assertEquals(calendarNY.get(Calendar.YEAR), calendarSNG.get(Calendar.YEAR));
		Assert.assertEquals(calendarNY.get(Calendar.MONTH), calendarSNG.get(Calendar.MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.DAY_OF_MONTH) + 1, calendarSNG.get(Calendar.DAY_OF_MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.HOUR_OF_DAY) , calendarSNG.get(Calendar.HOUR_OF_DAY)+ 12);
	}
	
	@Test
	public void testDSTAndSingaporeTimeAndEndOfMonth() {
		TimeZone tzSNG = TimeZone.getTimeZone("Asia/Singapore");
		TimeZone tzNY = TimeZone.getTimeZone("America/New_York");
		Calendar calendarSNG = Calendar.getInstance(tzSNG);
		calendarSNG.setTimeInMillis(dsttime2);
		Calendar calendarNY = Calendar.getInstance(tzNY);
		calendarNY.setTimeInMillis(dsttime2);

		Assert.assertEquals(calendarNY.get(Calendar.YEAR), calendarSNG.get(Calendar.YEAR));
		Assert.assertEquals(calendarNY.get(Calendar.MONTH) + 1, calendarSNG.get(Calendar.MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.DAY_OF_MONTH)-29, calendarSNG.get(Calendar.DAY_OF_MONTH));
		Assert.assertEquals(calendarNY.get(Calendar.HOUR_OF_DAY) , calendarSNG.get(Calendar.HOUR_OF_DAY)+ 12);
	}	

	
}
Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: