Java Parse Date parseDate(String certDate)

Here you can find the source of parseDate(String certDate)

Description

parse Date

License

Open Source License

Declaration

public static Date parseDate(String certDate) 

Method Source Code


//package com.java2s;
/*/* w ww .java  2  s . c o  m*/
 * FIEBDC 3 parser  
 * Copyright (C) 2014 DiSiD Technologies
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/copyleft/gpl.html>.
 */

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static DateFormat fullDateFormat = new SimpleDateFormat("ddMMyyyy");
    private static DateFormat mediumDateFormat = new SimpleDateFormat("ddMMyy");
    private static DateFormat monthYearDateFormat = new SimpleDateFormat("MMyy");
    private static DateFormat singleMonthYearDateFormat = new SimpleDateFormat("Myy");
    private static DateFormat yearDateFormat = new SimpleDateFormat("yy");

    public static Date parseDate(String certDate) {
        String dateToParse = certDate;
        DateFormat dateFormat = fullDateFormat;
        switch (certDate.length()) {
        case 1:
        case 2:
            dateFormat = yearDateFormat;
            break;
        case 3:
            dateFormat = singleMonthYearDateFormat;
            break;
        case 4:
            dateToParse = replaceDoubleZero(dateToParse, 0);
            dateFormat = monthYearDateFormat;
            break;
        case 5:
        case 6:
            dateToParse = replaceDoubleZero(dateToParse, 0);
            dateToParse = replaceDoubleZero(dateToParse, 2);
            dateFormat = mediumDateFormat;
            break;
        case 7:
        case 8:
        default:
            dateToParse = replaceDoubleZero(dateToParse, 0);
            dateToParse = replaceDoubleZero(dateToParse, 2);
            dateFormat = fullDateFormat;
        }

        try {
            Date date = dateFormat.parse(dateToParse);
            return date;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Replaces a "00" in a date String with a "01". This is because the FIEBDC3
     * format allows to set the day or month value as "00" when it is ignored,
     * but we are parsing a full date and so we need a valid one, and parsers
     * don't admit "00" as day or month.
     * 
     * @param value
     *            the string to perform the replacement on
     * @param start
     *            where to look for the "00" in te text
     * @return a text with "00" value replaced if found
     */
    public static String replaceDoubleZero(String value, int start) {
        if (value.regionMatches(start, "00", 0, 2)) {
            String first = start > 0 ? value.substring(0, start) : "";
            String last = value.substring(start + 2);
            return first + "01" + last;
        }
        return value;
    }
}

Related

  1. parseDate(SimpleDateFormat parser, String str, String[] parsePatterns)
  2. parseDate(String _dateString)
  3. parseDate(String actual, DateFormat format, Pattern pattern)
  4. parseDate(String applyDt)
  5. parseDate(String buildDate, SimpleDateFormat dateFormat)
  6. parseDate(String currDate)
  7. parseDate(String d)
  8. parseDate(String d)
  9. parseDate(String d)