Java Yesterday getYesterday(Date date, Boolean isBefore, int interval)

Here you can find the source of getYesterday(Date date, Boolean isBefore, int interval)

Description

get Yesterday

License

Apache License

Declaration

public static Date getYesterday(Date date, Boolean isBefore, int interval) 

Method Source Code

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    public static final String YMD_HMS = "yyyy-MM-dd HH:mm:ss";

    public static Date getYesterday(Date date, Boolean isBefore, int interval) {
        Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
        calendar.setTime(date);/*  w  w  w  . j a v  a 2  s . c om*/
        calendar.add(Calendar.DATE, isBefore ? -interval : +interval);
        return str2Date(date2Str(calendar.getTime()));
    }

    public static Date str2Date(String str) {
        Date d = null;
        SimpleDateFormat sdf = new SimpleDateFormat(YMD_HMS);
        try {
            d = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    public static Date str2Date(String str, String pattern) {
        Date d = null;
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            d = sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;
    }

    public static String date2Str(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YMD_HMS);
        return sdf.format(date);
    }

    public static String date2Str(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
}

Related

  1. getYesterday()
  2. getYesterday()
  3. getYesterDay()
  4. getYesterday()
  5. getYesterday(Date date)
  6. getYesterDay(String pattern)
  7. getYesterday6AM()
  8. getYesterdayBegin()
  9. getYesterdayCompactTime()