aspose file tools*
The moose likes Beginning Java and the fly likes displaying time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "displaying time" Watch "displaying time" New topic
Author

displaying time

Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
hi ranchers,

i would like to manipulate the date and time in java. Im currently studying how to create an alarm clock windowed application. Can you give me any leads or info on how to go about this.

thank you all.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Im currently studying how to create an alarm clock windowed application.

a Swing application?

if so, add a JLabel to display the clock
add a timer to change the label's text
the label's text is a SimpleDateFormat() of 'new Date()'
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
how about the time.. is there a built in class that i can use. is it the java.sql.time ??

thanks
vijaya bacina
Ranch Hand

Joined: Aug 23, 2005
Posts: 155
java.uti.Date() will hold both date and time
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
how will i display the time on a JLabel for example, i mean every second. like a clock. when i was using vb there was a timer control, and it had this timer event. any help.. thanks guys for being prompt.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14342
    
  22

Browse through the Java API documentation and see if you can find anything. Hava a look at the class java.util.Timer, for example. Other useful classes might be java.util.Calendar and java.text.SimpleDateFormat.

Google also is very useful, search for "java timer" or "java clock" or something similar.
[ October 31, 2006: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
MV Krishna
Greenhorn

Joined: Sep 23, 2006
Posts: 5
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
Krishna and Jesper,

thanks for the comments and codes.. well appreciated..

im also visiting www.javaalmanac.com for references..
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
hi guys,

im starting to create my alarm clock object, hope you can check what im doing. just asking for your opinion.

This is what i have so far:

Alarm_Clock Object
Properties
- Hour, Minute, Second, Month, Day, Year
Methods
- get and set methods for the properties above
- getTime (returns hh:mm:ss)
- getDate (returns mm/dd/yy)
- setAlarm (sets the date, time and message to be shown)
- setAlarmSound (mp3 sound, ^^ )

Am i correct?

Now i was thinking of using a Calendar object to represent the time and date of the alarm clock instead, i dont know if this is correct. Since it has some properties and methods that deals the same thing.

Thanks
[ October 31, 2006: Message edited by: Cyrus Serrano ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14342
    
  22

Your ideas for your alarm clock class sound OK.

I wouldn't call it "Alarm_Clock" with an underscore in it; just "AlarmClock". You can call it whatever you want ofcourse, but there is a defacto coding standard / style that almost every Java programmer uses, in which class names are written in CamelCase, just like all the classes and interfaces in the standard Java API. See: Code Conventions for the Java Programming Language.

Class Calendar and class Date are both useful for storing dates and times; you could use either. Class Calendar contains a number of useful methods for doing date arithmetic.

Instead of the AlarmClock class having separate properties for hour, minute, second etc. you could also just give it one Date property:
Cyrus Serrano
Ranch Hand

Joined: Sep 29, 2003
Posts: 137
this is what i have in mind so far:

public class AlarmClock extends JPanel{

private Calendar currentSetting;


public AlarmClock()
{
currentSetting = Calendar.getInstance();
}

//Sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.
public void setDateAndTime(int yr,int mn,int dom, int hod, int min)
{
currentSetting.set(yr, mn, dom, hod, min);
}

}


---

I've used the Calendar class instead of the Date class since some of the methods of the date class are deprecated and requires the use of the Calendar class instead. Any comments guys.

@Jesper, thanks for the naming convention.. noted.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: displaying time