Solved

Generating Current time...

Posted on 2001-06-08
10
168 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

738 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