Java Year Format getStringFormatYYYYMMddWithHyphens(Date aDate)

Here you can find the source of getStringFormatYYYYMMddWithHyphens(Date aDate)

Description

get String Format YYYYM Mdd With Hyphens

License

Open Source License

Parameter

Parameter Description
aDate a parameter

Declaration


public static String getStringFormatYYYYMMddWithHyphens(Date aDate) 

Method Source Code


//package com.java2s;

import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.GregorianCalendar;

public class Main {
    /**//from w w  w .  ja v a  2 s  .c om
     *
     */
    /*======================================================*/
    private static final String FIRST_DAY = "01";

    /**
     *
     * @param aDate
     * @return
     *
     */
    /*==================================================================*/
    public static String getStringFormatYYYYMMddWithHyphens(Date aDate) {
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");

        if (aDate != null) {
            String myFormattedDate = myFormatter.format(aDate);
            return myFormattedDate;
        }
        return null;
    }

    /**
     *
     * @param aYear
     * @param aMonth
     * @param aDay
     * @return
     *
     */
    /*==================================================================*/
    public static String getStringFormatYYYYMMddWithHyphens(String aYear, String aMonth, String aDay) {

        String myFormattedDate = null;
        boolean myValidDate = false;
        try {
            myValidDate = isValidObjectModelDay(Integer.parseInt(aYear), Integer.parseInt(aMonth),
                    Integer.parseInt(aDay));
            if (myValidDate) {
                StringBuffer myDate = new StringBuffer(10);
                myDate.append(aYear);
                myDate.append("-");
                if (aMonth.length() < 2) {
                    myDate.append("0");
                }
                myDate.append(aMonth);
                myDate.append("-");
                if (aDay.length() < 2) {
                    myDate.append("0");
                }
                myDate.append(aDay);
                myFormattedDate = myDate.toString();
            }

        } catch (NumberFormatException myException) {
            // Do nothing, just return null
        }

        return myFormattedDate;
    }

    /**
     *
     * @param aYear
     * @param aMonth
     * @return
     *
     */
    /*==================================================================*/
    public static String getStringFormatYYYYMMddWithHyphens(String aYear, String aMonth) {
        String myDay = FIRST_DAY;
        return getStringFormatYYYYMMddWithHyphens(aYear, aMonth, myDay);
    }

    /**
     *
     * @param aYear
     * @param aMonth
     * @param aDay
     *
     * @return boolean
     *
     */
    /*==============================================================*/
    public static boolean isValidObjectModelDay(int aYear, int aMonth, int aDay) {
        boolean result = false;

        result = isValidObjectModelYear(aYear);
        if (!result) {
            return result;
        }
        result = isValidObjectModelMonth(aMonth);
        if (!result) {
            return result;
        }

        //If they day is 0 or negative then it is an invalid date
        if (aDay <= 0) {
            return false;
        }

        switch (aMonth) {

        case 4:
        case 6:
        case 9:
        case 11:
            if (aDay > 30) {
                result = false;
            } else {
                result = true;
            }
            break;
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            if (aDay > 31) {
                result = false;
            } else {
                result = true;
            }
            break;
        case 2:
            GregorianCalendar gc = new GregorianCalendar();
            if (gc.isLeapYear(aYear)) {
                if (aDay > 29) {
                    result = false;
                } else {
                    result = true;
                }
            } else {
                if (aDay > 28) {
                    result = false;

                } else {
                    result = true;
                }
            }
            break;
        default:
            result = false;
        }
        return result;
    }

    /**
     *
     * @param aYear
     * @return boolean
     *
     */
    /*==============================================================*/

    public static boolean isValidObjectModelYear(int aYear) {
        boolean validYear = false;
        if (aYear > 1800) {
            validYear = true;
        }
        return validYear;
    }

    /**
     *
     * @param aMonth
     * @return boolean
     *
     */
    /*==============================================================*/
    public static boolean isValidObjectModelMonth(int aMonth) {
        boolean result = false;
        if (aMonth > 0 && aMonth < 13) {
            result = true;
        } else {
            result = false;
        }
        return result;
    }
}

Related

  1. getDDMMYYYYDate(Date date)
  2. getFirstDate(String yearMonthStr, String yearMonthFormat, String dateFormat)
  3. getFormatToDateSimple(String yyyyMMdd)
  4. getStartAndEndWeekOfMonth(int year, int month, int week, String format)
  5. getStringFormatMMYYWithoutHyphens(Date aDate)
  6. getTimeByYMD()
  7. getYear(SimpleDateFormat df, String dateStr)
  8. getYear(String date, String dateformat)
  9. getYear(String pFormattedDate)