• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting the Correct Time

 
Robin Richardson
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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_
 
Cindy Glass
"The Hood"
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calendar uses the date from your computer's OS. Does your computer show the correct time?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic