Android calculating minutes/hours/days from point in time

12,673

Solution 1

long milliseconds = date.getTime() - new Date().getTime();

that will run the calculation based on the difference between the date of the tweet and now.

At the moment, you are basing your calculation on date.getTime() which is the number of milliseconds since midnight on 1-Jan-1970. Because you also introduces modulos, your current function gives you the time elapsed between midnight UTC and the tweet.

Solution 2

Use DateUtils. it is fully localized and does what you want to do in a human-friendly manner.

Here is an example for getting a relative time span expression that is automatically localized:

long time = ... // The Twitter post time stamp
long now = System.currentTimeMillis();
CharSequence relativeTimeStr = DateUtils.getRelativeTimeSpanString(time, 
    now, DateUtils.SECOND_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE);

It produces output such as "10 seconds ago" or "in 5 minutes".

Solution 3

You can modify the input variable "timeAtMiliseconds", for my example was at format of date was at Miliseconds.

private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat formatterYear = new SimpleDateFormat("MM/dd/yyyy");

public static String parseDate(@NotNull Long timeAtMiliseconds) {
    timeAtMiliseconds *= 1000L; //Check if this is unnecessary for your use

    if (timeAtMiliseconds == 0) {
        return "";
    }

    //API.log("Day Ago "+dayago);
    String result = "now";
    String dataSot = formatter.format(new Date());
    Calendar calendar = Calendar.getInstance();

    long dayagolong = timeAtMiliseconds;
    calendar.setTimeInMillis(dayagolong);
    String agoformater = formatter.format(calendar.getTime());

    Date CurrentDate = null;
    Date CreateDate = null;

    try {
        CurrentDate = formatter.parse(dataSot);
        CreateDate = formatter.parse(agoformater);

        long different = Math.abs(CurrentDate.getTime() - CreateDate.getTime());

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        if (elapsedDays == 0) {
            if (elapsedHours == 0) {
                if (elapsedMinutes == 0) {
                    if (elapsedSeconds < 0) {
                        return "0" + " s";
                    } else {
                        if (elapsedSeconds > 0 && elapsedSeconds < 59) {
                            return "now";
                        }
                    }
                } else {
                    return String.valueOf(elapsedMinutes) + "m ago";
                }
            } else {
                return String.valueOf(elapsedHours) + "h ago";
            }

        } else {
            if (elapsedDays <= 29) {
                return String.valueOf(elapsedDays) + "d ago";
            }
            if (elapsedDays > 29 && elapsedDays <= 58) {
                return "1Mth ago";
            }
            if (elapsedDays > 58 && elapsedDays <= 87) {
                return "2Mth ago";
            }
            if (elapsedDays > 87 && elapsedDays <= 116) {
                return "3Mth ago";
            }
            if (elapsedDays > 116 && elapsedDays <= 145) {
                return "4Mth ago";
            }
            if (elapsedDays > 145 && elapsedDays <= 174) {
                return "5Mth ago";
            }
            if (elapsedDays > 174 && elapsedDays <= 203) {
                return "6Mth ago";
            }
            if (elapsedDays > 203 && elapsedDays <= 232) {
                return "7Mth ago";
            }
            if (elapsedDays > 232 && elapsedDays <= 261) {
                return "8Mth ago";
            }
            if (elapsedDays > 261 && elapsedDays <= 290) {
                return "9Mth ago";
            }
            if (elapsedDays > 290 && elapsedDays <= 319) {
                return "10Mth ago";
            }
            if (elapsedDays > 319 && elapsedDays <= 348) {
                return "11Mth ago";
            }
            if (elapsedDays > 348 && elapsedDays <= 360) {
                return "12Mth ago";
            }

            if (elapsedDays > 360 && elapsedDays <= 720) {
                return "1 year ago";
            }

            if (elapsedDays > 720) {
                Calendar calendarYear = Calendar.getInstance();
                calendarYear.setTimeInMillis(dayagolong);
                return formatterYear.format(calendarYear.getTime()) + "";
            }

        }

    } catch (java.text.ParseException e) {
        e.printStackTrace();
    }
    return result;
}

      //USAGE
    Log.d("TAG Pretty date: ", parseDate(System.currentTimeMillis()));
Share:
12,673
Patrick
Author by

Patrick

Updated on June 30, 2022

Comments

  • Patrick
    Patrick almost 2 years

    I am parsing twitters and i want to display how long ago it was since the twitter was posted. But it doesn't seem to be calculating correctly. I got the formula from another post here on Stackoverflow and i tried to build the return statement from it.

    public static String getTwitterDate(Date date){
        long milliseconds = date.getTime();
        int minutes = (int) ((milliseconds / (1000*60)) % 60);
        int hours   = (int) ((milliseconds / (1000*60*60)) % 24);
    
        if (hours > 0){
            if (hours == 1)
                return "1 hour ago";
            else if (hours < 24)
                return String.valueOf(hours) + " hours ago";
            else
            {
                int days = (int)Math.ceil(hours % 24);
                if (days == 1)
                    return "1 day ago";
                else
                    return String.valueOf(days) + " days ago";
            }
        }
        else
        {
            if (minutes == 0)
                return "less than 1 minute ago";
            else if (minutes == 1)
                return "1 minute ago";
            else
                return String.valueOf(minutes) + " minutes ago";
        }
    }
    

    Parsing the twitter date/time with this (also from a post on Stackoverflow)

    public static Date parseTwitterDate(String date)
    {
        final String TWITTER = "EEE, dd MMM yyyy HH:mm:ss Z";
        SimpleDateFormat sf = new SimpleDateFormat(TWITTER, Locale.ENGLISH);
        sf.setLenient(true);
    
        try {
            return sf.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    

    Example of a twitter date: "created_at":"Sat, 30 Jun 2012 14:44:40 +0000",

    As far as i can tell, the twitters are getting parsed correctly but not calculated correctly (getTwitterDate). That returns 11 hours some times when its a difference of 4-5 hours.