1

what is the easiest and fastest way to convert minutes (double) to default time hh:mm:ss

for example I used this code in python and it's working

time = timedelta(minutes=250.0) print time

result: 4:10:00

is there a java library or a simple code can do it?

abdulla-alajmi
  • 491
  • 1
  • 9
  • 17
  • What do you mean by "default time"? What's defaulting here? It sounds like you mean "to a text format of hh:mm:ss". – Jon Skeet Nov 21 '14 at 11:53
  • In case before you have your double you also have a Date `SimpleDateFormat sdf = new SimpleDateFormat("HH:mm.ss.S"); String timestamp = sdf.format(myDate.getTime());` – dasLort Nov 21 '14 at 12:02
  • @Jon Skeet the default time is hh:mm:ss because you don't use just minutes or just hours or seconds. – abdulla-alajmi Nov 21 '14 at 13:22
  • @dasLort thanks, MihaiC already solved my problem. – abdulla-alajmi Nov 21 '14 at 13:26
  • Who doesn't? And it's still not a "default time" - it may be a default time *format*, but it's not a time in itself. – Jon Skeet Nov 21 '14 at 13:26
  • @JonSkeet well, you cannot say I will come after 382 minutes while you mean 6 hours and 22 minutes! :) thanks Jon – abdulla-alajmi Nov 21 '14 at 13:59
  • @user2564147: Um, you can. And if the value is 5 minutes, you're unlikely to say "I will come in 0 hours 5 minutes and 0 seconds." That's certainly not something *I'd* say, anyway... – Jon Skeet Nov 21 '14 at 14:11

3 Answers3

0

EDIT: To show the seconds as SS you can make an easy custom formatter variable to pass to the String.format() method

EDIT: Added logic to add one minute and recalculate seconds if the initial double value has the number value after the decimal separator greater than 59.

EDIT: Noticed loss of precision when doing math on the double (joy of working with doubles!) seconds, so every now and again it would not be the correct value. Changed code to properly calculate and round it. Also added logic to treat cases when minutes and hour overflow because of cascading from seconds.

Try this (no external libraries needed)

public static void main(String[] args) {
    final double t = 1304.00d;

    if (t > 1440.00d) //possible loss of precision again
        return;

    int hours = (int)t / 60;
    int minutes = (int)t % 60;
    BigDecimal secondsPrecision = new BigDecimal((t - Math.floor(t)) * 100).setScale(2, RoundingMode.HALF_UP);
    int seconds = secondsPrecision.intValue();

    boolean nextDay = false;

    if (seconds > 59) {
        minutes++; //increment minutes by one
        seconds = seconds - 60; //recalculate seconds
    }

    if (minutes > 59) {
        hours++;
        minutes = minutes - 60;
    }

    //next day
    if (hours > 23) {
        hours = hours - 24;
        nextDay = true;
    }

    //if seconds >=10 use the same format as before else pad one zero before the seconds
    final String myFormat = seconds >= 10 ? "%d:%02d:%d" : "%d:%02d:0%d";
    final String time = String.format(myFormat, hours, minutes, seconds);
    System.out.print(time);
    System.out.println(" " + (nextDay ? "The next day" : "Current day"));
}

Of course this can go on and on, expanding on this algorithm to generalize it. So far it will work until the next day but no further, so we could limit the initial double to that value.

 if (t > 1440.00d)
        return;
MihaiC
  • 1,618
  • 1
  • 10
  • 14
  • correct! easy and simple, one more thing, the seconds displayed as S how can I change it to SS (for example: 8 to 08) thanks a lot – abdulla-alajmi Nov 21 '14 at 13:54
  • also please not that the initial double can't have a value for the decimals greater than 59 (the result won't add 1 minute) so if you define it as say 30.60 or 30.61 it won't be correct. you have to ensure the value after the . is not greater than 59. you can easily check with an if after you calculate the seconds so if(seconds>59) System.out.println("Invalid input time") – MihaiC Nov 21 '14 at 14:18
  • i edited code to treat the value greater than 59 seconds – MihaiC Nov 21 '14 at 14:23
  • I fixed it and got the correct time (wouldn't you mind if I edit your answer?) sometimes the seconds value is 59.134134 I add if seconds > 59 seconds = 00 minutes++ thank you so much – abdulla-alajmi Nov 21 '14 at 17:35
  • please check this http://stackoverflow.com/questions/27053276/date-and-time-picker-in-java – abdulla-alajmi Nov 21 '14 at 17:36
0

Using Joda you can do something like:

import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;


    final Period a = Period.seconds(25635);
    final PeriodFormatter hoursMinutes = new PeriodFormatterBuilder().appendHours().appendSuffix(" hour", " hours")
            .appendSeparator(" and ").appendMinutes().appendSuffix(" minute", " minutes").appendSeparator(" and ")
            .appendSeconds().appendSuffix(" second", " seconds").toFormatter();
    System.out.println(hoursMinutes.print(a.normalizedStandard()));
jjlema
  • 850
  • 5
  • 8
0
//Accept minutes from user and return time in HH:MM:SS format

    private String convertTime(long time)
{
        String finalTime = "";
        long hour = (time%(24*60)) / 60;
        long minutes = (time%(24*60)) % 60;
        long seconds = time / (24*3600);

        finalTime = String.format("%02d:%02d:%02d",
                TimeUnit.HOURS.toHours(hour) ,
                TimeUnit.MINUTES.toMinutes(minutes),
                TimeUnit.SECONDS.toSeconds(seconds));
        return finalTime;
    }
Nitin Mehta
  • 255
  • 4
  • 3