Home arrow static arrow Java Programming [Archive] - Question about SimpleDateFormat
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Question about SimpleDateFormat
This topic has 2 replies on 1 page.

Posts:2
Registered: 7/6/04
Question about SimpleDateFormat  
Jul 6, 2004 11:24 PM



 
Hi,

I have a period of time in milliseconds that I would like to display in hh:mm:ss:SSS format.

For some reason when I run the code below it always displays wrong. It should print out "00:01:00:000" but it always prints out "10:01:00:000". Could anyone tell me why it adds 10 hours to my value of 60000 milliseconds?

public void testDateFormat()
{
try
{
long diff = 60000;
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss:SSS");
System.out.println("Date: " + formatter.format(new Date(diff)));
}
catch (Exception e)
{
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}

Regards,

Lachlan James
 

Posts:18,384
Registered: 21.03.00
Re: Question about SimpleDateFormat  
Jul 6, 2004 11:29 PM (reply 1 of 2)



 
Have you checked your timezone?

/Kaj
 

Posts:2
Registered: 7/6/04
Re: Question about SimpleDateFormat  
Jul 6, 2004 11:31 PM (reply 2 of 2)



 
Don't worry I found another way...

long diff = 60000;
millisecondsToString(diff);

public static String millisecondsToString(long time)
{
int milliseconds = (int) (time % 1000);
int seconds = (int) ( (time / 1000) % 60);
int minutes = (int) ( (time / 60000) % 60);
int hours = (int) ( (time / 3600000) % 24);
String millisecondsStr = (milliseconds < 10 ? "00" : (milliseconds < 100 ? "0" : "")) + milliseconds;
String secondsStr = (seconds < 10 ? "0" : "") + seconds;
String minutesStr = (minutes < 10 ? "0" : "") + minutes;
String hoursStr = (hours < 10 ? "0" : "") + hours;
return new String(hoursStr + ":" + minutesStr + ":" + secondsStr + "." + millisecondsStr);
}
 
This topic has 2 replies on 1 page.