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

4

6

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!

Glabrate answered 9/10, 2012 at 13:31 Comment(2)
I don't think you can use dd for DAYS since one d is Day of the monthAcetylide
d instead of dd gives me 1 instead of 01, but unfortunately not 0 :(Glabrate
A
8

You are going wrong about it by using SimpleDateFormat

SimpleDateFormat is use to get a DATE instance not counting time

You should use TimeUnit

Here's an example

 public static void main(String[] args) throws Exception {
        long millis = 900000;

        String s = String.format("%d min, %d sec",
                TimeUnit.MILLISECONDS.toMinutes(millis),
                TimeUnit.MILLISECONDS.toSeconds(millis)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

        System.out.println(s);


    }

This will get you

15 min, 0 sec

Just format to your heart's content and add Days and Hours...

Acetylide answered 9/10, 2012 at 13:44 Comment(0)
J
12

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);
Juliajulian answered 9/10, 2012 at 13:35 Comment(1)
Thanks alot, this fixed my hours! :) Any idea how to fix the number of days?Glabrate
A
8

You are going wrong about it by using SimpleDateFormat

SimpleDateFormat is use to get a DATE instance not counting time

You should use TimeUnit

Here's an example

 public static void main(String[] args) throws Exception {
        long millis = 900000;

        String s = String.format("%d min, %d sec",
                TimeUnit.MILLISECONDS.toMinutes(millis),
                TimeUnit.MILLISECONDS.toSeconds(millis)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

        System.out.println(s);


    }

This will get you

15 min, 0 sec

Just format to your heart's content and add Days and Hours...

Acetylide answered 9/10, 2012 at 13:44 Comment(0)
B
7

The value 900,000 passed to SimpleDateFormat is a point in time ... 15 minutes past January 1, 1970, 00:00:00 GMT or:

January 1, 1970, 00:15:00 GMT

On this date and time in 1970 in your time zone the day is 1 and the hour is 1.

Branchiopod answered 9/10, 2012 at 13:42 Comment(0)
T
0

The answer is correct. The SimpleDate.format(900000) statement formats the time 900000 ms past epoch which would be 15 minutes into the first hour

Tomfoolery answered 9/10, 2012 at 13:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.