Android-将“日期转换为时间",但在48年前返回 [英] Android - Converting Date to Time ago but it is returning 48 years ago

查看:43
本文介绍了Android-将“日期转换为时间",但在48年前返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将日期转换为timeago.返回的内容:1小时前,1个月,2个月前,1年.

I am trying to convert a date to timeago. Something that returns: 1 hour ago, 1 month, 2 months ago, 1 year ago.

这是一个可从WordPress获取博客帖子的Android应用.

This is an android app that gets blog posts from WordPress.

但是,我的当前代码返回(对于每个帖子) 48年前.

However, my current code returns (for every post) 48 years ago.

我试图在互联网上找到答案,是的,那里的大多数答案都对我有所帮助,但是我仍然在48年前的每一篇帖子中都找到了答案.

I tried to find an answer on the internet and yes most of the answers out there helped me, but I still got 48 years ago for every post.

那里的大多数答案对我来说都行不通.

Most of the answers out there didn't work for me.

请注意,我没有使用Java8.很多答案都建议使用Java 8,但是我不能使用它.

Please note that I am not using Java 8. A lot of the answers recommended Java 8 but I can't use it.

public String parseDateToddMMyyyy(String time) {
    String inputPattern = "yyyy-MM-dd'T'HH:mm:ss";
    String outputPattern = "yyyy-MM-dd HH:mm:ss'Z'";
    SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
    SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);

    inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    outputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

    Date date = null;
    String str = null;

    try {
        date = inputFormat.parse(time);
        str = outputFormat.format(date);

        long dateInMillinSecond = date.getTime();

        str = toDuration(dateInMillinSecond);

        Log.e("Blog - ", "Date " + date);
        //output for the dare is Sat May 19 22:59:42 EDT 2018

        Log.e("Blog - ", "Str " + str);
        //output is 48 years ago

    } catch (ParseException e) {
        e.printStackTrace();
    }
    return str;
}

我还发现了这两种将毫秒转换为时间的方法,并且我认为这两种方法都能找到.因此,我认为问题在于日期.日期格式未转换为毫秒(不确定).

I also found these 2 methods that convert millisecond to time ago and I THINK that these two methods work finds. So, I think that the problem is the date. The date format is not converting into millisecond (Not sure).

public static final List<Long> times = Arrays.asList(
    TimeUnit.DAYS.toMillis(365),
    TimeUnit.DAYS.toMillis(30),
    TimeUnit.DAYS.toMillis(1),
    TimeUnit.HOURS.toMillis(1),
    TimeUnit.MINUTES.toMillis(1),
    TimeUnit.SECONDS.toMillis(1));

public static final List<String> timesString = 
    Arrays.asList("year","month","day","hour","minute","second");

public static String toDuration(long duration) {
    StringBuffer res = new StringBuffer();
    for(int i=0;i< times.size(); i++) {
        Long current = times.get(i);
        long temp = duration/current;
        if(temp>0) {
            res.append(temp).append(" ").append(timesString.get(i))
            .append(temp != 1 ? "s" : "").append(" ago");
            break;
        }
    }
    if("".equals(res.toString()))
        return "0 seconds ago";
    else
        return res.toString();
}

编辑

变量 long dateInMilliSecond 返回 1524430807000

推荐答案

我会看一下您的代码,但我绝对应该说, 48年前使我想起了1970年1月1日.向人性化功能发送null或0作为日期.

I will look at your code but I should definitely say that, 48 years ago reminds me 1 Jan 1970. Probably you're sending null or 0 as date to humanizer function.

审阅后

我认为问题在于,您正在计算日期的持续时间,而不是差异.如果要计算实际的日期之前,则应将时差发送到 toDuration 函数.

I think the problem is that, you're calculating duration of a date, not difference. If you want to calculate a real date ago, you should send time difference to toDuration function.

当前行为非常正常,因为您向 toDuration 函数发送了 xx-2018 日期,并且基本上返回了 48年前(从零开始)是1970年).您应该寄出差额.例如;

Current behavior is quite normal because you send a x-x-2018 date to toDuration function and it returns 48 years ago basically(since zero is 1970). You should send the difference. For example;

long difference = System.currentTimeMillis() - dateInMillisecond;
String humanizedDate = toDuration(difference);

这篇关于Android-将“日期转换为时间",但在48年前返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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