爪哇(安卓)转换SQLite的日期英寸×天前" [英] Java(Android) convert SQLite date to "x days ago"

查看:170
本文介绍了爪哇(安卓)转换SQLite的日期英寸×天前"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

String date = "2010-10-9 12:00:00";

我想分析该字符串,然后减去从当前日期/时间日期/时间,所以我可以输出类似于一个字符串为2天前。

I want to parse that string, then subtract that date/time from the current date/time so that I can output a string similar to "2 days ago".

推荐答案

这是我使用,扩展了Android的标准DateUtils一个辅助类。它有一个先进的逻辑,对于今天的时间戳,这将显示几秒钟或几分钟或几小时,而对于其他的时间戳,将显示日期

This is a helper class I'm using, extending the standard DateUtils of Android. It has an advanced logic, that for timestamps of today, it would display the seconds or minutes or hours, while for other timestamps it would display the date.

您可以调整的逻辑在 getTimeDiffString 方法您的需求。作为参数,你就解析了日期日期= formatter.parse(dateString)时间戳; ,你在获取上述code

You can adjust the logic to your needs in the getTimeDiffString method. As parameter, you would parse the timestamp of Date date = formatter.parse(dateString); that you're fetching in above code.

在code逻辑,符合了时间戳显示当你从Facebook或Twitter知道这一点。

The code logic complies with the 'timestamp display' as you know it from Facebook or Twitter.

public class DateTimeUtils extends DateUtils {

     private static String mTimestampLabelYesterday;
     private static String mTimestampLabelToday;
     private static String mTimestampLabelJustNow;
     private static String mTimestampLabelMinutesAgo;
     private static String mTimestampLabelHoursAgo;
     private static String mTimestampLabelHourAgo;

    /**
     * Singleton contructor, needed to get access to the application context & strings for i18n
     * @param context Context
     * @return DateTimeUtils singleton instanec
     * @throws Exception
     */
     public static DateTimeUtils getInstance(Context context) {
         mCtx = context;
         if (instance == null) {
             instance = new DateTimeUtils();
             mTimestampLabelYesterday = context.getResources().getString(R.string.WidgetProvider_timestamp_yesterday);
             mTimestampLabelToday = context.getResources().getString(R.string.WidgetProvider_timestamp_today);
             mTimestampLabelJustNow = context.getResources().getString(R.string.WidgetProvider_timestamp_just_now);
             mTimestampLabelMinutesAgo = context.getResources().getString(R.string.WidgetProvider_timestamp_minutes_ago);
             mTimestampLabelHoursAgo = context.getResources().getString(R.string.WidgetProvider_timestamp_hours_ago);
             mTimestampLabelHourAgo = context.getResources().getString(R.string.WidgetProvider_timestamp_hour_ago);
         }
         return instance;
     }

    /**
     * Checks if the given date is yesterday.
     *
     * @param date - Date to check.
     * @return TRUE if the date is yesterday, FALSE otherwise.
     */
    public static boolean isYesterday(long date) {

        final Calendar currentDate = Calendar.getInstance();
        currentDate.setTimeInMillis(date);

        final Calendar yesterdayDate = Calendar.getInstance();
        yesterdayDate.add(Calendar.DATE, -1);

        return yesterdayDate.get(Calendar.YEAR) == currentDate.get(Calendar.YEAR) && yesterdayDate.get(Calendar.DAY_OF_YEAR) == currentDate.get(Calendar.DAY_OF_YEAR);
    }

    public static String[] weekdays = new DateFormatSymbols().getWeekdays(); // get day names
    public static final long millisInADay = 1000 * 60 * 60 * 24;


    ...

    /**
     * Displays a user-friendly date difference string
     * @param timedate Timestamp to format as date difference from now
     * @return Friendly-formatted date diff string
     */
    public String getTimeDiffString(long timedate) {
        Calendar startDateTime = Calendar.getInstance();
        Calendar endDateTime = Calendar.getInstance();
        endDateTime.setTimeInMillis(timedate);
        long milliseconds1 = startDateTime.getTimeInMillis();
        long milliseconds2 = endDateTime.getTimeInMillis();
        long diff = milliseconds1 - milliseconds2;

        long hours = diff / (60 * 60 * 1000);
        long minutes = diff / (60 * 1000);
        minutes = minutes - 60 * hours;
        long seconds = diff / (1000);

        boolean isToday = DateTimeUtils.isToday(timedate);
        boolean isYesterday = DateTimeUtils.isYesterday(timedate);

        if (hours > 0 && hours < 12) {
            return hours==1? String.format(mTimestampLabelHourAgo,hours) : String.format(mTimestampLabelHoursAgo,hours);
        } else if (hours <= 0) {
            if (minutes > 0)
                return String.format(mTimestampLabelMinutesAgo,minutes);
            else {
                return mTimestampLabelJustNow;
            }
        } else if (isToday) {
            return mTimestampLabelToday;
        } else if (isYesterday) {
            return mTimestampLabelYesterday;
        } else if (startDateTime.getTimeInMillis() - timedate < millisInADay * 6) {
            return weekdays[endDateTime.get(Calendar.DAY_OF_WEEK)];
        } else {
            return formatDateTime(mCtx, timedate, DateUtils.FORMAT_NUMERIC_DATE);
        }
    }

} 

而strings.xml中认为:

while strings.xml holds:

<string name="WidgetProvider_timestamp_today">Today</string>
<string name="WidgetProvider_timestamp_yesterday">Yesterday</string>
<string name="WidgetProvider_timestamp_hour_ago">%s hour ago</string>
<string name="WidgetProvider_timestamp_hours_ago">%s hours ago</string>
<string name="WidgetProvider_timestamp_minutes_ago">%s minutes ago</string>
<string name="WidgetProvider_timestamp_just_now">Just now</string>

这篇关于爪哇(安卓)转换SQLite的日期英寸×天前&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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