Skip to content Skip to sidebar Skip to footer

Formatting Milliseconds To Hh:mm:ss Format

I am having milliseconds value and want to display the time subtracting 5 minutes from current milliseconds value in hh:mm:ss format. Code String str = String.format('%02d:%02d:%0

Solution 1:

Check this solution. It's more elegant

SimpleDateFormatsdf=newSimpleDateFormat("HH:mm:ss", Locale.getDefault());
Stringstr= sdf.format(newDate(System.currentTimeMillis()));
Toast.makeText(getApplicationContext(), "Alarm Set."+str, Toast.LENGTH_LONG).show()

Solution 2:

It is working fine as is, the only reason you see such a huge offset is because it is calculating the total number of hours since the UNIX epoch.

When you do a Calendar.getInstance() it gets you the current point in time. Converting it to milliseconds are the total millis since the UNIX epoch.

You can check the total number of hours since the epoch:

//Check for the hours since the UNIX Epoch
System.out.println(System.currentTimeMillis() / 3600000);

Output:

386439

You code below would also produce this result appended with the minutes and seconds of the current point in time:

Calendar cal = Calendar.getInstance();

Stringstr = String
        .format("%02d:%02d:%02d",
                TimeUnit.MILLISECONDS.toHours((cal.getTimeInMillis() - 300000)),
                TimeUnit.MILLISECONDS.toMinutes(cal.getTimeInMillis() - 300000)
                        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                                .toHours(cal.getTimeInMillis() - 300000)),
                TimeUnit.MILLISECONDS.toSeconds(cal.getTimeInMillis() - 300000)
                        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                                .toMinutes(cal.getTimeInMillis() - 300000)));

System.out.println(str);

Output:

386439:38:20

Note: Your reference example considers a constant value of millis (3600000) hence it gets a readable time there.

The better solution is provided in the other answer which provides for your requirement.

Solution 3:

Using the Joda-Time library makes this work much easier.

// Note that milliseconds-since-epoch needs to be a "long" rather than an "int".longmillis=newDateTime().getMillis(); 

// Specify a time zone rather than rely on default.DateTimeZonetimeZone= DateTimeZone.forID( "Asia/Kolkata" ); // Formerly known as Calcutta India.// Instantiate a DateTime object from number of milliseconds since Unix epoch.DateTimedateTime=newDateTime( millis, timeZone );

// Go back 5 minutes.DateTimedateTimeEarlier= dateTime.minusMinutes( 5 );

// Get a formatter to render a string of the time portion.DateTimeFormatterformatter= ISODateTimeFormat.hourMinuteSecond();

// Use the formatter to create a string.Stringoutput= formatter.print( dateTimeEarlier );

Dump to console…

System.out.println( "millis: " + millis );
System.out.println( "dateTime: " + dateTime );
System.out.println( "dateTimeEarlier: " + dateTimeEarlier );
System.out.println( "output: " + output );

When run…

millis: 1391391422174
dateTime: 2014-02-03T07:07:02.174+05:30
dateTimeEarlier: 2014-02-03T07:02:02.174+05:30
output: 07:02:02

Post a Comment for "Formatting Milliseconds To Hh:mm:ss Format"