49

我正在使用 TimeStamp 类将秒转换为天、小时、分钟、秒。我使用了以下代码

public static void calculateTime(long seconds) {
        int day = (int)TimeUnit.SECONDS.toDays(seconds);        
        long hours = TimeUnit.SECONDS.toHours(seconds) - TimeUnit.SECONDS.toHours(TimeUnit.SECONDS.toDays(seconds));
        long minute = TimeUnit.SECONDS.toMinutes(seconds) - TimeUnit.SECONDS.toMinutes(TimeUnit.SECONDS.toHours(seconds));
        long second = TimeUnit.SECONDS.toSeconds(seconds) - TimeUnit.SECONDS.toSeconds(TimeUnit.SECONDS.toMinutes(seconds));

        System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second);

    }

但我没有得到正确的结果。例如,当我调用此方法时,calculateTime(3600)它给了我结果Day 0 Hour 1 Minute 60 Seconds 3540而不是Day 0 Hour 1 Minute 0 Seconds 0.

我的逻辑有什么问题?请帮我。

4

8 回答 8

107

它应该像

 int day = (int)TimeUnit.SECONDS.toDays(seconds);        
 long hours = TimeUnit.SECONDS.toHours(seconds) - (day *24);
 long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);
 long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60);

编辑 说明:

  1. 天数计算是正确的,不需要解释。
  2. TimeUnit.SECONDS.toHours(seconds) 将为您提供从秒到小时的直接转换,而不考虑您已经计算的天数。减去您已经获得的天数的小时数,即天*24。你现在有剩余的时间。
  3. 分钟和秒也一样。您需要分别减去已经获得的小时和分钟。
于 2012-07-06T07:55:37.923 回答
12

你可以这样做只使用TimeUnit

public static void calculateTime(long seconds) {
    int day = (int) TimeUnit.SECONDS.toDays(seconds);
    long hours = TimeUnit.SECONDS.toHours(seconds) -
                 TimeUnit.DAYS.toHours(day);
    long minute = TimeUnit.SECONDS.toMinutes(seconds) - 
                  TimeUnit.DAYS.toMinutes(day) -
                  TimeUnit.HOURS.toMinutes(hours);
    long second = TimeUnit.SECONDS.toSeconds(seconds) -
                  TimeUnit.DAYS.toSeconds(day) -
                  TimeUnit.HOURS.toSeconds(hours) - 
                  TimeUnit.MINUTES.toSeconds(minute);
    System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second);
}

或稍短但可能不那么直观

public static void calculateTime(long seconds) {
    int day = (int) TimeUnit.SECONDS.toDays(seconds);
    long hours = TimeUnit.SECONDS.toHours(seconds) -
                 TimeUnit.DAYS.toHours(day);
    long minute = TimeUnit.SECONDS.toMinutes(seconds) -
                  TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(seconds));
    long second = TimeUnit.SECONDS.toSeconds(seconds) -
                  TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(seconds));
    System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second);
}
于 2012-07-06T08:30:40.397 回答
10

简单方法:

public static void calculateTime(long seconds) {
    long sec = seconds % 60;
    long minutes = seconds % 3600 / 60;
    long hours = seconds % 86400 / 3600;
    long days = seconds / 86400;

    System.out.println("Day " + days + " Hour " + hours + " Minute " + minutes + " Seconds " + sec);
}
于 2016-06-16T09:50:28.057 回答
2

这是我创建的代码:(3600 秒显示“天:0 小时:1 分钟:0 秒:0”)

public class TimeConvert 
{
    public static void main(String[] args) 
    {
        int fsec,d,h,m,s,temp=0,i;
        fsec=3600;
        //For Days
        if(fsec>=86400)
        {
            temp=fsec/86400;
            d=temp;
            for(i=1;i<=temp;i++)
            {
                fsec-=86400;
            }
        }
        else
        {
            d=0;
        }
        //For Hours
        if(fsec>=3600)
        {
            temp=fsec/3600;
            h=temp;
            for(i=1;i<=temp;i++)
            {
                fsec-=3600;
            }            
        }
        else
        {
            h=0;
        }
        //For Minutes
        if(fsec>=60)
        {
            temp=fsec/60;
            m=temp;
            for(i=1;i<=temp;i++)
            {
                fsec-=60;
            }            
        }
        else
        {
            m=0;
        }
        //For Seconds
        if(fsec>=1)
        {
            s=fsec;            
        }
        else
        {
            s=0;
        }
        System.out.println("Days:"+d+" Hours:"+h+" Minutes:"+m+" Seconds:"+s);
    }
}

希望它能回答你的问题。

于 2012-07-06T08:15:25.093 回答
1

迟到但有帮助

以 00:00:00 格式获取时间

/**
     * The time in format.
     *
     * in The Format of 00:00:00
     */

public String getTimeInFormat(long _SECONDS)
{
    if(TimeUnit.SECONDS.toHours(_SECONDS)>0)
    {
        return  String.format("%02d:%02d:%02d",
                TimeUnit.SECONDS.toHours(_SECONDS),
                TimeUnit.SECONDS.toMinutes(_SECONDS) -
                        TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(_SECONDS)),
                TimeUnit.SECONDS.toSeconds(_SECONDS) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(_SECONDS)));
    }
    else {
        return  String.format("%02d:%02d",
                TimeUnit.SECONDS.toMinutes(_SECONDS) -
                        TimeUnit.HOURS.toMinutes(TimeUnit.SECONDS.toHours(_SECONDS)),
                TimeUnit.SECONDS.toSeconds(_SECONDS) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.SECONDS.toMinutes(_SECONDS)));
    }

}
于 2018-10-25T08:17:32.957 回答
0

试试这个

public static void calculateTime(long seconds) {

int day = (int)TimeUnit.SECONDS.toDays(seconds);        
long hours = TimeUnit.SECONDS.toHours(seconds) ;

long tempSec = seconds - (TimeUnit.HOURS.toSeconds(hours) );
System.out.println("after hours calculation "+ tempSec);

long minute = TimeUnit.SECONDS.toMinutes(tempSec); 

if(tempSec > TimeUnit.MINUTES.toSeconds(minute)){
    tempSec = tempSec - (TimeUnit.MINUTES.toSeconds(minute) );          
}else{
    tempSec = TimeUnit.MINUTES.toSeconds(minute) - tempSec;
}
System.out.println("after min calculation "+ tempSec);


long second = TimeUnit.SECONDS.toSeconds(tempSec) ;

System.out.println("Day " + day + " Hour " + hours + " Minute " + minute + " Seconds " + second);
}
于 2012-07-06T08:17:50.053 回答
0

这是我的代码:

public static String secondsToString(TimeUnit greatestUnit, long sourceDuration, TimeUnit sourceUnit) {

    int ordinal = greatestUnit.ordinal();
    if(ordinal<=sourceUnit.ordinal())
        return String.format("%02d", sourceDuration);

    final long greatestDuration = greatestUnit.convert(sourceDuration, sourceUnit);
    final long rest = sourceDuration - sourceUnit.convert(greatestDuration, greatestUnit);

    return String.format("%02d:", greatestDuration) + secondsToString(TimeUnit.values()[--ordinal], rest, sourceUnit);
}

或循环

public static String secondsToStringByLoop(TimeUnit greatestUnit, long sourceDuration, TimeUnit sourceUnit) {

    final StringBuffer sb = new StringBuffer();
    int ordinal = greatestUnit.ordinal();

    while(true){
        if(ordinal<=sourceUnit.ordinal()) {
            sb.append(String.format("%02d", sourceDuration));
            break;
        }

        final long greatestDuration = greatestUnit.convert(sourceDuration, sourceUnit);
//          if(greatestDuration>0 || sb.length()>0)
        sb.append(String.format("%02d:", greatestDuration));

        sourceDuration -= sourceUnit.convert(greatestDuration, greatestUnit);
        greatestUnit = TimeUnit.values()[--ordinal];
    };

    return sb.toString();
}

用法示例:

String str = secondsToString(TimeUnit.DAYS, 1000, TimeUnit.SECONDS);

函数返回:“00:00:16:40”(天:小时:分钟:秒)

str = UnitsConverter.secondsToString(TimeUnit.DAYS, 1000, TimeUnit.MINUTES);

返回:“00:16:40”(天:小时:分钟)

str = UnitsConverter.secondsToString(TimeUnit.MINUTES, 1000, TimeUnit.SECONDS);

返回:“16:40”(分:秒)

于 2019-03-26T19:21:08.293 回答
-1
public static void timeCalculator(){


      Scanner input = new Scanner(System.in);
      System.out.print("Enter length of time in seconds: ");
      int n = input.nextInt();
      int nDay = n/86400;
      int nHours = (n%86400)/3600;
      int nMin = ((n%86400)%3600) /60;
      int nSec =(((n%86400)%3600)%60);
      System.out.println();
      System.out.print("That is "+ nDay+ " day(s),"+nHours+"  hour(s), "+nMin+"  minute(s), and "+nSec+" second(s). ");
}
于 2017-08-30T13:07:27.337 回答