This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting the Correct Time" Watch "Getting the Correct Time" New topic

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?
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);

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

Read more at:

Cindy Glass
"The Hood"

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);
Yielded: 15:12:27
Marilyn de Queiroz

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

"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:
subject: Getting the Correct Time
It's not a secret anymore!