Android 在日期写今天昨天 2 天前这样

And*_*oid 7 android jodatime

我需要像今天、昨天、2 天前那样打印日期,因为我已经完成了我得到的日期就像:String date1 = "Thu Nov 13 19:01:25 GMT+05:30 2014"; 打电话str=get_userTime(date1);

private String get_userTime(String usertime) {
        Date d = null;

        // String datee = "Thu Nov 13 19:01:25 GMT+05:30 2014";
        String datee = usertime;
        SimpleDateFormat inputFormat = new SimpleDateFormat(
                "EE MMM dd HH:mm:ss zz yyy");

        try {
            d = inputFormat.parse(datee);
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SimpleDateFormat outputFormat1 = new SimpleDateFormat("MM/dd/yyyy");
        System.out.println(outputFormat1.format(d));
         op = outputFormat1.format(d);
        op=formatToYesterdayOrToday(op);
        return op;

    }
Run Code Online (Sandbox Code Playgroud)

这是昨天/今天获得的另一个功能,我使用了这个链接

public static String formatToYesterdayOrToday(String date) {
        date=op;
        DateTime dateTime = DateTimeFormat.forPattern("EEE hh:mma MMM d, yyyy")
                .parseDateTime(date);
        DateTime today = new DateTime();
        DateTime yesterday = today.minusDays(1);
        DateTimeFormatter timeFormatter = DateTimeFormat.forPattern("hh:mma");

        if (dateTime.toLocalDate().equals(today.toLocalDate())) {
            return "Today " + timeFormatter.print(dateTime);
        } else if (dateTime.toLocalDate().equals(yesterday.toLocalDate())) {
            return "Yesterday " + timeFormatter.print(dateTime);
        } else {
            return date;
        }
    }
Run Code Online (Sandbox Code Playgroud)

但得到错误:

11-21 13:12:10.626: E/AndroidRuntime(20654): java.lang.IllegalArgumentException: Invalid format: "11/21/2014"
11-21 13:12:10.626: E/AndroidRuntime(20654):    at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:871)
Run Code Online (Sandbox Code Playgroud)

DateTime dateTime = DateTimeFormat.forPattern("EEE hh:mma MMM d, yyyy")
                .parseDateTime(date); //In (formatToYesterdayOrToday())
Run Code Online (Sandbox Code Playgroud)

我用过 Joda-time.jar

Sar*_*tal 4

您可以尝试以下代码:(增量是以毫秒为单位的时间)

public static String getDisplayableTime(long delta)
{       
     long difference=0;
     Long mDate = java.lang.System.currentTimeMillis();     

     if(mDate > delta)
     {
         difference= mDate - delta;     
         final long seconds = difference/1000;
         final long minutes = seconds/60;
         final long hours = minutes/60;
         final long days = hours/24;
         final long months = days/31;
         final long years = days/365;

        if (seconds < 0)
        {
          return "not yet";
        }
        else if (seconds < 60)
        {
          return seconds == 1 ? "one second ago" : seconds + " seconds ago";
        }
        else if (seconds < 120)
        {
          return "a minute ago";
        }
        else if (seconds < 2700) // 45 * 60
        {
          return minutes + " minutes ago";
        }
        else if (seconds < 5400) // 90 * 60
        {
          return "an hour ago";
        }
        else if (seconds < 86400) // 24 * 60 * 60
        {
          return hours + " hours ago";
        }
        else if (seconds < 172800) // 48 * 60 * 60
        {
          return "yesterday";
        }
        else if (seconds < 2592000) // 30 * 24 * 60 * 60
        {
          return days + " days ago";
        }
        else if (seconds < 31104000) // 12 * 30 * 24 * 60 * 60
        {

          return months <= 1 ? "one month ago" : days + " months ago";
        }
        else
        {

          return years <= 1 ? "one year ago" : years + " years ago";
        }
    }
        return null;
}
Run Code Online (Sandbox Code Playgroud)