Here you can find the source of getStringFormatYYYYMMddWithHyphens(Date aDate)
Parameter | Description |
---|---|
aDate | a parameter |
public static String getStringFormatYYYYMMddWithHyphens(Date aDate)
//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; } }