4

我有一些文档,它的创建时间以毫秒为单位。我需要将它们分开为今天、昨天、过去 7 天、过去 30 天、超过 30 天。

我使用了以下代码:convertSimpleDayFormat(1347022979786);

public static String convertSimpleDayFormat(Long val) {


            long displayTime = System.currentTimeMillis() - val;
            displayTime = displayTime/86400000;
            String displayTimeVal = "";
            if(displayTime <1)
            {
                    displayTimeVal = "today";  
            }
            else if(displayTime<2)
            {
                    displayTimeVal = "yesterday";
            } 
            else if(displayTime<7)
            {
                    displayTimeVal = "last7days";
            } 
            else if(displayTime<30)
            {
                    displayTimeVal = "last30days";
            }
            else
            {
                    displayTimeVal = "morethan30days";
            }
            return displayTimeVal;
    }

我减去当前时间并传递毫秒并转换为一天。

但我面临的问题是,我无法以毫秒为单位计算日期的确切时间。

我想为今天计算:从午夜 00:00 到午夜 24:00。(正好是 24 小时。)

同样,我想将毫秒精确地转换为今天、昨天、过去 7 天、过去 30 天和超过 30 天。

4

2 回答 2

9
private static Calendar clearTimes(Calendar c) {
    c.set(Calendar.HOUR_OF_DAY,0);
    c.set(Calendar.MINUTE,0);
    c.set(Calendar.SECOND,0);
    c.set(Calendar.MILLISECOND,0);
    return c;
}

public static String convertSimpleDayFormat(long val) {
    Calendar today=Calendar.getInstance();
    today=clearTimes(today);

    Calendar yesterday=Calendar.getInstance();
    yesterday.add(Calendar.DAY_OF_YEAR,-1);
    yesterday=clearTimes(yesterday);

    Calendar last7days=Calendar.getInstance();
    last7days.add(Calendar.DAY_OF_YEAR,-7);
    last7days=clearTimes(last7days);

    Calendar last30days=Calendar.getInstance();
    last30days.add(Calendar.DAY_OF_YEAR,-30);
    last30days=clearTimes(last30days);


    if(val >today.getTimeInMillis())
    {
            return "today";  
    }
    else if(val>yesterday.getTimeInMillis())
    {
        return "yesterday";
    } 
    else if(val>last7days.getTimeInMillis())
    {
        return "last7days";
    } 
    else if(val>last30days.getTimeInMillis())
    {
        return "last30days";
    }
    else
    {
        return "morethan30days";
    }
}
于 2012-09-10T06:34:07.580 回答
0

这是一个小技巧,没有经过严格的测试。使用风险自负。我已使其可扩展,因此您可以添加新的持续时间。

public static String prettyTimeStamp(long timeStamp) {
        Calendar c = Calendar.getInstance();
        c.clear(Calendar.HOUR_OF_DAY);
        c.clear(Calendar.MINUTE);
        c.clear(Calendar.SECOND);
        c.clear(Calendar.MILLISECOND);
        long today = c.getTimeInMillis();
        final long oneDay = 24 * 60 * 60 * 1000L;
        final long[] durations = new long[] { today - oneDay, today,
                today + 7 * oneDay, today + 30 * oneDay };
        final String[] labels = "Yesterday,Today,Last 7 days,Last 30 Days,More than 30 Days"
                .split(",");
        int pos = Arrays.binarySearch(durations, timeStamp);
        return labels[pos < 0 ? ~pos : pos];
    }

顺便说一句,你真的应该只使用像PrettyTime这样的库

于 2012-09-10T06:48:02.897 回答