Java SQL Date Get 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.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

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

    public static Date getDateBeforTwelveMonth() {
        String date = "";
        Calendar cla = Calendar.getInstance();
        cla.setTime(getCurrentDate());//  w  ww  . ja  v a 2  s  .com
        int year = cla.get(Calendar.YEAR) - 1;
        int month = cla.get(Calendar.MONTH) + 1;
        if (month > 9) {
            date = String.valueOf(year) + DATE_DIVISION + String.valueOf(month) + DATE_DIVISION + "01";
        } else {
            date = String.valueOf(year) + DATE_DIVISION + "0" + String.valueOf(month) + 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(DATE_PATTERN_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 = TIME_PATTERN_DEFAULT;

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

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

    public static Date parseDate(Date date, String pattern) {
        DateFormat df = new SimpleDateFormat(pattern);
        String dateStr = df.format(date);
        return parseDate(pattern, dateStr);
    }

    public static synchronized Date getTime(final long millis) {
        Date date = new Date();
        if (millis > 0) {
            date.setTime(millis);
        }
        return date;
    }

    public static String format(Date aTs_Datetime) {
        return format(aTs_Datetime, DATE_PATTERN_DEFAULT);
    }

    public static String format(Date aTs_Datetime, String as_Pattern) {
        if (aTs_Datetime == null || as_Pattern == null)
            return null;
        SimpleDateFormat dateFromat = new SimpleDateFormat();
        dateFromat.applyPattern(as_Pattern);

        return dateFromat.format(aTs_Datetime);
    }

    /**
     * @param aTs_Datetime
     * @param as_Pattern
     * @return
     */
    public static String format(Timestamp aTs_Datetime, String as_Pattern) {
        if (aTs_Datetime == null || as_Pattern == null)
            return null;
        SimpleDateFormat dateFromat = new SimpleDateFormat();
        dateFromat.applyPattern(as_Pattern);

        return dateFromat.format(aTs_Datetime);
    }
}

Related

  1. getDate(Timestamp ts)
  2. getDateAddMonth(java.sql.Date date, int months)
  3. getDateAsLong(ResultSet rset, Enum field)
  4. getDateAsLong(Timestamp date)
  5. getDateBefore(Date date, Integer before)
  6. getDateByNum(String dateNum)
  7. getDateByYearAndMonth(int year, int month)
  8. getDateDiscrepancy(String startDate, String endDate)
  9. getDateEnd(Date date)