5

我正在解析推特,我想显示自推特发布以来的时间。但它似乎计算不正确。我从 Stackoverflow 上的另一篇文章中获得了公式,并尝试从中构建返回语句。

public static String getTwitterDate(Date date){
    long milliseconds = date.getTime();
    int minutes = (int) ((milliseconds / (1000*60)) % 60);
    int hours   = (int) ((milliseconds / (1000*60*60)) % 24);

    if (hours > 0){
        if (hours == 1)
            return "1 hour ago";
        else if (hours < 24)
            return String.valueOf(hours) + " hours ago";
        else
        {
            int days = (int)Math.ceil(hours % 24);
            if (days == 1)
                return "1 day ago";
            else
                return String.valueOf(days) + " days ago";
        }
    }
    else
    {
        if (minutes == 0)
            return "less than 1 minute ago";
        else if (minutes == 1)
            return "1 minute ago";
        else
            return String.valueOf(minutes) + " minutes ago";
    }
}

用这个解析 twitter 日期/时间(也来自 Stackoverflow 上的帖子)

public static Date parseTwitterDate(String date)
{
    final String TWITTER = "EEE, dd MMM yyyy HH:mm:ss Z";
    SimpleDateFormat sf = new SimpleDateFormat(TWITTER, Locale.ENGLISH);
    sf.setLenient(true);

    try {
        return sf.parse(date);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

推特日期示例:"created_at":"Sat, 30 Jun 2012 14:44:40 +0000",

据我所知,推特被正确解析但计算不正确(getTwitterDate)。当相差 4-5 小时时,有时会返回 11 小时。

4

5 回答 5

12

使用DateUtils。它已完全本地化,并以人性化的方式完成您想做的事情。

这是获取自动本地化的相对时间跨度表达式的示例:

long time = ... // The Twitter post time stamp
long now = System.currentTimeMillis();
CharSequence relativeTimeStr = DateUtils.getRelativeTimeSpanString(time, 
    now, DateUtils.SECOND_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);

它产生诸如“10 秒前”或“5 分钟内”之类的输出。

于 2012-06-30T14:59:45.663 回答
5

long milliseconds = date.getTime() - new Date().getTime();

这将根据推文日期和现在之间的差异运行计算。

目前,您的计算基于date.getTime()自 1970 年 1 月 1 日午夜以来的毫秒数。因为您还引入了模数,所以您当前的函数为您提供了午夜 UTC 和推文之间经过的时间。

于 2012-06-30T15:02:07.513 回答
4

您可以修改输入变量“timeAtMiliseconds”,因为我的示例是日期格式为毫秒。

private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat formatterYear = new SimpleDateFormat("MM/dd/yyyy");

public static String parseDate(@NotNull Long timeAtMiliseconds) {
    timeAtMiliseconds *= 1000L; //Check if this is unnecessary for your use

    if (timeAtMiliseconds == 0) {
        return "";
    }

    //API.log("Day Ago "+dayago);
    String result = "now";
    String dataSot = formatter.format(new Date());
    Calendar calendar = Calendar.getInstance();

    long dayagolong = timeAtMiliseconds;
    calendar.setTimeInMillis(dayagolong);
    String agoformater = formatter.format(calendar.getTime());

    Date CurrentDate = null;
    Date CreateDate = null;

    try {
        CurrentDate = formatter.parse(dataSot);
        CreateDate = formatter.parse(agoformater);

        long different = Math.abs(CurrentDate.getTime() - CreateDate.getTime());

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        if (elapsedDays == 0) {
            if (elapsedHours == 0) {
                if (elapsedMinutes == 0) {
                    if (elapsedSeconds < 0) {
                        return "0" + " s";
                    } else {
                        if (elapsedSeconds > 0 && elapsedSeconds < 59) {
                            return "now";
                        }
                    }
                } else {
                    return String.valueOf(elapsedMinutes) + "m ago";
                }
            } else {
                return String.valueOf(elapsedHours) + "h ago";
            }

        } else {
            if (elapsedDays <= 29) {
                return String.valueOf(elapsedDays) + "d ago";
            }
            if (elapsedDays > 29 && elapsedDays <= 58) {
                return "1Mth ago";
            }
            if (elapsedDays > 58 && elapsedDays <= 87) {
                return "2Mth ago";
            }
            if (elapsedDays > 87 && elapsedDays <= 116) {
                return "3Mth ago";
            }
            if (elapsedDays > 116 && elapsedDays <= 145) {
                return "4Mth ago";
            }
            if (elapsedDays > 145 && elapsedDays <= 174) {
                return "5Mth ago";
            }
            if (elapsedDays > 174 && elapsedDays <= 203) {
                return "6Mth ago";
            }
            if (elapsedDays > 203 && elapsedDays <= 232) {
                return "7Mth ago";
            }
            if (elapsedDays > 232 && elapsedDays <= 261) {
                return "8Mth ago";
            }
            if (elapsedDays > 261 && elapsedDays <= 290) {
                return "9Mth ago";
            }
            if (elapsedDays > 290 && elapsedDays <= 319) {
                return "10Mth ago";
            }
            if (elapsedDays > 319 && elapsedDays <= 348) {
                return "11Mth ago";
            }
            if (elapsedDays > 348 && elapsedDays <= 360) {
                return "12Mth ago";
            }

            if (elapsedDays > 360 && elapsedDays <= 720) {
                return "1 year ago";
            }

            if (elapsedDays > 720) {
                Calendar calendarYear = Calendar.getInstance();
                calendarYear.setTimeInMillis(dayagolong);
                return formatterYear.format(calendarYear.getTime()) + "";
            }

        }

    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
    return result;
}

      //USAGE
    Log.d("TAG Pretty date: ", parseDate(System.currentTimeMillis()));
于 2015-10-15T14:48:22.070 回答
0

现在一天 twitter 时间响应如下:Thu Nov 05 03:13:48 +0000 2015

我将它们转换为5 秒前/5 分钟前/5 小时前/5 天前以调用服装功能:

public void TwitterTimeDifferentitaion(String responseTime) {

SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
                    ParsePosition pos = new ParsePosition(0);
                    long then = formatter.parse(responseTime, pos).getTime();
                    long now = new Date().getTime();

                    long seconds = (now - then) / 1000;
                    long minutes = seconds / 60;
                    long hours = minutes / 60;
                    long days = hours / 24;

                    String friendly = null;
                    long num = 0;
                    if (days > 0) {
                        num = days;
                        friendly = days + " day";
                    }
                    else if (hours > 0) {
                        num = hours;
                        friendly = hours + " hour";
                    }
                    else if (minutes > 0) {
                        num = minutes;
                        friendly = minutes + " minute";
                    }
                    else {
                        num = seconds;
                        friendly = seconds + " second";
                    }
                    if (num > 1) {
                        friendly += "s";
                    }
                    createdAt = friendly + " ago";
                    System.out.println("TotalTime>>" + createdAt);

}  

每当响应格式更改比 SimpleDateFormat() 也更改和工作完美..!!!

于 2015-11-06T05:16:41.887 回答
-1

long daysDifference = calender01.getTime() - calender02.getTime();

                            if (TimeUnit.MILLISECONDS.toHours(daysDifference) <= 24) {
                                if (TimeUnit.MILLISECONDS.toHours(daysDifference) == 0) {
                                    long days = TimeUnit.MILLISECONDS.toMinutes(daysDifference);
                                    dayCounter.setText(days + " minute(s) more");
                                } else {
                                    long days = TimeUnit.MILLISECONDS.toHours(daysDifference);
                                    dayCounter.setText(days + " hours(s) more");
                                }
                            } else {
                                long days = TimeUnit.MILLISECONDS.toDays(daysDifference + 24 * 60 * 60 * 1000);
                                dayCounter.setText(days + " day(s) more");
                            }
于 2019-07-26T02:37:32.843 回答