Java Day getDayForDate(String dateToCheck, String pattern)

Here you can find the source of getDayForDate(String dateToCheck, String pattern)

Description

Parses a string into a date.

License

Open Source License

Parameter

Parameter Description
dateToCheck The date string to check.
pattern The pattern to use. <p>

Return

The day for the date or -1 on error.

Declaration

public static int getDayForDate(String dateToCheck, String pattern) 

Method Source Code


//package com.java2s;
/*/*from   w w  w . ja  v a2 s  .  c  om*/
 * DateHelper.java
 *
 * Copyright (c) 2004-2011 Gregory Kotsaftis
 * gregkotsaftis@yahoo.com
 * http://zeus-jscl.sourceforge.net/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Main {
    /**
     * Parses a string into a date. String should be in
     * <code>SimpleDateFormat</code> format. Returns only the day of the date or
     * -1 on error.
     * <p>
     * <b>NOTE:</b> only 'dd' is supported!
     * <p>
     * 
     * @param dateToCheck
     *            The date string to check.
     * @param pattern
     *            The pattern to use.
     *            <p>
     * @return The day for the date or -1 on error.
     */
    public static int getDayForDate(String dateToCheck, String pattern) {
        if (isDateValid(dateToCheck, pattern) && dateToCheck.length() == pattern.length()) {
            int index = pattern.indexOf("dd"); // only "dd" is supported
            if (index == -1) {
                return (-1);
            }

            String day_str = dateToCheck.substring(index, index + 2);
            int day = -1;
            try {
                Integer i = new Integer(day_str);
                day = i.intValue();
            } catch (Exception e) {
                // day is already -1
            }

            return (day);
        } else {
            return (-1);
        }
    }

    /**
     * Checks a string to see if it contains a valid date in
     * <code>SimpleDateFormat</code>.
     * <p>
     * 
     * @param dateToCheck
     *            The date string to check.
     * @param pattern
     *            The pattern to use.
     *            <p>
     * @return <code>true</code> if it contains a valid date in
     *         <code>SimpleDateFormat</code>.
     */
    public static boolean isDateValid(String dateToCheck, String pattern) {
        if (dateToCheck == null || pattern == null)
            return (false);

        boolean result = false;
        try {
            java.util.Date in = parseDate(dateToCheck, pattern);
            String out = dateToString(in, pattern);
            if (dateToCheck.compareTo(out) == 0) {
                result = true;
            }
        } catch (Exception e) {
            // result is already false
        }

        return (result);
    }

    /**
     * Parses a string into a date. String should be in
     * <code>SimpleDateFormat</code> format. e.g.
     * <code>java.util.Date d = parseDate(myDate, "dd/MM/yyyy");</code>
     * <p>
     * 
     * @param myDate
     *            The date string.
     * @param pattern
     *            The pattern to use.
     *            <p>
     * @return The <code>Date</code>.
     *         <p>
     * @throws ParseException
     */
    public static java.util.Date parseDate(String myDate, String pattern) throws ParseException {
        if (myDate == null || pattern == null)
            throw new ParseException("Null arguments!", 0);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        java.util.Date uDate = simpleDateFormat.parse(myDate);
        return (uDate);
    }

    /**
     * Converts a date to a string based on a <code>SimpleDateFormat</code>
     * pattern. e.g. <code>String s = dateToString(uDate, "dd/MM/yyyy");</code>
     * <p>
     * 
     * @param uDate
     *            The date string.
     * @param pattern
     *            The pattern to use.
     *            <p>
     * @return The string of the date or <code>null</code> on error.
     */
    public static String dateToString(java.util.Date uDate, String pattern) {
        if (uDate == null || pattern == null)
            return (null);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        String myDate = simpleDateFormat.format(uDate);

        return (myDate);
    }
}

Related

  1. getDayBetween(String firstDay, String secondDay)
  2. getDayDate(String date, String formatFormat)
  3. getDayDesc(Date arg)
  4. getDayEnd(Date date)
  5. getDayEndTime(int off, String timezone)
  6. getDayFormat(Date date)
  7. getDayInWeekBeginAndEnd(Date date)
  8. getDayInWeekForMonthIn2Characters(int year, int month)
  9. getDayName(String dateString)