需要时间差与字符串类似" A Min ago"或者"一小时前"

时间:2014-07-16 12:10:53

标签: java android

我是Android开发的新手。

我需要一个帮助来将当前时间转换为一个静态时间。

感谢您的帮助。

我有一个这样的字符串

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

我想将其转换为,这意味着我当前时间与created_at字符串之间存在差异。

23 mins ago // Example

谢谢,

Dharmik

2 个答案:

答案 0 :(得分:4)

只需使用我创建的以下实用程序类,并在其构造函数中传递两个日期对象。随后使用 getDifferenceString()方法获取相同的内容。

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

    }

答案 1 :(得分:1)

它很简单做这样的事情(注意我没有安装java等我只是在我的ipad上的Note中键入它,所以我不确定它是否有效但它应该是这样的):

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)

我希望有帮助:)