com.feilong.core.text.DateFormatUtilTemp.java Source code

Java tutorial

Introduction

Here is the source code for com.feilong.core.text.DateFormatUtilTemp.java

Source

/*
 * Copyright (C) 2008 feilong
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.feilong.core.text;

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Validate;

/**
 * {@link DateFormat}/??.
 * 
 * <h3>{@link SimpleDateFormat}?:</h3>
 * 
 * <blockquote>
 * see <a href="http://newslxw.iteye.com/blog/1114851">SimpleDateFormat?</a>
 * <br>
 * ? {@link SimpleDateFormat} Synchronization
 * </blockquote>
 * 
 * @author <a href="http://feitianbenyue.iteye.com/">feilong</a>
 * @see java.text.Format
 * @see java.text.DateFormat
 * @see java.text.SimpleDateFormat
 * @see org.apache.commons.beanutils.converters.DateConverter
 * @see org.apache.commons.beanutils.locale.converters.DateLocaleConverter
 * @see <a href="http://newslxw.iteye.com/blog/1114851">SimpleDateFormat?</a>
 * @since 1.0.1
 * @deprecated parse ?{@link org.apache.commons.lang3.time.DateUtils#parseDate(String, String...)}
 */
@Deprecated
public final class DateFormatUtilTemp {

    /** Don't let anyone instantiate this class. */
    private DateFormatUtilTemp() {
        //AssertionError?. ?????. ???.
        //see Effective Java 2nd
        throw new AssertionError("No " + getClass().getName() + " instances for you!");
    }

    /**
     * parse?.
     * 
     * <p style="color:red">
     * ?, {@link com.feilong.core.date.DateUtil#toDate(String, String...)}
     * </p>
     * 
     * <p>
     *  {@link #parse(String, String, Locale)},locale {@link Locale#getDefault()}
     * </p>
     * 
     * @param dateString
     *            the date string
     * @param pattern
     *             {@link com.feilong.core.DatePattern} ?
     * @return  <code>dateString</code> null, {@link NullPointerException}<br>
     *          <code>dateString</code> blank, {@link IllegalArgumentException}<br>
     *          <code>pattern</code>  null, {@link NullPointerException}<br>
     *          <code>pattern</code>  blank, {@link IllegalArgumentException}<br>
     *         ? {@link Locale#getDefault()}, {@link #parse(String, String, Locale)}
     * @see #parse(String, String, Locale)
     */
    public static Date parse(String dateString, String pattern) {
        return parse(dateString, pattern, Locale.getDefault());
    }

    /**
     *  <code>dateString</code> ?.
     * 
     * @param dateString
     *            the date string
     * @param pattern
     *             {@link com.feilong.core.DatePattern} ?
     * @param locale
     *            , null, {@link Locale#getDefault()}
     * @return  <code>dateString</code> null, {@link NullPointerException}<br>
     *          <code>dateString</code> blank, {@link IllegalArgumentException}<br>
     *          <code>pattern</code>  null, {@link NullPointerException}<br>
     *          <code>pattern</code>  blank, {@link IllegalArgumentException}<br>
     *         ? {@link java.text.SimpleDateFormat#parse(String, ParsePosition)}
     * @see SimpleDateFormat#parse(String)
     * @see SimpleDateFormat#parse(String, ParsePosition)
     */
    public static Date parse(String dateString, String pattern, Locale locale) {
        Validate.notBlank(dateString, "dateString can't be null/empty!");
        Validate.notBlank(pattern, "pattern can't be null/empty!");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern,
                ObjectUtils.defaultIfNull(locale, Locale.getDefault()));
        //? java.text.DateFormat#parse(String)  ParseException checked Exception
        return simpleDateFormat.parse(dateString, new ParsePosition(0));//?, null
    }
}