Java Day getDay(String aDate)

Here you can find the source of getDay(String aDate)

Description

get Day

License

Open Source License

Declaration

public static int getDay(String aDate) 

Method Source Code


//package com.java2s;
import java.text.ParseException;
import java.text.SimpleDateFormat;

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

public class Main {
    public static int getDay(String aDate) {
        int myDay = -1;
        if (isValidObjectModelDate(aDate)) {
            Integer Day = new Integer(aDate.substring(8));
            myDay = Day.intValue();/*  w  w w .  jav a2s .  co  m*/
        }
        return myDay;
    }

    /**
     *
     * @param aDate
     * @return
     *
     */
    /*==============================================================*/
    public static boolean isValidObjectModelDate(String aDate) {
        if (aDate == null) {
            return true;
        }

        if (aDate.equals("")) {
            return true;
        }

        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");

        if (aDate.length() != 10) {
            return false;
        }

        if (!(aDate.substring(4, 5).equals("-")) || !(aDate.substring(7, 8).equals("-"))) {
            return false;
        }

        try {
            Date myDate = myFormatter.parse(aDate);
        } catch (ParseException myParseException) {
            return false;
        }

        //YYYY-MM-DD
        //0123456789
        String year = aDate.substring(0, 4);
        Integer Year = new Integer(year);
        int myYear = Year.intValue();

        String month = aDate.substring(5, 7);
        Integer Month = new Integer(month);
        int myMonth = Month.intValue();

        String day = aDate.substring(8);
        Integer Day = new Integer(day);
        int myDay = Day.intValue();

        if (isValidObjectModelYear(myYear)) {
            if (isValidObjectModelMonth(myMonth)) {
                if (isValidObjectModelDay(myYear, myMonth, myDay)) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     *
     * @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;
    }

    /**
     *
     * @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;
    }
}

Related

  1. getDay(Date date)
  2. getDay(Date date)
  3. getDay(Date date, HashMap date2day)
  4. getDay(java.util.Date date)
  5. getDay(SimpleDateFormat df, String dateStr)
  6. GetDay(String agmipDate)
  7. getDAY(String strDate)
  8. getDay(String string)
  9. getDay(String tempdat, Locale locale)