Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: SimpleDateFormat adds 1 to day and hour (Time span)

i am trying to output "900,000 milliseconds" in the format "days:hours:minutes:seconds" and I am using this code right now:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd:HH:mm:ss");
String formatted = simpleDateFormat.format(900000)

900,000 milliseconds should be 15 minutes, so I want it to return

00:00:15:00

or something like this...

But for any reason it returns

01:01:15:00

Can anybody tell me why and how to fix it?

I thought it had to do something with time zones, but it added 1 to the number of days as well...?

Thanks in advance!

like image 509
Abandoned account Avatar asked Oct 09 '12 13:10

Abandoned account


People also ask

Is SimpleDateFormat deprecated?

Class SimpleDateFormat. Deprecated. A class for parsing and formatting dates with a given pattern, compatible with the Java 6 API.

What does SimpleDateFormat return?

Return Value: The method returns Date or time in string format of mm/dd/yyyy.

How do I change date format in SimpleDateFormat?

Java SimpleDateFormat Example String pattern = "MM-dd-yyyy"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);

What is the difference between DateFormat and SimpleDateFormat?

The java SimpleDateFormat allows construction of arbitrary non-localized formats. The java DateFormat allows construction of three localized formats each for dates and times, via its factory methods.


1 Answers

Day is 01 because it stands for 01 January. Hour 01 may be related to your time zone. Try this:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd:HH:mm:ss");    
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String formatted = simpleDateFormat.format(900000);
like image 177
Adam Dyga Avatar answered Sep 30 '22 20:09

Adam Dyga