Java Day of getYesday()

Here you can find the source of getYesday()

Description

get Yesday

License

Open Source License

Declaration

public static final String getYesday() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    private static String defaultDatePattern = "yyyy-MM-dd";
    public static SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static final String getYesday() {
        String strDate = getCurrenDateTime();
        Calendar cale = toCalendar(strDate);
        cale.add(Calendar.DAY_OF_YEAR, -1);
        return dateFormatterByPattern(cale.getTime(), "yyyy-MM-dd");

    }/*from  w w  w.ja v  a 2s  .co  m*/

    public static String getCurrenDateTime() {
        String dt = dateTimeFormatter.format(new Date());
        return dt;
    }

    private static final Calendar toCalendar(String strDate) {
        Calendar cale = null;
        try {
            Date thisDate = dateTimeFormatter.parse(strDate);
            cale = Calendar.getInstance();
            cale.setTime(thisDate);
        } catch (Exception e) {
            return null;
        }
        return cale;
    }

    public static String dateFormatterByPattern(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        if (date != null) {
            return sdf.format(date);
        } else {
            return sdf.format(new Date());
        }
    }

    public static String format(Date date) {
        return format(date, getDatePattern());
    }

    public static String format(Date date, String pattern) {
        String returnValue = "";

        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            returnValue = df.format(date);
        }

        return (returnValue);
    }

    public static Date parse(String strDate) throws ParseException {
        return parse(strDate, getDatePattern());
    }

    public static Date parse(String strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(strDate);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static Date parse(Date strDate, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        try {
            return df.parse(df.format(strDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDatePattern() {
        return defaultDatePattern;
    }
}

Related

  1. getTimestamp(String format, Integer daysInFuture)
  2. getTwoDay(String sj1, String sj2)
  3. getTwoDay(String sj1, String sj2)
  4. getTwoDay2String(String startDate, String endDate, Format format)
  5. getWorkingDay(Calendar d1, Calendar d2)
  6. getYYYY_MM_DD(int offsetDays)
  7. isBirthdayPartAvail(String number)
  8. isDiffIsBiggerThanMin(Date lastLogIn, Date now, Long daysL)
  9. isEndOfDay(GregorianCalendar cal1)