-2

I have the list of date types. My requirement here is to format the date in ascending order in the ArrayList<String> and need an utility method to get the string like "1 day ago", "2 days ago" & "40 days ago" like that. From the below test data.

2017-03-31T19:56:06.733Z
2017-03-31T19:55:38.227Z
2017-04-25T18:01:26.069Z
2017-04-25T17:57:49.656Z
2017-04-25T17:59:18.867Z
halfer
  • 19,824
  • 17
  • 99
  • 186
Takermania
  • 1,345
  • 2
  • 12
  • 20
  • 4
    You don't need to be a hardcore technical guy to research and find some [examples](http://stackoverflow.com/questions/1770010/how-do-i-measure-time-elapsed-in-java) and make an attempt to solve your issue from there. – sham Apr 27 '17 at 16:28
  • 1
    Arranging the values in an `ArrayList` and/or sorting that list, is not "formatting". Loading them as `Instant` objects is "parsing". Sorting has nothing to do with format/parse. Also, how do you define "days ago"? E.g. if it is not 1 minute past midnight, and given date is 1 minute before midnight, i.e. 2 minutes ago, is that to be considered "1 day ago"? Or is "1 day ago" something like `>= 24 hours`? Anyway, I can't seem to figure out what your *question* is. – Andreas Apr 27 '17 at 16:48

2 Answers2

0

It worked for me Guys!! You can reuse this... Direct copy paste will work..

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class TesterClass {



public static int getDaysLeft(String date) {


    final Date systemDate = new Date();
    final Calendar couponDateCalenderInstance = Calendar.getInstance();
    final Calendar systemDateCalenderInstance = Calendar.getInstance();


    int days;
    try {
             Date tempDate;

            final DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
            tempDate = dateformat.parse(date);
            couponDateCalenderInstance.setTime(tempDate);
            systemDateCalenderInstance.setTime(systemDate);
            final long diff = couponDateCalenderInstance.getTimeInMillis() -
                    systemDateCalenderInstance.getTimeInMillis();
            days = (int) (diff / (24 * 60 * 60 * 1000));
    } catch (Exception e) {
        days = -1;
        System.out.println(e);
    }
    return days;
}

public static void main(String[] args) {

       String testData =  "2017-03-31T19:56:06.733Z";

       String parsedString = testData.substring(0,10);
       System.out.println(parsedString);

       System.out.println("Days Left " + TesterClass.getDaysLeft(parsedString) );
}

}
Takermania
  • 1,345
  • 2
  • 12
  • 20
0

Actually to get time in millisecond from those Strings you can:

public static long getTimeInMillis(String rawDate){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss.SSS");
        try {
            Date dateTime = dateFormat.parse(rawDate);
            long millis = dateTime.getTime();
            return millis;
        } catch (Exception e){
            e.printStackTrace();
            Log.e("DateParser", e.getMessage(), e);
            return 0;
        }
    }

And to convert it to relative time you can:

public static String getTimeDifference(long time) {
        if (time < 1000000000000L) {
            // if timestamp given in seconds, convert to millis
            time *= 1000;
        }

        long now = System.currentTimeMillis();
        if (time > now || time <= 0) {
            return null;
        }

        // TODO: localize
        final long diff = now - time;
        if (diff < MINUTE_MILLIS) {
            return "just now";
        } else if (diff < 2 * MINUTE_MILLIS) {
            return "a minute ago";
        } else if (diff < 50 * MINUTE_MILLIS) {
            return diff / MINUTE_MILLIS + " minutes ago";
        } else if (diff < 90 * MINUTE_MILLIS) {
            return "an hour ago";
        } else if (diff < 24 * HOUR_MILLIS) {
            return diff / HOUR_MILLIS + " hours ago";
        } else if (diff < 48 * HOUR_MILLIS) {
            return "yesterday";
        } else {
            return diff / DAY_MILLIS + " days ago";
        }
    }

So by calling getTimeDifference(getTimeInMillis(rawDate)) you will have a String showing Relative Time based on Current System Time..

Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36