Java Parse Date Pattern YYYY parse(String format, String val)

Here you can find the source of parse(String format, String val)

Description

Parse a String val based on the string format provided.

License

Open Source License

Parameter

Parameter Description
format The SimpleDateFormat string
val The date string to parse

Return

date The parsed Date

Declaration

public static Date parse(String format, String val) throws java.text.ParseException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import java.util.Date;

import java.util.StringTokenizer;
import java.util.TimeZone;

public class Main {
    private static SimpleDateFormat stdFormatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    /**/*from  w w  w.j  av  a  2  s .  c  o m*/
     * Parse a date string which is in the format of "yyyyMMddHHmmssSSS" into a
     * java.util.Date object.
     * 
     * @param val
     *            The date string to parse
     * @return date The parsed Date
     */
    public static Date parse(String val) throws java.text.ParseException {
        if (val == null || val.trim().length() == 0)
            return null;
        return stdFormatter.parse(val);
    }

    /**
     * Parse a String val based on the string format provided.
     * 
     * @param format
     *            The SimpleDateFormat string
     * @param val
     *            The date string to parse
     * 
     * @return date The parsed Date
     */
    public static Date parse(String format, String val) throws java.text.ParseException {
        if (val == null || val.trim().length() == 0)
            return null;
        validateDate(val, format);
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        formatter.setLenient(false);
        return formatter.parse(val);
    }

    /**
     * Parse a String val based on the string format and timezone provided.
     * 
     * @param format
     *            The SimpleDateFormat string
     * @param val
     *            The date string to parse
     * @param timezone
     *            The timezone
     * 
     * @return date The parsed Date
     */
    public static Date parse(String format, String val, String timezone) throws java.text.ParseException {
        if (val == null || val.trim().length() == 0)
            return null;
        TimeZone tz = null;
        if (timezone == null || timezone.length() == 0) {
            tz = TimeZone.getDefault();
        } else {
            tz = TimeZone.getTimeZone(timezone);
        }
        return parse(format, val, tz);
    }

    /**
     * This private parse method is called by the public parse method that
     * accepts a timezone.
     * 
     * @param format
     *            The SimpleDateFormat string
     * @param val
     *            The date string to parse
     * @param timezone
     *            The timezone
     * 
     * @return date The parsed Date
     */
    private static Date parse(String format, String val, TimeZone timezone) throws java.text.ParseException {
        if (val == null || val.trim().length() == 0)
            return null;
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        formatter.setTimeZone(timezone);
        formatter.setLenient(false);
        return formatter.parse(val);
    }

    private static void validateDate(String val, String format) throws ParseException {
        if (format.equals("MM/dd/yyyy")) {
            StringTokenizer strt = new StringTokenizer(val, "/");
            if (strt.countTokens() != 3) {
                throw new ParseException("Incorrect date format", 0);
            } else {
                try {
                    int month = Integer.parseInt(strt.nextToken());
                    int day = Integer.parseInt(strt.nextToken());
                    int year = Integer.parseInt(strt.nextToken());
                    if (month < 1 || month > 12 || day < 1 || day > 31 || year > 3000 || year < 1000) {
                        throw new ParseException("Incorrect date format", 0);
                    }
                } catch (NumberFormatException e) {
                    throw new ParseException("Incorrect date format", 0);
                }
            }
        }
    }
}

Related

  1. parse(String dateString)
  2. parse(String dateString)
  3. parse(String dateString, String dateFormat)
  4. parse(String dateText)
  5. parse(String ds)
  6. parse(String input)
  7. parse(String original)
  8. parse(String param)
  9. parse(String param)