Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java Time Zone Example 
 

In this section we have written a java program which will take two Time Zone IDs as its argument and then convert the time between provided time zone IDs.

 

Java Time Zone Example

                         

Converting time between time zones

In this section we have written a java program which will take two Time Zone IDs as its argument and then convert the time between provided time zone IDs.

In our program we have firstly created a new date object and then after we have created two time zone class's objects with according to the provided time zone IDs.

TimeZone firstTime = TimeZone.getTimeZone(args[0]);   
TimeZone secondTime = TimeZone.getTimeZone(args[1]);

Above lines of code creates two objects of TimeZone as firstTime and secondTime. We have called getTimeZone() method to create these objects.

Here is the full example code of ConvertTimeZone.java as follows:

ConvertTimeZone.java

  ///////////////////////////////////
 // Convert time between time zone //
///////////////////////////////////

import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.TimeZone;
 
public class ConvertTimeZone {
    public static void main(String[] args) {
      Date date = new Date();
      DateFormat firstFormat = new SimpleDateFormat();
      DateFormat secondFormat = new SimpleDateFormat();
      TimeZone firstTime = TimeZone.getTimeZone(args[0]);
      TimeZone secondTime = TimeZone.getTimeZone(args[1]);
      firstFormat.setTimeZone(firstTime);
      secondFormat.setTimeZone(secondTime);
      System.out.println("-->"+args[0]+": " + firstFormat.format(date));
      System.out.println("-->"+args[1]+": " + secondFormat.format(date));
    }
}

Output:

Compile the ConvertTimeZone.java file and when executing them provide the two Time zone Ids from which to which time is to be converted. Here in above program we have provided two command line arguments as IST and GMT.

C:\DateExample>javac ConvertTimeZone.java

C:\DateExample>java ConvertTimeZone IST GMT
-->IST: 10/11/08 4:16 PM
-->GMT: 10/11/08 10:46 AM

C:\DateExample>

Download Source Code

                         

» View all related tutorials
Related Tags: c class orm form format char pattern using this character characters show for example cte program to ram exam ci

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.