Solved

Generating Current time...

Posted on 2001-06-08
10
166 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
oracle 11g 23 80
jdbc error in jsp application 20 45
eclipse buid path vs tomcat lib path 10 23
Java: How do I open the default windows program for a given file type 3 28
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

770 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