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

时间:2010-11-20 21:02:10

标签: java android datetime time simpledateformat

我的日期格式为:

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

我想把它转换为类似的东西:

“3分钟前”

我该怎么做呢?

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

丑陋,但是诀窍......是的,重复一次! :(

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);
}

答案 2 :(得分:0)

要解析日期,请检查How to parse a date? 要以更好的格式显示它,请检查PrettyTime