Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

Generating Current time...

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
kgsoft_sys
Asked:
kgsoft_sys
  • 3
  • 2
  • 2
  • +3
1 Solution
 
CalronCommented:
try

System.currentTimeMillis();

which returns the current system time in Milliseconds.
0
 
kgsoft_sysAuthor Commented:
sorry..i want the time to display in hour:minute:second..TQ
0
 
boycey19Commented:
Use the java.util.Date class

Date d = new Date();
String strDate = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CalronCommented:
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
 
TimYatesCommented:
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
 
mmuruganandamCommented:
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
 
TimYatesCommented:
Doh! ;-)
0
 
objectsCommented:

>  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
 
TimYatesCommented:
hooray!! (I was beginning to doubt myself ;-) )
0
 
objectsCommented:
Also grab for your trusty javadoc :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now