Java Year Get getYearForDate(String dateToCheck, String pattern)

Here you can find the source of getYearForDate(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 The pattern to use. <p>

Return

The date or -1 on error.

Declaration

public static int getYearForDate(String dateToCheck, String pattern) 

Method Source Code


//package com.java2s;
/*//from   w  w w  .  j a v  a 2 s.  co 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 year of the date
     * or -1 on error.
     * <p>
     * <b>NOTE:</b> only 'yyyy' is supported!
     * <p>
     * 
     * @param dateToCheck
     *            The date string to check.
     * @param pattern
     *            the The pattern to use.
     *            <p>
     * @return The date or -1 on error.
     */
    public static int getYearForDate(String dateToCheck, String pattern) {
        if (isDateValid(dateToCheck, pattern) && dateToCheck.length() == pattern.length()) {
            int index = pattern.indexOf("yyyy"); // only "yyyy" is supported
            if (index == -1) {
                return (-1);
            }

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

            return (year);
        } 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. getYear(String string)
  2. getYear(String tempdat, Locale locale)
  3. getYear(String[] contents)
  4. getYearCount(String from, String to)
  5. getYearEnd(Date date)
  6. getYearList(int years)
  7. getYearLongDesc(Date year)
  8. getYearlyIntervals(Date start, Date end)
  9. getYearName(String dateString)