newbie clocl update problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: newbie clocl update problem

  1. #1
    Join Date
    Aug 2003
    Posts
    4

    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);

    }
    }

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    This is hardly the sort of program to start off with, but hey!

    There are different ways of doing this, some better than others, but here's one way:
    Code:
    import java.util.*;
    import java.text.DateFormat;
    import java.io.* ;
    
    
    public class TellTime {
    
    	public static void main(String[]arguments) {
    		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) {
    	
    		}
    		
    		for (int tick = 0; tick < 20; tick++) {
    			//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);
    			try {
    				Thread.sleep(1000);
    			}
    			catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    This is quite an inefficient way of doing it, but it's reasonably easy to see what's happening - you put the time calculation loop which will repeat 20 times. Each time the time is display, the program (to be more precise, the program's "thread") goes to sleep for one second.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Aug 2003
    Posts
    4
    Hi there!
    Thanks for the suggestion. I only thought I'd try this because it would be useful when I'm about to call friends abroad! Do you have any other suggestions for a suitable beginners project? As I said -- I've only just started teaching myself java, and would really appreciate any hints -- I don't want to do the usual "make a list of 1st 400 prome numbers" kind of projects they suggest in Java learning books -- they seem so boring!
    --Munna

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    I know what you mean, but writing those sorts of programs are a necessary evil, especially if you haven't done any serious programming before.

    My suggestion to you is to get through any books you've got (http://www.mindview.net/Books/TIPatterns/) and then once you've done those, then do the more challenging problems:

    http://mindprod.com/projects.html
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Aug 2003
    Posts
    4

    Thumbs up

    Thanks! I'll check those out!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center