Android Long to Date Convert formatPublishDaysAgo(long timestamp)

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

Description

format Publish Days Ago

License

Open Source License

Declaration

public static String formatPublishDaysAgo(long timestamp) 

Method Source Code

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

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

public class Main {
    public static String formatPublishDaysAgo(long timestamp) {
        long days = daysSinceTimestamp(timestamp);
        String dayString;/*from ww w . j  ava 2  s .  c  o m*/

        if (days == Long.MAX_VALUE) {
            dayString = "Never Published";
        } else {
            if (days < 0) {
                if (days == -1) {
                    dayString = new SimpleDateFormat("'Tomorrow at' HH:mm",
                            Locale.US).format(new Date(timestamp));
                } else {
                    dayString = new SimpleDateFormat("'In " + (-days)
                            + " days at' HH:mm", Locale.US)
                            .format(new Date(timestamp));
                }
            } else if (days == 0) {
                dayString = new SimpleDateFormat("'Today at' HH:mm",
                        Locale.US).format(new Date(timestamp));
            } else if (days == 1) {
                dayString = "Yesterday";
            } else {
                dayString = days + " days ago";
            }
        }
        return dayString;
    }

    /**
     * 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. daysSinceTimestamp(long timestamp)
  2. encodeDate(long date)
  3. format(String aFormat, long aDate)
  4. formatDuration(Context context, long millis)
  5. formatDurationShort(Context context, long millis)
  6. formatTime(final long time)
  7. formatToCountdown(long millis)
  8. fullFromUtc(long milliseconds)
  9. generateFileName(long sysTime)