无法按预期将毫秒转换为秒

时间:2015-07-26 09:16:42

标签: android milliseconds seconds

嗨我需要将毫秒(2015年7月25日为1437790538)转换为秒,但是当我试图转换秒数然后它不起作用时,我得到了无关的结果。从最近两天起,这些结果非常强调。

我已经使用此代码来实现此目的

              long duration_seconds  = 1437790538;

            Calendar calendar = Calendar.getInstance();
                TimeZone tz = TimeZone.getDefault();
                calendar.setTimeInMillis(duration_seconds * 1000);
                calendar.add(Calendar.MILLISECOND,
                        tz.getOffset(calendar.getTimeInMillis()));
                 SimpleDateFormat sdf = new
                 SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                Date currenTimeZone = (Date) calendar.getTime();
                String resultabc = sdf.format(currenTimeZone);

                long curMillis = currenTimeZone.getTime() / 1000;

// long seconds =(curMillis / 1000)%60;

                Log.e("test", "datee1 :" + resultabc + " , " + curMillis + " , " + seconds);

                getDurationBreakdown(curMillis);

                calculateTime(seconds);

我需要计算这样的帖子总时间

public static void calculateTime(long seconds) {

    int day1 = (int) TimeUnit.SECONDS.toDays(seconds);
    long hours1 = TimeUnit.SECONDS.toHours(seconds) - (day1 * 24);
    long minute1 = TimeUnit.SECONDS.toMinutes(seconds)
            - (TimeUnit.SECONDS.toHours(seconds) * 60);
    long second1 = TimeUnit.SECONDS.toSeconds(seconds)
            - (TimeUnit.SECONDS.toMinutes(seconds) * 60);

    Log.e("test", "time interval ::" + "Day " + day1 + " Hour " + hours1
            + " Minute " + minute1 + " Seconds " + second1);
    // int days, hours, mins, seconds, justnow;
    days = (int) day1;
    hours = (int) hours1;
    mins = (int) minute1;
    seconds = (int) second1;

    System.out.println("Day " + day1 + " Hour " + hours1 + " Minute1 "
            + minute1 + " Seconds " + second1);
 public static String getDurationBreakdown(long millis)
    {
        if(millis < 0)
        {
            throw new IllegalArgumentException("Duration must be greater than zero!");
        }

        long days = TimeUnit.MILLISECONDS.toDays(millis);
        millis -= TimeUnit.DAYS.toMillis(days);
        long hours = TimeUnit.MILLISECONDS.toHours(millis);
        millis -= TimeUnit.HOURS.toMillis(hours);
        long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
        millis -= TimeUnit.MINUTES.toMillis(minutes);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);

        StringBuilder sb = new StringBuilder(64);
        sb.append(days);
        sb.append(" Days ");
        sb.append(hours);
        sb.append(" Hours ");
        sb.append(minutes);
        sb.append(" Minutes ");
        sb.append(seconds);
        sb.append(" Seconds");

        Log.e("test", "time interval" + sb.toString());

        return(sb.toString());
    }

}

这两种方法都给出了错误的结果。 如果有人知道如何以正确的方式做这件事请帮我解决这个问题.. 提前谢谢

2 个答案:

答案 0 :(得分:2)

你的

long curMillis = currenTimeZone.getTime() / 1000;

似乎有误导性。 .getTime()会以毫秒为单位给你时间,所以将它除以1000就会得到秒而不是毫米......

<强>更新

据我了解您的代码,您只想计算给定日期(以秒表示)与实际日期之间的差异。为此你可以做到

long myDateInSecs = 1437790538; //  25 july 2015

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(myDateInSecs * 1000);

Calendar calendarNow = Calendar.getInstance();

long diff = calendarNow.getTime() - calendar.getTime();

long seconds = TimeUnit.MILLISECONDS.toSeconds(diff);
long minutes = TimeUnit.MILLISECONDS.toMinutes(diff); 
...

答案 1 :(得分:0)

Sleep
  

1437790538%60 - &gt; 38

1437790538是以秒为单位的时间戳,而不是以毫秒为单位。