Java Yesterday yesterdayDate()

Here you can find the source of yesterdayDate()

Description

yesterday Date

License

Open Source License

Declaration

public static Date yesterdayDate() 

Method Source Code

//package com.java2s;

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

public class Main {

    public static Date yesterdayDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        return StringToDate(sdf.format(c.getTime()));
    }/*from   w w w.j av  a2  s  .  c  o  m*/

    public static Date StringToDate(String dateStr) {
        SimpleDateFormat sdf = null;
        Date date = null;
        if (dateStr.length() > 10) {
            if (dateStr.indexOf("T") > -1) {
                dateStr = dateStr.replace("T", " ");
            }
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        }
        try {
            date = sdf.parse(dateStr);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
            return null;
        }
        return date;
    }
}

Related

  1. yesterday()
  2. yesterday()
  3. yesterday()
  4. yesterday(Date date)
  5. yesterday(int daybefore, String pattern)
  6. yesterdayDate3()