English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi everybody,

I am new to Java, and wanted to add the CurrentTime into a small program...

I imported:
import java.util.Date;


public static void main(String[] args)
{
System.out.println("Current Time is: " + currentTime());

}

private static long currentTime()
{
Date date = new Date();
return date.getTime();

}



I tend to get a really long number...

Can anyone help me with that?

Thank you in advance

2007-02-21 09:02:09 · 1 answers · asked by ...Sky the Limits... 5 in Computers & Internet Programming & Design

Amit:
Thanks a lot for the code...but the hours don't tend to work right - I get a wrong value
The minutes and seconds work perfectly...

This is the code I added:
System.out.println("Current Time is: " + (currentTime() % 86400000) / 3600000+ ":" + (currentTime() % 3600000) / 60000 + ":" + (currentTime() % 60000) / 1000);

2007-02-21 12:50:02 · update #1

It's working perfectly now...
I think the error was in the program I'm using or something...

Thanks a lot

2007-02-21 15:08:38 · update #2

1 answers

The long number is the numbers of millisecs since Jan 1, 1970 12:00AM GMT.
To caculate the hour: (currentTime % 86400000) / 3600000.
// 86,400 is the number of hours in a day.

To Calculate minutes: (currentTime % 3600000) / 60000;
// 3600 seconds in an hour.

seconds: (currentTime % 60000) / 1000;

The remaining millisecs currentTime % 1000;

2007-02-21 09:22:35 · answer #1 · answered by Amit Y 5 · 0 0

fedest.com, questions and answers