Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

current time

Status
Not open for further replies.

AndBack3

Programmer
Oct 27, 2001
15
0
0
US
I need a way of accessing the current time, and have no idea how. I was thinking of something along the lines of:
int hour
int minute
int second
int millisecond (maybe float second instead)?

I would then set each of these values to the current value (based on the system clock) and work with them. If there's an easier way that deals with one integer (ie:

long milliseconds;

that would work as well, then I could convert it. Thanks
 
Have a look at the Java API documentation (either on the java sun website or where ever you downloaded it to) and have a look at
Code:
java.util.Calendar
it will provide what you are after. It provides methods to return YEAR, MONTH, DAY, HOUR, MINTUES, SECOND, MILLISECOND.
or
Code:
static long currentTimeMillis()
which is in
Code:
java.lang.System



[rant]
This is no directed so much at you AndBack3, but to everyone that asks simple question like "How do I do..." and the answer is right there in front of them.

** READ THE JAVA API DOCUMENTATION **

It is comprehensive, useful, and 8 times out of 10 will solve the problem. When it can't, or you dont understand it, then come on here and ask away, and we will answer.

Please!
[/rant]


The Java API Docs are found at -------------------------------------------
There are no onions, only magic
-------------------------------------------
 
I agree w/your comments jfryer..however...here you go AndBack3 ..a code sample to get you started...

There are two ways go can do that you want:

1.

import java.util.*;

public class GiveMeTheTime
{
public static void main(String[] args) throws Exception
{

java.text.SimpleDateFormat sd = new java.text.SimpleDateFormat("hh:mm:ss");
String time = "The time is " +sd.format(new Date());

System.out.println(time);
}
}

or

2. - Please note that I just cut and pasted this from the Java Doc - Class GregorianCalendar:


import java.util.*;

public class GiveMeTheTime
{
public static void main(String[] args) throws Exception
{

Calendar calendar = new GregorianCalendar();
Date trialTime = new Date();
calendar.setTime(trialTime);

// print out a bunch of interesting things


System.out.println("ERA: " + calendar.get(Calendar.ERA));
System.out.println("YEAR: " + calendar.get(Calendar.YEAR));
System.out.println("MONTH: " + calendar.get(Calendar.MONTH));
System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH));
System.out.println("DATE: " + calendar.get(Calendar.DATE));
System.out.println("DAY_OF_MONTH: " + calendar.get(Calendar.DAY_OF_MONTH));
System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR));
System.out.println("DAY_OF_WEEK: " + calendar.get(Calendar.DAY_OF_WEEK));
System.out.println("DAY_OF_WEEK_IN_MONTH: "
+ calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH));
System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM));
System.out.println("HOUR: " + calendar.get(Calendar.HOUR));
System.out.println("HOUR_OF_DAY: " + calendar.get(Calendar.HOUR_OF_DAY));
System.out.println("MINUTE: " + calendar.get(Calendar.MINUTE));
System.out.println("SECOND: " + calendar.get(Calendar.SECOND));
System.out.println("MILLISECOND: " + calendar.get(Calendar.MILLISECOND));
System.out.println("ZONE_OFFSET: "
+ (calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000)));
System.out.println("DST_OFFSET: "
+ (calendar.get(Calendar.DST_OFFSET)/(60*60*1000)));


}
}
 
thx for the help...I understand that it is preferable to check the api first, but I'm kinda new and some parts are still a bit over my head...it's also sometimes hard to find the right one to check.
 
another problem...if I use this to display a clock, it needs to refresh often. The only way I know to refresh it is to repaint, but then the entire screen goes white and there it flashes every time it refreshes. Since I can't control exactly when it refreshes, it does it multiple times, leaving an unfavorable display. How can I just change the number, not repaint the whole thing? Also, can I set it up so it will change only when the second changes (or in that case, update every minute and not worry about seconds)?

Thanks-
AB3
 
Hope this will help u



import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.io.*;



class TimeAndDateFormatter {
String formattedDate;
String daysOfWeek[]= { "Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday" };
String monthsOfYear[] = { "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December" };


TimeAndDateFormatter( Date date, String title ) {
int h = date.getHours();
int m = date.getMinutes();
int s = date.getSeconds();
int dy = date.getDay();
int dt = date.getDate();
int mn = date.getMonth();
int yr = date.getYear();
String hours, minutes, seconds, am_pm;
String day, numericalDate, month, year;


if( h == 0 ) {
hours = new String( "12" );
am_pm = new String( "am" );


} else if( h > 12 ) {
hours = new String( "" + (h-12) );
am_pm = new String( "pm" );


} else if( h == 12 ) {
hours = new String( "12" );
am_pm = new String( "pm" );


} else {
hours = new String( "" + h );
am_pm = new String( "am" );
}


if( m < 10 ) {
minutes = new String( &quot;0&quot; + m );


} else {
minutes = new String( &quot;&quot; + m );
}


if( s < 10 ) {
seconds = new String( &quot;0&quot; + s );


} else {
seconds = new String( &quot;&quot; + s );
}
day = daysOfWeek[ dy ];
numericalDate = new String( &quot;&quot; + dt );
month = monthsOfYear[ mn ];
year = new String( &quot;&quot; + (1900 + yr) );
formattedDate = new String( day + &quot;, &quot; +
month + &quot; &quot; +
numericalDate + &quot;, &quot; +
year + &quot; &quot; + title + &quot; &quot; +
hours+ &quot;:&quot; +
minutes + &quot;:&quot; +
seconds + &quot; &quot; + am_pm
);
}


public String toString() {
return formattedDate;
}
}
public class Time extends java.applet.Applet implements Runnable


{
private Thread clock;
private String font_name, title, datestring; // name of the font
private int height, width, font_size; // font size (points)
private Font wordFont;
private FontMetrics wordMetrics;
private int textcolor, backcolor, bordercolor;
private int stringx, stringy;
private int delay, s_width;
TimeAndDateFormatter formattedTimeAndDate;
public void init()


{
String temp;
height = size().height; //set the size
width = size().width;
// get the name and size of the font
temp = getParameter(&quot;delay&quot;);
delay= (temp==null) ? 5000 : Integer.parseInt( temp );
temp=getParameter(&quot;title&quot;);
title = (temp==null) ? &quot; &quot; : temp;
temp=getParameter(&quot;font&quot;);
font_name= (temp==null) ? &quot;TimesRoman&quot; : temp;
temp = getParameter(&quot;fontsize&quot;);
font_size= (temp==null) ? 12 : Integer.parseInt( temp );
// initialise the font
wordFont = new Font(font_name, Font.BOLD, font_size);
if (wordFont == null)
wordFont = getFont();
wordMetrics = getFontMetrics (wordFont);
temp = getParameter(&quot;textcolor&quot;);
textcolor = (temp==null) ? 0x000000 : Integer.parseInt(temp, 16);
temp = getParameter(&quot;backcolor&quot;);
backcolor= (temp==null) ? 0xffffff : Integer.parseInt(temp, 16);
temp = getParameter(&quot;bordercolor&quot;);
bordercolor = (temp==null) ? backcolor : Integer.parseInt(temp, 16);
}
public void start()


{
clock = new Thread(this); // start the thread
clock.start();
}

public void stop()


{
clock.stop();
}
public void run()


{
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
while(true)


{
Date date = new Date();
formattedTimeAndDate =
new TimeAndDateFormatter( date, title );
datestring = formattedTimeAndDate.toString();

// System.out.println( &quot;Current Time and
// Date: &quot; + formattedTimeAndDate );
repaint();


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


{}
}
}
public void update (Graphics g)


{
paint(g);
}
public synchronized void paint(Graphics g)


{
g.setFont(wordFont); // set the font in the graphics context
g.setColor(new Color(backcolor)); // fill area with the background color
g.fillRect(0,0,width,height);
g.setColor(new Color(textcolor));
s_width = wordMetrics.stringWidth(datestring);
stringy = (height - (wordMetrics.getHeight()-wordMetrics.getLeading()))/2 + (wordMetrics.getHeight()-wordMetrics.getLeading()-wordMetrics.getDescent());
stringx = (width - s_width)/2;
g.drawString (datestring, stringx, stringy);
g.setColor(new Color(bordercolor));
g.drawRect(0, 0, width-1, height-1);
}


public void paintApplet(Graphics g)


{
}
}
 
AndBack3 if u want to have second to second changes make

delay= (temp==null) ? 5000 : Integer.parseInt( temp );
as

delay= (temp==null) ? 1000 : Integer.parseInt( temp );


It will work perfectly as system clock

any questions let me know
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top