Java String to Date toDate(String dateString, String dateFormatPattern)

Here you can find the source of toDate(String dateString, String dateFormatPattern)

Description

Converts a String to a Date, using the specified pattern.

License

Open Source License

Parameter

Parameter Description
dateString the String to convert
dateFormatPattern the pattern

Return

the corresponding Date

Declaration

public static Date toDate(String dateString, String dateFormatPattern) throws ParseException 

Method Source Code

//package com.java2s;
/**//  w  w w  .j  a v a2 s . c o  m
 * Copyright (C) 2002-2005 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

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

public class Main {
    private static SimpleDateFormat dateFormat = new SimpleDateFormat();

    /**
     * Converts a String to a Date, using the specified pattern.
     * (see java.text.SimpleDateFormat for pattern description)
     *
     * @param dateString the String to convert
     * @param dateFormatPattern the pattern
     * @return the corresponding Date
     * @exception ParseException, if the String doesn't match the pattern
     */
    public static Date toDate(String dateString, String dateFormatPattern) throws ParseException {
        Date date = null;
        if (dateFormatPattern == null) {
            dateFormatPattern = "yyyy-MM-dd";
        }
        synchronized (dateFormat) {
            dateFormat.applyPattern(dateFormatPattern);
            dateFormat.setLenient(false);
            date = dateFormat.parse(dateString);
        }
        return date;
    }
}

Related

  1. toDate(String dateStr, String pattern)
  2. toDate(String dateString)
  3. toDate(String dateString)
  4. toDate(String dateString)
  5. toDate(String dateString, Locale locale)
  6. toDate(String dateString, String format)
  7. toDate(String datetime)
  8. toDate(String dateToConvert)
  9. toDate(String dateToDatify)