Java Parse Date parseDate(String dateStr, String format)

Here you can find the source of parseDate(String dateStr, String format)

Description

Parse the date with the given date string and date format.

License

Open Source License

Parameter

Parameter Description
dateStr a string of date
format a string of date format

Exception

Parameter Description
ParseException an exception

Return

an instance of Date after parsed

Declaration

public static Date parseDate(String dateStr, String format) throws ParseException 

Method Source Code

//package com.java2s;
/************************************************************************************
 * @File name   :      Utils.java// w  ww  .j  ava 2  s .  co m
 *
 * @Author      :      Brenda Yin
 *
 * @Date        :      2011-1-24
 *
 * @Copyright Notice: 
 * Copyright (c) 2011 SGM, Inc. All  Rights Reserved.
 * This software is published under the terms of the SGM Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------------
 * Date                         Who             Version            Comments
 * 2011-1-24 ????12:43:43        Brenda Yin         1.0                Initial Version
 ************************************************************************************/

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**
     * Parse the date with the given date string and date format.
     * 
     * @Date : 2011-6-9
     * @param dateStr
     *            a string of date
     * @param format
     *            a string of date format
     * @return an instance of Date after parsed
     * @throws ParseException
     */
    public static Date parseDate(String dateStr, String format) throws ParseException {
        if (isNullOrEmpty(format) || isNullOrEmpty(dateStr)) {
            return null;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat(format);

        return dateFormat.parse(dateStr.trim());
    }

    /**
     * Check if the input str is null or ""
     * 
     * @Date : 2011-3-21
     * @param str
     *            - string
     * @return true if str is null or empty; otherwise, false
     */
    public static boolean isNullOrEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }
}

Related

  1. parseDate(String datestr)
  2. parseDate(String dateStr)
  3. parseDate(String dateStr, SimpleDateFormat... formats)
  4. parseDate(String dateStr, String dateFormat)
  5. parseDate(String dateStr, String dateFormat)
  6. parseDate(String dateStr, String format)
  7. parseDate(String dateStr, String... formats)
  8. parseDate(String dateString)
  9. ParseDate(String dateString)