Java Month Get getMonthForDate(String dateToCheck, String pattern)

Here you can find the source of getMonthForDate(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 date or -1 on error.

Declaration

public static int getMonthForDate(String dateToCheck, String pattern) 

Method Source Code


//package com.java2s;
/*//www. ja  va 2 s  .  c o m
 * 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 month of the date
     * or -1 on error.
     * <p>
     * <b>NOTE:</b> only 'MM' is supported!
     * <p>
     * 
     * @param dateToCheck
     *            The date string to check.
     * @param pattern
     *            The pattern to use.
     *            <p>
     * @return The date or -1 on error.
     */
    public static int getMonthForDate(String dateToCheck, String pattern) {
        if (isDateValid(dateToCheck, pattern) && dateToCheck.length() == pattern.length()) {
            int index = pattern.indexOf("MM"); // only "MM" is supported
            if (index == -1) {
                return (-1);
            }

            String month_str = dateToCheck.substring(index, index + 2);
            int month = -1;
            try {
                Integer i = new Integer(month_str);
                month = i.intValue();

                // month starts from 0...11
                month--;
            } catch (Exception e) {
                // month is already -1
            }

            return (month);
        } 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. getMonthEnd(Date date)
  2. getMonthEnd(String strdate)
  3. getMonthEnd(String strdate)
  4. getMonthEndDate(Date date)
  5. getMonthFirstDate(final Date date, final String format)
  6. getMonthForInt(int num)
  7. getMonthForString(String monthStr, Locale locale)
  8. getMonthInfo(int monthInfo)
  9. getMonthLength(String countDate)