JAVA convert minutes into default time [hh:mm:ss]

拜拜、爱过 提交于 2019-12-11 11:52:02

问题


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?


回答1:


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;



回答2:


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()));



回答3:


//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;
    }


来源:https://stackoverflow.com/questions/27060759/java-convert-minutes-into-default-time-hhmmss

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!