10

I'm trying to get a double value with seconds to convert to a custom time format. I've tried SimpleDate Format but that doesn't work...

I have a double value in this format: 407.33386569554585 (representing 407 seconds and 333 milliseconds ...).

I need that in this format: HH/MM/SS.MS

How would i do that?

Thanks in advance!

isherwood
  • 58,414
  • 16
  • 114
  • 157
Diesal11
  • 3,389
  • 9
  • 28
  • 29

4 Answers4

19

Multiply by 1,000 then cast to a long and construct a Date with it.

Date date = new Date((long)(dubTime*1000));

From here, you can use a SimpleDateFormat to make the string you'd like.

String formattedDate = new SimpleDateFormat("HH/mm/ss.SSS").format(date);
Tim Bender
  • 20,112
  • 2
  • 49
  • 58
  • 2
    It keeps adding 9 hours and 30 mins on....funnily enough thats my time zone +9:30... – Diesal11 Nov 13 '11 at 23:51
  • Sounds like you might be wanting to set the `Locale` of the `Date` or change the `Locale` after construction. Not sure. Basically, the `long` you are passing is going to be interpreted as the milliseconds that have passed since Jan 1, 1970 GMT. If the `double` value for time you have is not a count of seconds since that epoch, you'll need to perform additional transformations to make it so. – Tim Bender Nov 14 '11 at 03:15
6

And in Java 8 style (if you want to avoid the Date deprecation warning):

DateTimeFormatter formatter =
    DateTimeFormatter.ofPattern("HH/mm/ss.SSS").withZone(ZoneId.of("UTC"));
Instant instant = Instant.ofEpochMilli((long)(dubTime*1000));
return formatter.format(instant);
Jacob Eckel
  • 1,633
  • 1
  • 13
  • 22
1

Just to add to Tim's answer: to get proper string regardless of your timezone, just use SimpleDateFormat.setTimeZone():

DateFormat dateFormat = new SimpleDateFormat("HH/mm/ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
// ...
double doubleTime=1000.0;
Date date = new Date((long) (doubleTime*1000));
String formattedTime = dateFormat.format(date);

And be sure to consult DateFormat.setTimeZone javadoc to know when the timeZone may get overwritten.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
1

Here is a bit of code we use that you can easily adapt. The implementation below will print a value as 00days 00h00m00s00ms:

public final static long ONE_MILLISECOND = 1;
public final static long MILLISECONDS_IN_A_SECOND = 1000;

public final static long ONE_SECOND = 1000;
public final static long SECONDS_IN_A_MINUTE = 60;

public final static long ONE_MINUTE = ONE_SECOND * 60;
public final static long MINUTES_IN_AN_HOUR = 60;

public final static long ONE_HOUR = ONE_MINUTE * 60;
public final static long HOURS_IN_A_DAY = 24;
public final static long ONE_DAY = ONE_HOUR * 24;
public final static long DAYS_IN_A_YEAR = 365;

public String formatHMSM(Number n) {


    String res = "";
    if (n != null) {
        long duration = n.longValue();

        duration /= ONE_MILLISECOND;
        int milliseconds = (int) (duration % MILLISECONDS_IN_A_SECOND);
        duration /= ONE_SECOND;
        int seconds = (int) (duration % SECONDS_IN_A_MINUTE);
        duration /= SECONDS_IN_A_MINUTE;
        int minutes = (int) (duration % MINUTES_IN_AN_HOUR);
        duration /= MINUTES_IN_AN_HOUR;
        int hours = (int) (duration % HOURS_IN_A_DAY);
        duration /= HOURS_IN_A_DAY;
        int days = (int) (duration % DAYS_IN_A_YEAR);
        duration /= DAYS_IN_A_YEAR;
        int years = (int) (duration);
        if (days == 0) {
            res = String.format("%02dh%02dm%02ds%03dms", hours, minutes, seconds, milliseconds);
        } else if (years == 0) {
            res = String.format("%ddays %02dh%02dm%02ds%03dms", days, hours, minutes, seconds, milliseconds);
        } else {
            res = String.format("%dyrs %ddays %02dh%02dm%02ds", years, days, hours, minutes, seconds);
        }
    }
    return res;

}
Jason Buberel
  • 4,930
  • 2
  • 19
  • 10