cn.loveapple.service.util.DateUtil.java Source code

Java tutorial

Introduction

Here is the source code for cn.loveapple.service.util.DateUtil.java

Source

/*
 * $HeadURL$
 * $Author$
 * $Revision$
 * $Date$
 *
 * ====================================================================
 *
 * Copyright (C) 2008 by loveapple.cn
 *
 * All copyright notices regarding loveapple and loveapple CoreLib
 * MUST remain intact in the scripts, documents and source code.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Correspondence and Marketing Questions can be sent to:
 * info at loveapple
 *
 * @author: loveapple
 */
package cn.loveapple.service.util;

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

import org.apache.commons.lang.StringUtils;

/**
 * 
 * @author $Author$
 * @version $Revision$
 * @date $Date$
 * @id $Id$
 *
 */
public class DateUtil {
    /**
     * YYYYMMDD
     */
    public static final String DATE_PTTERN_YYYYMMDD = "yyyyMMdd";

    /**
     * ???
     * 
     * @see SimpleDateFormat ???
     * @param source ?
     * @param pattern 
     * @return ????????<code>null</code>?????????
     */
    public static Date paseDate(String source, String pattern) {
        if (StringUtils.isEmpty(source) || StringUtils.isEmpty(pattern)) {
            return null;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            return format.parse(source);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * ?????????
     * 
     * @see #paseDate(String, String)
     * @param source ?
     * @param pattern 
     * @return ??
     */
    public static boolean isDateStr(String source, String pattern) {
        return paseDate(source, pattern) != null;
    }

    /**
     * 
     * @param source
     * @param pattern
     * @return
     */
    public static String toDateString(Date source, String pattern) {
        if (source == null || StringUtils.isEmpty(pattern)) {
            return null;
        }
        try {
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            return format.format(source);
        } catch (Exception e) {
            return null;
        }
    }
}