aspose file tools*
The moose likes Beginning Java and the fly likes Getting the Correct Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting the Correct Time" Watch "Getting the Correct Time" New topic
Author

Getting the Correct Time

Robin Richardson
Ranch Hand

Joined: Mar 01, 2001
Posts: 37
What is the best way to get a program to display the correct time in the output? I am using java.util.Calendar, but at 12:24:40 it is displaying 0244. How do I correct this, or what is a better way to show the time?
Thanks
Robin
Shama Khan
Ranch Hand

Joined: Aug 14, 2000
Posts: 185
Not a direct answer but ...

SimpleDateFormat formatter
= new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz");
Date currentTime_1 = new Date();
String dateString = formatter.format(currentTime_1);

// Parse the previous string back into a Date.
ParsePosition pos = new ParsePosition(0);
Date currentTime_2 = formatter.parse(dateString, pos);
System.out.println(currentTime_2);

Yielded: Tue Oct 09 14:46:18 EDT 2001

Read more at: http://java.sun.com/products/jdk/1.1/docs/api/java.text.SimpleDateFormat.html#_top_

Shama
Cindy Glass
"The Hood"
Greenhorn

Joined: Oct 09, 2001
Posts: 4
More Direct:
// import java.text.*;java.util.*;
Date today;
String output;
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("H:mm:ss");
today = new Date();
output = formatter.format(today);
System.out.println(output);
Yielded: 15:12:27
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
Calendar uses the date from your computer's OS. Does your computer show the correct time?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the Correct Time