Solved

Generating Current time...

Posted on 2001-06-08
10
163 Views
Last Modified: 2010-03-31
Dear expert...

can any of you provide me with the code how to generate current time in java...i need it urgently..thank !

from : Alex Gan
0
Comment
Question by:kgsoft_sys
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 5

Expert Comment

by:Calron
ID: 6166994
try

System.currentTimeMillis();

which returns the current system time in Milliseconds.
0
 

Author Comment

by:kgsoft_sys
ID: 6167050
sorry..i want the time to display in hour:minute:second..TQ
0
 
LVL 3

Expert Comment

by:boycey19
ID: 6167244
Use the java.util.Date class

Date d = new Date();
String strDate = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
0
 
LVL 5

Expert Comment

by:Calron
ID: 6167337
The methods getHours() etc. are deprecated in JDK 1.3 you should use the Calendar class instead:

Calendar rightNow = Calendar.getInstance();
int hours = rightNow.get(Calendar.HOUR);
int mins = rightNow.get(Calendar.MINUTE);
int secs = rightNow.get(Calendar.SECOND);
0
 
LVL 35

Expert Comment

by:TimYates
ID: 6167453
Or:

  public String dateToString()
  {
    SimpleDateFormat df = new SimpleDateFormat( "dd/MM/yy HH:mm:ss" ) ;
    java.util.Date d = new java.util.Date() ;
    try
    {
      return df.format( d ) ;
    }
    catch( Exception e )
    {
      return "oops" ;
    }
  }

Tim.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:mmuruganandam
ID: 6167816
Hai kgsoft_sys,

As Mr.TimYates said,


 public String dateToString()
 {
   SimpleDateFormat df = new SimpleDateFormat( "dd/MM/yy HH:mm:ss" ) ;
   java.util.Date d = new java.util.Date() ;

// instead of the above line
// because it is deprecated.
// use the below line

   java.sql.Date d = new java.sql.Date(System.currentTimeMillis());

   try
   {
     return df.format( d ) ;
   }
   catch( Exception e )
   {
     return "oops" ;
   }
 }
0
 
LVL 35

Accepted Solution

by:
TimYates earned 18 total points
ID: 6167950
Doh! ;-)
0
 
LVL 92

Expert Comment

by:objects
ID: 6169835

>  java.util.Date d = new java.util.Date() ;

> // instead of the above line
> // because it is deprecated.

'deprecated' in which JDK???
Unless it's been deprecated in 1.4, I think you'll find the empty ctor is fine.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 6176640
hooray!! (I was beginning to doubt myself ;-) )
0
 
LVL 92

Expert Comment

by:objects
ID: 6179413
Also grab for your trusty javadoc :)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
countX 22 70
nestparen challenge 4 56
pairs challenge 5 44
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now