Java Date Before getDateBeforTwelveMonth()

Here you can find the source of getDateBeforTwelveMonth()

Description

get Date Befor Twelve Month

License

Apache License

Declaration

public static Date getDateBeforTwelveMonth() 

Method Source Code

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

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

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

    public static Date getDateBeforTwelveMonth() {
        String date = "";
        Calendar cla = Calendar.getInstance();
        cla.setTime(getCurrentDate());//  w w  w  .  j av  a  2  s  . c  om
        int year = cla.get(Calendar.YEAR) - 1;
        int month = cla.get(Calendar.MONTH) + 1;
        if (month > 9) {
            date = String.valueOf(year) + C_DATE_DIVISION + String.valueOf(month) + C_DATE_DIVISION + "01";
        } else {
            date = String.valueOf(year) + C_DATE_DIVISION + "0" + String.valueOf(month) + C_DATE_DIVISION + "01";
        }

        Date dateBefore = parseDate(date);
        return dateBefore;
    }

    public static Date getCurrentDate() {
        Calendar cal = Calendar.getInstance();
        Date currDate = cal.getTime();

        return currDate;
    }

    /**
     * Parse a string and return a date value
     *
     * @param dateValue
     * @return
     * @throws Exception
     */
    public static Date parseDate(String dateValue) {
        return parseDate(C_DATE_PATTON_DEFAULT, dateValue);
    }

    /**
     * Parse a string and return the date value in the specified format
     *
     * @param strFormat
     * @param dateValue
     * @return
     * @throws ParseException
     * @throws Exception
     */
    public static Date parseDate(String strFormat, String dateValue) {
        if (dateValue == null)
            return null;

        if (strFormat == null)
            strFormat = C_TIME_PATTON_DEFAULT;

        SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
        Date newDate = null;

        try {
            newDate = dateFormat.parse(dateValue);
        } catch (Exception pe) {
            newDate = null;
        }

        return newDate;
    }
}

Related

  1. getDateBeforeOrAfter(int iDate)
  2. getDateBeforeOrAfterV2(int idx)
  3. getDateBeforeSomeMinutes(int minute, long timestamp)
  4. getDateBeforeTheDay(String day, int num)
  5. getDateBeforeToday(int num)
  6. getDateTimeAfterOfBeforeHour(Integer AfterOfBeforeHour)
  7. getDayBeforeOrAfter2(Date time, int days)
  8. getDayFormSomeDay(Date date, int n, boolean before)
  9. getDistanceMonthOfTwoDate(Date before, Date after)