0

I am new in Android Development.

I need one help to convert my current time with one static time.

Your help be appreciated.

I have one string like this

String created_at = "Wed Mar 03 19:37:35 +0000 2010";

I want to convert it like , which means difference between my current time and created_at string.

23 mins ago // Example

Thanks,

Dharmik

Dharmik Patel
  • 76
  • 1
  • 11
  • have you tried looking at joda library at http://www.joda.org/joda-time/ – Satya Jul 16 '14 at 12:13
  • Parse the `created_at` string into a date with a date-formatter (http://developer.android.com/reference/java/text/SimpleDateFormat.html), convert to milliseconds and substract it from the current time in millis. – Blacklight Jul 16 '14 at 12:18
  • You can use "Joda Time" library. It is clean and easy http://stackoverflow.com/questions/2179644/how-to-calculate-elapsed-time-from-now-with-joda-time – MiguelAngel_LV Jul 16 '14 at 12:52

2 Answers2

4

Just use the following utility class I've created and pass the two date objects in its constructor .Subsequently use the getDifferenceString() method to obtain the same.

 public class TimeDifference {
        int years;
        int months;
        int days;
        int hours;
        int minutes;
        int seconds;
        String differenceString;



        public TimeDifference(Date curdate, Date olddate) {

            float diff=curdate.getTime() - olddate.getTime();
            if (diff >= 0) {
                int yearDiff = Math.round( ( diff/ (365l*2592000000f))>=1?( diff/ (365l*2592000000f)):0);
                if (yearDiff > 0) {
                    years = yearDiff;
                    setDifferenceString(years + (years == 1 ? " year" : " years") + " ago");
                } else {
                    int monthDiff = Math.round((diff / 2592000000f)>=1?(diff / 2592000000f):0);
                    if (monthDiff > 0) {
                        if (monthDiff > 11)
                            monthDiff = 11;

                        months = monthDiff;
                        setDifferenceString(months + (months == 1 ? " month" : " months") + " ago");
                    } else {
                        int dayDiff = Math.round((diff / (86400000f))>=1?(diff / (86400000f)):0);
                        if (dayDiff > 0) {
                            days = dayDiff;
                            if(days==30)
                                days=29;
                            setDifferenceString(days + (days == 1 ? " day" : " days") + " ago");
                        } else {
                            int hourDiff = Math.round((diff / (3600000f))>=1?(diff / (3600000f)):0);
                            if (hourDiff > 0) {
                                hours = hourDiff;
                                setDifferenceString( hours + (hours == 1 ? " hour" : " hours") + " ago");
                            } else {
                                int minuteDiff = Math.round((diff / (60000f))>=1?(diff / (60000f)):0);
                                if (minuteDiff > 0) {
                                    minutes = minuteDiff;
                                    setDifferenceString(minutes + (minutes == 1 ? " minute" : " minutes") + " ago");
                                } else {
                                    int secondDiff =Math.round((diff / (1000f))>=1?(diff / (1000f)):0);
                                    if (secondDiff > 0)
                                        seconds = secondDiff;
                                    else
                                        seconds = 1;
                                    setDifferenceString(seconds + (seconds == 1 ? " second" : " seconds") + " ago");
                                }
                            }
                        }

                    }
                }

            }

        }
        public String getDifferenceString() {
            return differenceString;
        }

        public void setDifferenceString(String differenceString) {
            this.differenceString = differenceString;
        }
        public int getYears() {
            return years;
        }

        public void setYears(int years) {
            this.years = years;
        }

        public int getMonths() {
            return months;
        }

        public void setMonths(int months) {
            this.months = months;
        }

        public int getDays() {
            return days;
        }

        public void setDays(int days) {
            this.days = days;
        }

        public int getHours() {
            return hours;
        }

        public void setHours(int hours) {
            this.hours = hours;
        }

        public int getMinutes() {
            return minutes;
        }

        public void setMinutes(int minutes) {
            this.minutes = minutes;
        }

        public int getSeconds() {
            return seconds;
        }

        public void setSeconds(int seconds) {
            this.seconds = seconds;
        }

    }
humblerookie
  • 4,717
  • 4
  • 25
  • 40
1

its is simple do something like this ( Note I don't have java etc installed I have just typed it in Note on my ipad, so I am not sure if it works but it should be something like this) :

String dateString = "Wed Mar 03 19:37:35 2010";
    SimpleDateFormat dateFormat = new SimpleDateFormat("E M d hh:mm:ss y");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
// convert date to calnedar 
 Calendar previouseCal = Calendar.getInstance();
  previouseCal.setTime(convertedDate );

// then get the current time
Calendar currentCal = Calendar.getInstance();

// then get the diffrence 
long difference = currentCal.getTimeInMillis() - previouseCal.getTimeInMillis();

// if you need it in second  then 

int second = TimeUnit.MILLISECONDS.toSeconds(difference)

I hope that helps :)

Zardaloop
  • 1,594
  • 5
  • 22
  • 43