Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android in date write today yesterday 2 days ago like that

I need to print date like today,Yesterday,2 days ago like that for that i have done I am getting date like : String date1 = "Thu Nov 13 19:01:25 GMT+05:30 2014"; calling like 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;

    }

and this is another function to get yesterday/today i have use this Link

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;
        }
    }

But getting Error:

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)

On

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

I have used Joda-time.jar

like image 951
Android Avatar asked Nov 21 '14 07:11

Android


People also ask

How can I compare current date and time with another date and time in Android?

By Calendar Object:getTime(); Date date2 = calendar2. getTime(); Then compare both date using compareTo, before(date) or after(date).

How can I get data between two dates in Android?

DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd"); DateFormat df2 = new SimpleDateFormat("MMM dd"); String mydate = df2. format(df1. parse(first)); This way I get both dates from first and second String.


2 Answers

You can try this code: (delta is time in milliseconds)

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;
}
like image 61
Sarthak Mittal Avatar answered Nov 10 '22 11:11

Sarthak Mittal


for Android you can use the most simple way with Joda-Time-Android library:

Date yourTime = new Date();
DateTime dateTime = new DateTime(yourTime); //or simple DateTime.now()
final String result = DateUtils.getRelativeTimeSpanString(getContext(), dateTime);
like image 27
ultraon Avatar answered Nov 10 '22 09:11

ultraon