1

如何区分 4 周前和 1 个月前?当我在 android 上为它编写测试代码时,我遇到了这个问题。

我认为这取决于我测试的日期。我觉得对吗?如果我的意见是正确的,那么我该如何做好或通过测试代码?

我通过测试代码的一个想法是现在保持测试日期。对我有什么想法吗?


对不起,我想我的问题不清楚。我的代码的一部分在这里。

assertEquals("a month ago", timeAgoWithAddedValue(Calendar.MONTH, -1));

private String timeAgoWithAddedValue(int field, int value) {
    Calendar cal = Calendar.getInstance();
    cal.add(field, value);
    return timeAgo(cal.getTime());
}

public static final long WEEK_UNIT = DAY_UNIT * 7;
public static final long MONTH_UNIT = 2629746000L; // I found this value online, 1 Gregorian month -> ms
public static final long YEAR_UNIT = 31556952000L; // I found this value online, 1 Gregorian year -> ms

public String timeAgo(Date date) {
        long now = Calendar.getInstance().getTimeInMillis();
        long sinceNowInMillis = now - date.getTime();

        ...
        if (sinceNowInMillis < MONTH_UNIT) {
            long weeks = sinceNowInMillis / WEEK_UNIT;
            // return proper ago string with weeks
        }

        if (sinceNowInMillis < YEAR_UNIT) {
            long months = sinceNowInMillis / MONTH_UNIT;
            // return proper ago string with months
        }
        ...
}

几个月前我写了代码,当时它成功了。但是今天测试代码失败了。因为代码是预期的4 weeks ago。我想知道我的代码是错误的还是问题取决于周长,所以这是自然问题?

4

3 回答 3

0

获取以下功能:

  public static String millisToLongDHMS(long duration) {
  if (duration > 0) {
     duration = new Date().getTime() - duration;
  }
  if (duration < 0) {
     duration = 0;
  }

  StringBuffer res = new StringBuffer();
  long temp = 0;
  if (duration >= ONE_SECOND) {
     temp = duration / ONE_DAY;
     if (temp > 0) {
        duration -= temp * ONE_DAY;
        res.append(temp).append(" day").append(temp > 1 ? "s" : "")
                .append(duration >= ONE_MINUTE ? ", " : "");
     }

     temp = duration / ONE_HOUR;
     if (temp > 0) {
        duration -= temp * ONE_HOUR;
        res.append(temp).append(" hour").append(temp > 1 ? "s" : "")
                .append(duration >= ONE_MINUTE ? ", " : "");
     }

     temp = duration / ONE_MINUTE;
     if (temp > 0) {
        duration -= temp * ONE_MINUTE;
        res.append(temp).append(" min").append(temp > 1 ? "s" : "");
     }

     if (!res.toString().equals("") && duration >= ONE_SECOND) {
        res.append(" and ");
     }

     temp = duration / ONE_SECOND;
     if (temp > 0) {
        res.append(temp).append(" sec").append(temp > 1 ? "s" : "");
     }
     res.append(" ago");
     return res.toString();
  } else {
     return "Just Now";
  }
 }

更新

你可以使用我的另一个功能:

  public static String getTimeAgo(long time) {
     if (time < 1000000000000L) {
     // if timestamp given in seconds, convert to millis
     time *= 1000;
  }

  long now = System.currentTimeMillis();
  if (time > now || time <= 0) {
     return null;
  }

  // TODO: localize
  final long diff = now - time;
  if (diff < MINUTE_MILLIS) {
     return "just now";
  } else if (diff < 2 * MINUTE_MILLIS) {
     return "a minute ago";
  } else if (diff < 50 * MINUTE_MILLIS) {
     return diff / MINUTE_MILLIS + " min ago";
  } else if (diff < 90 * MINUTE_MILLIS) {
     return "an hour ago";
  } else if (diff < 24 * HOUR_MILLIS) {
     return diff / HOUR_MILLIS + " hours ago";
  } else if (diff < 48 * HOUR_MILLIS) {
     return "yesterday";
  } else {
     return diff / DAY_MILLIS + " days ago";
  }
 }
于 2016-12-26T07:46:07.687 回答
0

您可以使用这行代码来实现您的目标,而无需在项目中使用任何库

 DateUtils.getRelativeTimeSpanString(timestamp, new Date().getTime(), DateUtils.MINUTE_IN_MILLIS)

timestamp是很长的时间毫秒

在此处阅读更多文档DateUtils

例如,如果您传递时间戳,1482739673913您将得到6 minutes ago

如果时间戳是 1482582128877你会得到2 days ago

于 2016-12-26T07:53:42.580 回答
0

我弄清楚是什么问题。这取决于一个月中的几天。

一个月中的日子不一样(10 月 31 日、11 月 30 日、12 月 31 日 ..),但我在代码中计算时使用的是常量。

我的意思是,如果一个月有 31 天,那么我的代码中的 sinceNowInMillis 小于 MONTH_UNIT。所以结果是“4 周前”遵循我的代码。因为我使用 Calendar.add(Calender.MONTH, -1) 计算了目标日期。

另一方面,如果月份有 30 天或更少,因为我的代码中的 NowInMillis 大于 MONTH_UNIT。所以结果是按照我的代码“1 个月前”。

于 2016-12-26T14:15:53.623 回答