Java String to Date toDate(String pString, String pFormat)

Here you can find the source of toDate(String pString, String pFormat)

Description

Converts the string to a date, using the given format.

License

Open Source License

Parameter

Parameter Description
pString the string to convert
pFormat the date format

Return

the date

Declaration

public static Date toDate(String pString, String pFormat) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**/* www .  j a  v  a 2 s. co m*/
     * Converts the string to a date, using the default date format.
     *
     * @param pString the string to convert
     * @return the date
     * @see DateFormat
     * @see DateFormat#getInstance()
     */
    public static Date toDate(String pString) {
        // Default
        return toDate(pString, DateFormat.getInstance());
    }

    /**
     * Converts the string to a date, using the given format.
     *
     * @param pString the string to convert
     * @param pFormat the date format
     * @return the date
     * @todo cache formats?
     * @see java.text.SimpleDateFormat
     * @see java.text.SimpleDateFormat#SimpleDateFormat(String)
     */
    public static Date toDate(String pString, String pFormat) {
        // Get the format from cache, or create new and insert
        // Return new date
        return toDate(pString, new SimpleDateFormat(pFormat));
    }

    /**
     * Converts the string to a date, using the given format.
     *
     * @param pString the string to convert
     * @param pFormat the date format
     * @return the date
     * @see SimpleDateFormat
     * @see SimpleDateFormat#SimpleDateFormat(String)
     * @see DateFormat
     */
    public static Date toDate(final String pString, final DateFormat pFormat) {
        try {
            synchronized (pFormat) {
                // Parse date using given format
                return pFormat.parse(pString);
            }
        } catch (ParseException pe) {
            // Wrap in RuntimeException
            throw new IllegalArgumentException(pe.getMessage());
        }
    }
}

Related

  1. toDate(String format, String str)
  2. toDate(String formatDate, String stringDate)
  3. toDate(String param)
  4. toDate(String patten, String value)
  5. toDate(String pattern, String date)
  6. toDate(String s)
  7. toDate(String s)
  8. toDate(String s, String format)
  9. toDate(String sdate, SimpleDateFormat dateFormater)