Java Yesterday getYesterday()

Here you can find the source of getYesterday()

Description

get Yesterday

License

Apache License

Declaration

public static String getYesterday() 

Method Source Code


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

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;

public class Main {
    public static String getYesterday() {
        return getDate(-1);
    }/*from w  w w.  ja v a2s  .co  m*/

    public static String getDate(int days) {
        Calendar currentTime = Calendar.getInstance();
        currentTime.add(Calendar.DATE, days);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(currentTime.getTime());
    }

    public static Date getDate(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    public static Date getDate(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
}

Related

  1. getYesterday()
  2. getYesterday()
  3. getYesterday()
  4. getYesterday()
  5. getYesterday()
  6. getYesterDay()