get Yesterday - Java java.util

Java examples for java.util:Day

Description

get Yesterday

Demo Code


//package com.java2s;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    public static void main(String[] argv) throws Exception {
        Date date = new Date();
        System.out.println(getYesterday(date));
    }/*from   ww w  . j  a  v a  2 s. c o  m*/

    private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

    public static String getYesterday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - 1);
        date = cal.getTime();
        return sf.format(date);
    }
}

Related Tutorials