-2

I need an internationalized string which describes a comparison between a past or future Date object and the current date. So something like..

  • 45 minutes ago.
  • 3 days, 6 hours ago.
  • 3 hours left.
  • 6 days left.

I have looked at DateUtils class which has a getRelativeTimeSpanString(), but this shows time in formats like..

  • in 2 days.
  • on March 6th.
  • in 3 hours.

Are there any alternative libraries that someone has come across which supports this type of thing, ideally internationalized as well.

Eurig Jones
  • 8,226
  • 7
  • 52
  • 74

1 Answers1

2

Have a look at this code . this will help you .

private  String getPostDuration(String str_date) {
            // TODO Auto-generated method stub
            try {
                //String str_date = "10-Aug-2011 21:20:00";
                SimpleDateFormat formatter;
                Date date;
                //formatter = new SimpleDateFormat("dd/MMM/yyyy hh:mm:ss");
                formatter = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");

                date = (Date) formatter.parse(str_date);
                //System.out.println("Today is " + date);
                //System.out.println( date);

                Date currentDate=new Date();
                //System.out.println(currentDate);


                long diff = currentDate.getTime() - date.getTime();

//              System.out.println("The 21st century (up to " + today + ") is "
//                  + (diff / (1000 * 60 * 60 * 24)) + " days old.");



                if ((diff / (1000 * 60 * 60 * 24)>0)) {
                    // System.out.println((diff / (1000 * 60 * 60 * 24)) + " days ago");
                     return (diff / (1000 * 60 * 60 * 24)) + " days old.";
                }
                else if((diff / (1000 * 60 * 60)>0)){
                    //System.out.println((diff / (1000 * 60 * 60)) + " hours ago");
                    return (diff / (1000 * 60 * 60 )) + " hours ago";
                }
                else if ((diff / (1000 * 60)>0)) 
                    {
                    //System.out.println((diff / (1000 * 60)) + " minutes ago");
                    return (diff / (1000 * 60  )) + " minutes ago";
                }
                else// if ((diff / (1000)>0)) 
                {
                    //System.out.println((diff / (1000)) + "less than a minute ago");
                    return  "less than a minute ago";
                }

            } catch (ParseException e) {
                //System.out.println("Exception :" + e);
                return "";
            }
        }
itsrajesh4uguys
  • 4,610
  • 3
  • 20
  • 31