Java String to Date toDateWithFormatString(String date, String format)

Here you can find the source of toDateWithFormatString(String date, String format)

Description

Returns a Java representation of a date string.

License

Open Source License

Parameter

Parameter Description
date Date in string format.

Exception

Parameter Description
IllegalArgumentException If the provided date isn't in the given format.

Return

Java date representation of the given date string or null if date is null .

Declaration

private static Date toDateWithFormatString(String date, String format) 

Method Source Code


//package com.java2s;
/*//from  w  w  w. j  ava  2s.  c  om
 * Copyright (c) 2010 Mark Allen.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

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

public class Main {
    /**
     * Returns a Java representation of a {@code date} string.
     * 
     * @param date
     *          Date in string format.
     * @return Java date representation of the given {@code date} string or
     *         {@code null} if {@code date} is {@code null}.
     * @throws IllegalArgumentException
     *           If the provided {@code date} isn't in the given {@code format}.
     */
    private static Date toDateWithFormatString(String date, String format) {
        if (date == null)
            return null;

        try {
            return new SimpleDateFormat(format).parse(date);
        } catch (ParseException e) {
            throw new IllegalArgumentException(
                    "Unable to parse date '" + date + "' using format string '" + format + "'", e);
        }
    }
}

Related

  1. toDatetime(String sDate)
  2. toDateTimeFormat(String input, String inputfmt, String outputfmt)
  3. toDateTimeSSSString(long datetime)
  4. toDateTimeString(java.util.Date date)
  5. toDateTimeString(java.util.Date date)
  6. toDateWithTimezone(String dateTime, String pattern)
  7. transformStringToDate(String dateString)
  8. translateToDate(String aDate, String aFormat)