Is date yesterday - Android java.util

Android examples for java.util:Day

Description

Is date yesterday

Demo Code

import android.text.format.DateUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main{

    public static String getStringDate(long longDate) {
        Date dates = new Date(longDate);
        SimpleDateFormat format = null;
        if (DateUtils.isToday(longDate)) {
            format = new SimpleDateFormat("HH:mm:ss");
            return format.format(dates.getTime());
        }//from w w  w  .  ja v a 2 s  . c o m

        Calendar cale = Calendar.getInstance();
        cale.setTime(dates);
        Calendar currentCale = Calendar.getInstance();
        int days = cale.get(Calendar.DAY_OF_MONTH);
        int currentCaleDays = currentCale.get(Calendar.DAY_OF_MONTH);
        if (currentCaleDays - 1 == days) {
            format = new SimpleDateFormat("HH:mm:ss");
            return "yesterday  " + format.format(dates);
        }

        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(dates.getTime());
    }

}

Related Tutorials