Here you can find the source of parse(String format, String val)
Parameter | Description |
---|---|
format | The SimpleDateFormat string |
val | The date string to parse |
public static Date parse(String format, String val) throws java.text.ParseException
//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); } } } } }