我该如何格式化这个日期?

发布于 2024-10-03 04:44:41 字数 134 浏览 6 评论 0原文

我有一个格式如下的日期:

"yyyy-MM-dd'T'HH:mm:ss'Z'"

我想将其转换为类似以下内容的内容:

“3 分钟前”

我该如何执行此操作?

I have a date that has the format:

"yyyy-MM-dd'T'HH:mm:ss'Z'"

I'd like to convert it to something similar to:

"3 minutes ago"

How can I go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何处潇湘 2024-10-10 04:44:41

丑陋,但确实有效……是的,是重复的! :(

public static void main(String[] args) {
    Calendar c = Calendar.getInstance();
    c.set(2010, 10, 20, 22, 37, 12);

    Date start = c.getTime();

    Date end = new Date();


    long diff = (end.getTime() - start.getTime()) / 1000;

    long days    = diff / (60 * 60 * 24);
    long hours   = diff / (60 * 60) - days * 24;
    long minutes = diff / 60 - (hours * 60 + days * 24 * 60);
    long seconds = diff % 60;

    String output = "";
    if (days >= 1) 
        output += days + " day" + ((days > 1) ? "s" : "");

    if (hours >= 1) 
        output += " " + hours + " hour" + ((hours > 1) ? "s" : "");

    if (minutes >= 1) 
        output += " " + minutes + " minute" + ((minutes > 1) ? "s" : "");

    if (seconds >= 1) 
        output += " " + seconds + " second" + ((seconds > 1) ? "s" : "");

    output = output.trim();

    System.out.println(output);
}

Ugly, but does the trick.. and yes, a duplicate! :(

public static void main(String[] args) {
    Calendar c = Calendar.getInstance();
    c.set(2010, 10, 20, 22, 37, 12);

    Date start = c.getTime();

    Date end = new Date();


    long diff = (end.getTime() - start.getTime()) / 1000;

    long days    = diff / (60 * 60 * 24);
    long hours   = diff / (60 * 60) - days * 24;
    long minutes = diff / 60 - (hours * 60 + days * 24 * 60);
    long seconds = diff % 60;

    String output = "";
    if (days >= 1) 
        output += days + " day" + ((days > 1) ? "s" : "");

    if (hours >= 1) 
        output += " " + hours + " hour" + ((hours > 1) ? "s" : "");

    if (minutes >= 1) 
        output += " " + minutes + " minute" + ((minutes > 1) ? "s" : "");

    if (seconds >= 1) 
        output += " " + seconds + " second" + ((seconds > 1) ? "s" : "");

    output = output.trim();

    System.out.println(output);
}
有木有妳兜一样 2024-10-10 04:44:41

要搅动日期,请检查如何解析日期?
为了以更好的格式显示它,请检查 PrettyTime

For oarsing the date, check How to parse a date?
For displaying it in a nicer format, check for PrettyTime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文