Samples JDK
Sample.java
1 package com.freemindcafe.timezone.sample3;
2 
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
6 import java.util.Date;
7 import java.util.TimeZone;
8 
9 import junit.framework.Assert;
10 
11 import org.junit.Test;
12 
13 public class Sample {
14 
15  @Test
16  public void test1(){
17  TimeZone tz = TimeZone.getTimeZone("Asia/Calcutta");
18  Assert.assertEquals("India Standard Time",tz.getDisplayName());
19  Calendar cal = Calendar.getInstance(tz);
20  System.out.printf("%02d:%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
21  }
22 
23  @Test
24  public void test2(){
25  TimeZone tz = TimeZone.getTimeZone("IST");
26  Assert.assertEquals("India Standard Time",tz.getDisplayName());
27  Calendar cal = Calendar.getInstance(tz);
28  System.out.printf("%02d:%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
29  }
30 
31  @Test
32  public void test3(){
33  TimeZone tz = TimeZone.getTimeZone("Etc/GMT+5:30"); //This is wrong format hence falls back to GMT. Correct format is Etc/GMT(+-)n
34  Assert.assertEquals("Greenwich Mean Time",tz.getDisplayName());
35  Calendar cal = Calendar.getInstance(tz);
36  System.out.printf("%02d:%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
37  }
38 
39  @Test
40  public void test4(){
41  TimeZone tz = TimeZone.getTimeZone("Etc/GMT+5");
42  Assert.assertEquals("GMT-05:00",tz.getDisplayName());
43  Calendar cal = Calendar.getInstance(tz);
44  System.out.printf("%02d:%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
45  }
46 
47  @Test
48  public void test5(){
49  TimeZone tz = TimeZone.getTimeZone("Etc/GMT-5"); //Gets converted into GMT+5
50  Assert.assertEquals("GMT+05:00",tz.getDisplayName());
51  Calendar cal = Calendar.getInstance(tz);
52  System.out.printf("%02d:%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
53  }
54 
55  @Test
56  public void test6(){
57  TimeZone tz = TimeZone.getTimeZone("GMT+5:30"); //This is again IST
58  Assert.assertEquals("GMT+05:30",tz.getDisplayName());
59  Calendar cal = Calendar.getInstance(tz);
60  System.out.printf("%02d:%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
61  }
62 
63  //There was an bug. IST was pointing to Isreal Standard Time. Seems that have been fixed. SimpleDateFomrat is giving the correct date time string now.
64  @Test
65  public void test7() throws Exception{
66 
67  DateFormat fmt1 = new SimpleDateFormat("M/d/yy h:mm a Z");
68  Date date = fmt1.parse("6/29/2012 5:15 PM IST");
69  System.out.println(date);
70 
71  DateFormat fmt2 = new SimpleDateFormat("M/d/yy h:mm a");
72  fmt2.setTimeZone(TimeZone.getTimeZone("IST"));
73  System.out.println(fmt2.parse("6/29/2012 5:15 PM"));
74 
75  DateFormat fmt3 = new SimpleDateFormat("M/d/yy h:mm a");
76  fmt3.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
77  System.out.println(fmt3.parse("6/29/2012 5:15 PM"));
78 
79  }
80 
81 }