【问题标题】:Time Ago Function with Month and Year Android [duplicate]带有月份和年份Android的时间前功能[重复]
【发布时间】:2017-06-02 14:30:48
【问题描述】:

我在我的一个 android 应用程序中使用 time ago 函数和 TimeUnit。但我成功了几天......我想要它直到一个月和一年。我的功能如下......任何人都可以建议我如何增加直到一年?

private String displayQuoteTime(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);
    String postTime = "";
    if (day > 0) {
        if (day == 1) {
            postTime = day + " day";
        } else {
            postTime = day + " days";
        }
    } else if (hours > 0) {
        if (TextUtils.isEmpty(postTime)) {
            if (hours == 1) {
                postTime = hours + " hour";
            } else {
                postTime = hours + " hours";
            }

        } else {
            postTime = postTime + " " + hours + " hours";
        }

    } else if (minute > 0) {
        if (TextUtils.isEmpty(postTime)) {
            if (minute == 1) {
                postTime = minute + " min";
            } else {
                postTime = minute + " mins";
            }
        } else
            postTime = postTime + " " + minute + " mins";
    } else {
        if (TextUtils.isEmpty(postTime))
            postTime = "second";
    }
    postTime = postTime + " ago";

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

我还在学习....如果有人可以解决我的问题,请告诉我。谢谢

【问题讨论】:

  • @OleV.V.它不重复......你只能看到那里直到几天......而不是一个月等......
  • 您阅读答案了吗?四个答案中的三个涉及月份和年份。

标签: java android time duration period


【解决方案1】:
private static String displayQuoteTime(long seconds) {

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

            - TimeUnit.DAYS.toHours(day);
    int months = day / 30;
    int years = months / 12;
    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);
    String postTime = "";
    if (years > 0) {
        if (TextUtils.isEmpty(postTime)) {
            if (years == 1) {
                postTime = years + " year";
            } else {
                postTime = years + " years";
            }

        } else {
            postTime = postTime + " " + years + " years";
        }
    }
        else if (months > 0) {
        if (TextUtils.isEmpty(postTime)) {
            if (months == 1) {
                postTime = months + " month";
            } else {
                postTime = months + " months";
            }
        } else
            postTime = postTime + " " + months + " months";
    } else if (day > 0) {
        if (day == 1) {
            postTime = day + " day";
        } else {
            postTime = day + " days";
        }
    } else if (hours > 0) {
        if (TextUtils.isEmpty(postTime)) {
            if (hours == 1) {
                postTime = hours + " hour";
            } else {
                postTime = hours + " hours";
            }

        } else {
            postTime = postTime + " " + hours + " hours";
        }

    } else if (minute > 0) {
        if (TextUtils.isEmpty(postTime)) {
            if (minute == 1) {
                postTime = minute + " min";
            } else {
                postTime = minute + " mins";
            }
        } else
            postTime = postTime + " " + minute + " mins";
    }else {
        if (TextUtils.isEmpty(postTime))
            postTime = "second";
    }
    postTime = postTime + " ago";

    System.out.println(" years " + years + " months " + months + " Day " + day + " Hour " + hours + " Minute "
            + minute + " Seconds " + second);
    return postTime;
}

【讨论】:

  • 一个月 30 天?不是很准确/完美,但也许足够好,OP 将不得不做出决定。另外,虽然我已经看到了接受刻度线,但通常只有代码的答案很少有帮助,请添加一些解释。
猜你喜欢
  • 1970-01-01
  • 2021-05-10
  • 2012-10-22
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多