/* * Copyright 2006, XpertNet SARL, and individual contributors as indicated * by the contributors.txt. * * This 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 2.1 of * the License, or (at your option) any later version. * * This software 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 software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. * * @author Ravaneswaran Chinnasamy */ package com.xpn.xwiki.plugin.lucene; import org.apache.log4j.Logger; import java.util.Calendar; import java.text.SimpleDateFormat; import java.text.ParseException; public final class DateUtility { public static final String DATE_FORMAT_ONE = "dd-MM-yyyy"; public static final String DATE_FORMAT_TWO = "yyyy-MM-dd"; public static final String DATE_FORMAT_THREE = "yyyy-MM-dd HH:mm:ss.ms"; private static final Logger LOG = Logger.getLogger(DateUtility.class); public static String getDateString(String date, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); Calendar cal = Calendar.getInstance(); try { sdf.parse(date); } catch (ParseException e) { // Ingonore this } return sdf.format(cal.getTime()); } public static String getCurrentDate(String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); Calendar cal = Calendar.getInstance(); return sdf.format(cal.getTime()); } public static String getCurrentDate() { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_ONE); Calendar cal = Calendar.getInstance(); return sdf.format(cal.getTime()); } public static String getPreviousDate() { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_ONE); Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE) - 1); return sdf.format(cal.getTime()); } public static String getNextDate() { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_ONE); Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE) + 1); return sdf.format(cal.getTime()); } public static String getPreviousDate(int noOfDays) { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_ONE); Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE) - noOfDays); return sdf.format(cal.getTime()); } public static String getNextDate(int noOfDays) { SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_ONE); Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE) + noOfDays); return sdf.format(cal.getTime()); } }