Here you can find the source of toDate(String dateString, String dateFormatPattern)
Parameter | Description |
---|---|
dateString | the String to convert |
dateFormatPattern | the pattern |
public static Date toDate(String dateString, String dateFormatPattern) throws ParseException
//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; } }