Android Long to Date Convert daysSinceTimestamp(long timestamp)

Here you can find the source of daysSinceTimestamp(long timestamp)

Description

Determine days difference since timestamp to current time if timestamp is equal to Long.MAX_VALUE then return Long.MAX_VALUE

License

Open Source License

Parameter

Parameter Description
timestamp the start milliseconds from which start to count days

Return

numbers of days, if negative indicates days in the future beyond passed timestamp

Declaration

public static long daysSinceTimestamp(long timestamp) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**/*from  w  w w .  ja v a  2  s  .com*/
     * Determine days difference since timestamp to current time
     * if timestamp is equal to Long.MAX_VALUE then return Long.MAX_VALUE
     *
     * @param timestamp the start milliseconds from which start to count days
     * @return numbers of days, if negative indicates days in the future beyond
     * passed timestamp
     */
    public static long daysSinceTimestamp(long timestamp) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long nowTime = cal.getTimeInMillis();
        long dayTime = 24 * 60 * 60 * 1000;
        long days;

        if (timestamp == Long.MAX_VALUE) {
            days = Long.MAX_VALUE;
        } else {
            cal = Calendar.getInstance();
            cal.setTime(new Date(timestamp));
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);

            long tsWithoutTime = cal.getTimeInMillis();
            long spanTime = nowTime - tsWithoutTime;
            days = spanTime / dayTime;
        }
        return days;
    }
}

Related

  1. convertToTimeStamp(long time)
  2. convertToTimeStamp(long time, boolean showSeconds)
  3. dateFromUnixTimestamp(final long unixTimestamp)
  4. dateFromUtc(long milliseconds)
  5. dateUtil(long date)
  6. encodeDate(long date)
  7. format(String aFormat, long aDate)
  8. formatDuration(Context context, long millis)
  9. formatDurationShort(Context context, long millis)