Java Parse Date parseDate(String sDateTime, String sPattern)

Here you can find the source of parseDate(String sDateTime, String sPattern)

Description

Parse the String to return a valid Date object.

License

Open Source License

Parameter

Parameter Description
sDateTime The String value that contains the date.
sPattern The String value that contains the pattern to use (ie. "yyyy/MM/dd hh:mm:ss") during the conversion.

Declaration

public static Date parseDate(String sDateTime, String sPattern) 

Method Source Code


//package com.java2s;
import java.text.*;
import java.util.*;

public class Main {
    /**//from  w w  w .j a  v  a2  s  . c  o  m
     * The date format manager.  This handles all the date formatting functionality.
     */
    private static SimpleDateFormat fmtDateHandler = new SimpleDateFormat();

    /**
     * Parse the String to return a valid Date object. <p> This method uses the time zone.
     * @param    sDateTime    The String value that contains the date.
     * @param    sPattern     The String value that contains the pattern to use
     * (ie. "yyyy/MM/dd hh:mm:ss") during the conversion.
     */
    public static Date parseDate(String sDateTime, String sPattern) {
        TimeZone timeZone = TimeZone.getDefault();
        Date date = parseDate(sDateTime, sPattern, timeZone);
        return date;
    }

    /**
     * Parse the String to return a valid Date object.
     * @param    sDateTime    The String value that contains the date.
     * @param    sPattern     The String value that contains the pattern to use
     * (ie. "yyyy/MM/dd hh:mm:ss") during the conversion.
     * @param    timeZone     The specified time zone to use during the conversion.
     */
    public static Date parseDate(String sDateTime, String sPattern, TimeZone timeZone) {
        ParsePosition pos = new ParsePosition(0);
        fmtDateHandler.applyPattern(sPattern);
        fmtDateHandler.setTimeZone(timeZone);
        // Parse the input string.
        return fmtDateHandler.parse(sDateTime, pos);
    }
}

Related

  1. parseDate(String sDate, Locale locale)
  2. parseDate(String sDate, SimpleDateFormat _dateFormat)
  3. parseDate(String sDate, String sFormat)
  4. parseDate(String sDate, String sSourceFormat, String sDestinationFormat)
  5. parseDate(String sdate, String timeFormat, String timeZone)
  6. parseDate(String source, String format)
  7. parseDate(String src, String pattern)
  8. parseDate(String srtDate, String pattern)
  9. parseDate(String str)