-
newbie clocl update problem
Hi,
I have just started learning java (only two days now!) and thought I'd try my hand at making a clock that will give the time in UK and in any other timezone you enter. I have got this (see below), which works ok to give the times from the command line, but I want it to keep automatically updating itself once it is started. Please can anyone tell me how to do that?
Cheers,
Munna
-----------------
//This is the code so far
import java.util.*;
import java.text.DateFormat;
import java.io.* ;
public class TellTime
{
public static void main(String[]arguments)
{
/*
//read in the timezone requested by the user
System.out.print( "These are the available time zones: " );
String[] ssIDs = TimeZone.getAvailableIDs();
for( int i=0; i<ssIDs.length; i++ )
System.out.print( ssIDs[i]+" ");
*/
BufferedReader keyboard;
InputStreamReader reader;
System.out.println("Type the required timezone, followed by enter: ");
String sInputText = "";
reader = new InputStreamReader(System.in);
keyboard = new BufferedReader(reader);
try {
sInputText = keyboard.readLine( );
System.out.println("You typed: " + sInputText);
}
catch (IOException e) {
System.out.println("There was an error");
}
//current time in the UK
Calendar ukcal = Calendar.getInstance();
Date dateuk = ukcal.getTime(); //Date object, but getTime method allocates only the time to it.
DateFormat ukdf = DateFormat.getTimeInstance(); //this is where we need the java.txt package, and ask for just the time formatter
String ukTime = ukdf.format(dateuk); //time formatter works its magic on the time in Date object called 'dateuk', and returns a string called 'ukTime'
//current time elsewhere
Calendar wcal = Calendar.getInstance();
Date datewrld = wcal.getTime();
//String myDate = DateFormat.getDateInstance(DateFormat.LONG).format(date);
TimeZone tz = TimeZone.getTimeZone(sInputText);
TimeZone.setDefault(tz);
DateFormat wdf = DateFormat.getTimeInstance();
String wTime = wdf.format(datewrld);
//display time
System.out.println("In England the time is: BST "+ ukTime);
// Print the time zone string.
System.out.println("In your requested time zone the time is: "+ sInputText + " "+ wTime);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks