Java tutorial
/* * $HeadURL: https://loveapple.googlecode.com/svn/trunk/src/client/android/gaia/src/cn/loveapple/android/utils/DateUtil.java $ * $Author: hao0323@gmail.com $ * $Revision: 373 $ * $Date: 2012-03-11 11:25:00 +0900 (, 11 3 2012) $ * * ==================================================================== * * 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.client.android.utils; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.lang.time.DateUtils; /** * * @author $Author: hao0323@gmail.com $ * @version $Revision: 373 $ * @date $Date: 2012-03-11 11:25:00 +0900 (, 11 3 2012) $ * @id $Id: DateUtil.java 373 2012-03-11 02:25:00Z hao0323@gmail.com $ * */ public class DateUtil extends DateUtils { /** * 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 (source == null || pattern == null) { 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 || pattern == null) { return null; } try { SimpleDateFormat format = new SimpleDateFormat(pattern); return format.format(source); } catch (Exception e) { return null; } } /** * * @see #DATE_PTTERN_YYYYMMDD * @see #toDateString(Date, String) * @param source * @return */ public static String toDateString(Date source) { if (source == null) { return null; } return toDateString(source, DATE_PTTERN_YYYYMMDD); } /** * @see #DATE_PTTERN_YYYYMMDD * @see #toDateString(Date, String) * @return */ public static String toDateString() { return toDateString(new Date(), DATE_PTTERN_YYYYMMDD); } }