Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Generating Current time...

Posted on 2001-06-08
10
Medium Priority
?
173 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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!

 
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
 
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 36 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

715 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