Home arrow static arrow Java Programming [Archive] - clocks
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - clocks
This topic has 4 replies on 1 page.

Posts:21
Registered: 11/17/03
clocks  
Aug 2, 2004 1:48 PM



 
HI, i was wondering if anyone could help me, i am tring to make a clock program where it shows uk time, us time, and where u can add a time to the current uk time.

i was wondering if there ia any way in which the to USTime method could be programmed so i could just take away six hours from the uk time, thanks 4 ur help in advance

sami

import java.awt.*;
import java.util.*;
import java.text.SimpleDateFormat;
import java.applet.Applet;
import javax.swing.*;
import java.awt.event.*;

public class Clock extends JFrame implements Runnable {

// variable declarations
private Thread clockThread = null;
private boolean running;
private SimpleDateFormat timeFormatter;
private JTextField addTimeTextField;
private String clockText;
private JLabel UKLabel, CurrentTime, USLabel,toUSTime ,ResultLabel, AddLabel;

public Clock() {
Container c = getContentPane();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints =
new GridBagConstraints();

c.setLayout( gridbag);
constraints.fill = GridBagConstraints.HORIZONTAL;


running = false;
// create a formatter object which extracts
// only time information from a Date object
timeFormatter = new SimpleDateFormat("HH':'mm':'ss");

UKLabel = new JLabel("UK Time");
constraints.gridx=0;
constraints.gridy=0;
constraints.weightx=1.0;
// constraints.gridwidth = 3;
gridbag.setConstraints(UKLabel,constraints);
c.add( UKLabel );

clockText = timeFormatter.format(new Date());

CurrentTime = new JLabel(clockText);
constraints.gridx=1;
constraints.gridy=0;
constraints.weightx=1.0;
// constraints.gridwidth = 3;
gridbag.setConstraints(CurrentTime,constraints);
c.add( CurrentTime );

USLabel = new JLabel("US Time");
constraints.gridx=0;
constraints.gridy=1;
constraints.weightx=1.0;
// constraints.gridwidth = 3;
gridbag.setConstraints(USLabel,constraints);
c.add( USLabel );

toUSTime = new JLabel(clockText);
constraints.gridx=1;
constraints.gridy=1;
constraints.weightx=1.0;
// constraints.gridwidth = 3;
gridbag.setConstraints(toUSTime,constraints);
c.add( toUSTime);

AddLabel = new JLabel("Add Time");
constraints.gridx=0;
constraints.gridy=2;
constraints.weightx=1.0;
// constraints.gridwidth = 3;
gridbag.setConstraints(AddLabel,constraints);
c.add( AddLabel );

addTimeTextField = new JTextField(9);
constraints.gridx=1;
constraints.gridy=2;
constraints.weightx=1.0;
// constraints.gridwidth = 3;
gridbag.setConstraints(addTimeTextField,constraints);
c.add( addTimeTextField );

ResultLabel = new JLabel("Any Time");
constraints.gridx=0;
constraints.gridy=3;
constraints.weightx=1.0;
// constraints.gridwidth = 3;
gridbag.setConstraints(ResultLabel,constraints);
c.add( ResultLabel );

setSize( 300, 150 );
show();
start();
}
public void toUSTime(int CurrentTime){

}

public void start() {
// make a new thread object
if (clockThread == null) {
// pass it this object and a name
clockThread = new Thread(this, "Clock");
// start the thread
clockThread.start();
running = true;
}
}

public void run() {
// this method is called when the Thread manager
// allows this thread some computing time
while (running) {

CurrentTime.setText( timeFormatter.format(new Date()));
CurrentTime.repaint();

toUSTime.setText( timeFormatter.format(new Date()));
toUSTime.repaint();

try {
Thread.sleep(1000);
}
catch (InterruptedException e){ }
}
}

//public void paint(Graphics g) {
// get a new time and format it into a text string
//String clockText = timeFormatter.format(new Date());
// and print it
//g.drawString(clockText,30,69);

//}

public void stop() {
running = false;
clockThread = null;
}


public static void main( String args[] )
{
Clock app = new Clock();

app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}}

 

Posts:13,769
Registered: 00-11-29
Re: clocks  
Aug 2, 2004 1:56 PM (reply 1 of 4)



 
The best way to do this would be to use DateFormat objects. Create one for UK, enter the time, take create a Date from that, then pass that Date to another DateFormat with the US time (I assume you want central time. Once you've got that done you can easily convert between any two timezones.
 

Posts:21
Registered: 11/17/03
Re: clocks  
Aug 2, 2004 2:52 PM (reply 2 of 4)



 
Could you please give me some pointers on how to create dateformat objects, thanks for your reply. isnt there a method where i could just do a sum and just add six hours using toUSTime(int currentTime) as a method? without using timezones?

thnaks

 

Posts:13,769
Registered: 00-11-29
Re: clocks  
Aug 2, 2004 3:01 PM (reply 3 of 4)



 
Could you please give me some pointers on how to
create dateformat objects, thanks for your reply. isnt
there a method where i could just do a sum and just
add six hours using toUSTime(int currentTime) as a
method? without using timezones?

thnaks

Sure. But why go through the effort to get something that is less useful?

Use the Calendar.add() method for that kind of thing.

Otherwise look at the SimpleDateFormat class.
 

Posts:37,103
Registered: 3/30/99
Re: clocks  
Aug 2, 2004 3:06 PM (reply 4 of 4)



 
1) Please don't crosspost. It cuts down on the effectiveness of responses, leads to people wasting their time answering what others have already answered, makes for difficult discussion, and is generally just annoying and bad form.

http://forum.java.sun.com/thread.jsp?thread=543358&forum=54
http://forum.java.sun.com/thread.jsp?thread=543341&forum=31
http://forum.java.sun.com/thread.jsp?thread=543340&forum=31

2) When you post code, please use [code] and [/code] tags as described in [url=http://forum.java.sun.com/features.jsp#Formatting]Formatting Help[/url] on the message entry page. It makes it much easier to read.
 
This topic has 4 replies on 1 page.