Java Yesterday yesterday()

Here you can find the source of yesterday()

Description

yesterday

License

Apache License

Declaration

public static Date yesterday() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    public static Date yesterday() {
        return toYesterday(today());
    }//  w w  w. j  a va2  s .c om

    public static Date toYesterday(Date date) {
        return add(date, Calendar.DAY_OF_YEAR, -1);
    }

    /**
     * @return
     */
    public static Date today() {
        return toDay(new Date());
    }

    public static Date toDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    private static Date add(Date date, int field, int value) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(date.getTime());
        cal.add(field, value);
        return cal.getTime();
    }
}

Related

  1. getYesterdayFormatString(String format)
  2. getYesterdaysDate()
  3. getYesterdayStartDate()
  4. getYesterdayString()
  5. isYesterday(Date date)
  6. yesterday()
  7. yesterday()
  8. yesterDay()
  9. yesterday()