Androidメッセージボックスの表示日

   public static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
    public static SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public static SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("yyyy-MM-dd");
    public static TimeZone defaultTimeZone = TimeZone.getTimeZone("GMT+8");
    //时间String转化,默认传入的时间是东八区中国时间
    //这里只处理了三种字符串的情况,暂时没想到更优美的处理方式
    public static String turnDate(String dateString) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(simpleDateFormat.parse(dateString));
        } catch (ParseException e) {
            e.printStackTrace();
            try {
                simpleDateFormat1.setTimeZone(defaultTimeZone);
                calendar.setTime(simpleDateFormat1.parse(dateString));
            } catch (ParseException e1) {
                e1.printStackTrace();
                try {
                    simpleDateFormat2.setTimeZone(defaultTimeZone);
                    calendar.setTime(simpleDateFormat2.parse(dateString));
                } catch (ParseException e2) {
                    e2.printStackTrace();
                    simpleDateFormat3.setTimeZone(defaultTimeZone);
                    try {
                        calendar.setTime(simpleDateFormat3.parse(dateString));
                    } catch (ParseException e3) {
                        e3.printStackTrace();
                    }
                }
            }
        }
        Calendar now = Calendar.getInstance();

        int value1 = now.get(Calendar.YEAR);
        int value2 = calendar.get(Calendar.YEAR);
        if (value1 > value2) {
            return (value1 - value2) + "年前";
        } else if (value1 == value2) {
            value1 = now.get(Calendar.MONTH);
            value2 = calendar.get(Calendar.MONTH);
            if (value1 > value2) {
                return (value1 - value2) + "月前";
            } else if (value1 == value2) {
                value1 = now.get(Calendar.DATE);
                value2 = calendar.get(Calendar.DATE);
                if (value1 > value2) {
                    int day = value1 - value2;
                    if (day == 1) {
                        return "昨天";
                    } else if (day == 2) {
                        return "前天";
                    } else {
                        return day + "天前";
                    }
                } else if (value1 == value2) {
                    value1 = now.get(Calendar.HOUR_OF_DAY);
                    value2 = calendar.get(Calendar.HOUR_OF_DAY);
                    if (value1 > value2) {
                        return (value1 - value2) + "小时前";
                    } else if (value1 == value2) {
                        value1 = now.get(Calendar.MINUTE);
                        value2 = calendar.get(Calendar.MINUTE);
                        if (value1 > value2) {
                            return (value1 - value2) + "分钟前";
                        } else if (value1 == value2) {
                            return "现在";
                        }
                    }
                }
            }
        }
        return "";
    }


    //时间String从默认的东八区中国时区转化为本地默认时区的时间
    //这里只处理了三种字符串的情况,暂时没想到更优美的处理方式
    public static String turnToDefault(String dateString) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(simpleDateFormat.parse(dateString));
        } catch (ParseException e) {
            e.printStackTrace();
            try {
                simpleDateFormat1.setTimeZone(defaultTimeZone);
                calendar.setTime(simpleDateFormat1.parse(dateString));
            } catch (ParseException e1) {
                e1.printStackTrace();
                try {
                    simpleDateFormat2.setTimeZone(defaultTimeZone);
                    calendar.setTime(simpleDateFormat2.parse(dateString));
                } catch (ParseException e2) {
                    e2.printStackTrace();
                    simpleDateFormat3.setTimeZone(defaultTimeZone);
                    try {
                        calendar.setTime(simpleDateFormat3.parse(dateString));
                    } catch (ParseException e3) {
                        e3.printStackTrace();
                    }
                }
            }
        }
        simpleDateFormat2.setTimeZone(TimeZone.getDefault());
        return simpleDateFormat2.format(calendar.getTime());
    }

おすすめ

転載: blog.csdn.net/Ser_Bad/article/details/60763489