Java Yesterday getYesterday()

Here you can find the source of getYesterday()

Description

get Yesterday

License

Apache License

Declaration

public static Date getYesterday() throws ParseException 

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 Date getYesterday() throws ParseException {
        return addDay(getDate("yyyy-MM-dd"), -1);
    }/*from   w  w w  . j a  v a 2  s  .  c om*/

    public static String getYesterDay() {
        return getYesterdayOrTomorrow(getNowDate(), -1);
    }

    public static String addDay(String beginDateStr, int adddaycount) {
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
                "yyyy-MM-dd");
        try {
            Date beginDate = format.parse(beginDateStr);
            Calendar cal = Calendar.getInstance();
            cal.setTime(beginDate);
            cal.add(Calendar.DATE, adddaycount);
            Date enddate = cal.getTime();
            return format.format(enddate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date addDay(Date beginDate, int addcount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(beginDate);
        cal.add(Calendar.DAY_OF_YEAR, addcount);
        Date enddate = cal.getTime();
        return enddate;
    }

    public static Date getDate() {
        try {
            return getDate("yyyy-MM-dd");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDate(String format) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = new Date(System.currentTimeMillis());
        return convertStringToDate(df.format(date), format);
    }

    @SuppressWarnings("deprecation")
    public static String getYesterdayOrTomorrow(String nowDate, int con) {
        String ntime = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = df.parse(nowDate);
            Date endDate = null;
            if (con == -1) {
                endDate = new Date(startDate.getTime() - (long) 24
                        * (long) 3600 * (long) 1000);
            } else {
                endDate = new Date(startDate.getTime() + (long) 24
                        * (long) 3600 * (long) 1000);
            }
            ntime = endDate.getYear() + 1900 + "-"
                    + (endDate.getMonth() + 1) + "-" + endDate.getDate();
        } catch (Exception e) {
            //System.out.println(e);   
        }
        return ntime;
    }

    @SuppressWarnings("deprecation")
    public static String getNowDate() {
        Date date = new Date();
        int nowMonth = date.getMonth() + 1;
        int nowYear = date.getYear() + 1900;
        int day = date.getDate();
        String startTime = nowYear + "-" + nowMonth + "-" + day;
        return startTime;
    }

    public static Date getTime() {
        try {
            return getDate("yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date convertStringToDate(String time, String format)
            throws ParseException {
        if (format == null) {
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(time);
    }

    public static String getYear() {
        return getDateString("yyyy");
    }

    public static String getDateString() {
        return getDateString("yyyy-MM-dd");
    }

    public static String getDateString(int after) {
        return addDay(getDateString("yyyy-MM-dd"), after);
    }

    public static String getDateString(String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = new Date(System.currentTimeMillis());
        String dateStr = df.format(date);
        return dateStr;
    }
}

Related

  1. getYesterday()
  2. getYesterday()
  3. getYesterday()
  4. getYesterday()