日期和时间转换 [英] Date and time conversion

查看:189
本文介绍了日期和时间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个新闻应用程序,并从这个模式中得到json响应的日期和时间(2017-09-23T14:22:28Z) / p>

如何将此日期和时间转换为类似的内容(5秒前,10分钟前,3小时前等)?
在过了24小时之后,它应该显示该特定新闻标题的正常日期(2017-09-23)。

解决方案 Joda-Time对此非常好。

  compile'joda-time:joda-time:2.9。 9'



  String dateTime =2017-09-21T14:22:28Z; 
DateTimeFormatter format = DateTimeFormat.forPattern(yyyy-MM-dd'T'HH:mm:ssZ);
DateTime myBirthDate = format.parseDateTime(dateTime);
DateTime now = new DateTime();
期间=新期间(myBirthDate,现在);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendSeconds()。appendSuffix(Sekonds ago \\\

.appendMinutes()。appendSuffix(Minutes ago \\\

.appendHours()。appendSuffix(Hours ago \\\

.appendDays()。appendSuffix(Days ago \\\

.appendWeeks()。appendSuffix(Weeks )
.appendMonths()。appendSuffix(Months ago \\\

.appendYears()。appendSuffix(Years ago\\\

.printZeroNever ()
.toFormatter();
if(period.getDays()<1){
String result = formatter.print(period);
System.out.println(result);
} else {
DateTimeFormatter format24hMore = DateTimeFormat.forPattern(yyyy-MM-dd);
String result = format24hMore.print(myBirthDate);
System.out.println(result);



$ block
$编辑
创建方法
p>



  public String convertDate(String date){

String result = ;
DateTimeFormatter format = DateTimeFormat.forPattern(yyyy-MM-dd'T'HH:mm:ssZ);
DateTime from = format.parseDateTime(date);
DateTime now = new DateTime();
期间=新期间(从,现在);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendSeconds()。appendSuffix(Sekonds ago \\\

.appendMinutes()。appendSuffix(Minutes ago \\\

.appendHours()。appendSuffix(Hours ago \\\

.appendDays()。appendSuffix(Days ago\\\

.appendWeeks()。appendSuffix(Weeks )
.appendMonths()。appendSuffix(Months ago \\\

.appendYears()。appendSuffix(Years ago\\\

.printZeroNever ()
.toFormatter();
if(period.getDays()<1){
result = formatter.print(period);
} else {
DateTimeFormatter format24hMore = DateTimeFormat.forPattern(yyyy-MM-dd);
result = format24hMore.print(from);

}
返回结果;
}

,您可以调用此方法

  System.out.println(convertDate(2017-09-21T14:22:28Z)); 


I am working on a news app and I get the date and time from the json response in this pattern (2017-09-23T14:22:28Z).

How can I convert this date and time into something like that (5 seconds ago, 10 minutes ago, 3 hours ago, etc)? And after 24 hours past, it should then display the normal date of that particular news headline (2017-09-23).

解决方案

Joda-Time is pretty nice for this.

compile 'joda-time:joda-time:2.9.9'


String dateTime = "2017-09-21T14:22:28Z";
    DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
    DateTime myBirthDate = format.parseDateTime(dateTime);
    DateTime now = new DateTime();
    Period period = new Period(myBirthDate, now);
    PeriodFormatter formatter = new PeriodFormatterBuilder()
            .appendSeconds().appendSuffix(" Sekonds ago\n")
            .appendMinutes().appendSuffix(" Minutes ago\n")
            .appendHours().appendSuffix(" Hours ago\n")
            .appendDays().appendSuffix(" Days ago\n")
            .appendWeeks().appendSuffix(" Weeks ago\n")
            .appendMonths().appendSuffix(" Months ago\n")
            .appendYears().appendSuffix(" Years ago\n")
            .printZeroNever()
            .toFormatter();
    if (period.getDays()<1) {
        String result = formatter.print(period);
        System.out.println(result);
    } else {
        DateTimeFormatter format24hMore = DateTimeFormat.forPattern("yyyy-MM-dd");
        String result = format24hMore.print(myBirthDate);
        System.out.println(result);
    }

EDIT Create Method

public String convertDate(String date) {

    String result = "";
    DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
    DateTime from = format.parseDateTime(date);
    DateTime now = new DateTime();
    Period period = new Period(from, now);
    PeriodFormatter formatter = new PeriodFormatterBuilder()
            .appendSeconds().appendSuffix(" Sekonds ago\n")
            .appendMinutes().appendSuffix(" Minutes ago\n")
            .appendHours().appendSuffix(" Hours ago\n")
            .appendDays().appendSuffix(" Days ago\n")
            .appendWeeks().appendSuffix(" Weeks ago\n")
            .appendMonths().appendSuffix(" Months ago\n")
            .appendYears().appendSuffix(" Years ago\n")
            .printZeroNever()
            .toFormatter();
    if (period.getDays()<1) {
        result = formatter.print(period);
    } else {
        DateTimeFormatter format24hMore = DateTimeFormat.forPattern("yyyy-MM-dd");
        result = format24hMore.print(from);

    }
    return result;
}

and you can call this Method

System.out.println(convertDate("2017-09-21T14:22:28Z"));

这篇关于日期和时间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆