DateHelper.java :  » Content-Management-System » hippo-cms » nl » hippo » util » Java Open Source

Java Open Source » Content Management System » hippo cms 
hippo cms » nl » hippo » util » DateHelper.java
/*
* Copyright 2001-2007 Hippo (www.hippo.nl)
*
* 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 nl.hippo.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
public class DateHelper
{
    public static int oneWeekAgo()
    {
        GregorianCalendar date = new GregorianCalendar(); // today
        date.add(Calendar.DATE, -7); // one week ago
        int fromDate = dateAsYYYYMMDD(date);
        //System.out.println("DateHelper: oneWeekAgo: " + fromDate);
        return fromDate;
    }

    public static String timeOffset(String format, int years, int months, int days)
    {
        GregorianCalendar date = new GregorianCalendar(); // today
        if (years != 0) date.add(Calendar.YEAR,years);
        if (months != 0) date.add(Calendar.MONTH,months);
        if (days != 0) date.add(Calendar.DATE,days);

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(date.getTime());
    }

    public static int twoWeeksAgo()
    {
        GregorianCalendar date = new GregorianCalendar(); // today
        date.add(Calendar.DATE, -14); // two weeks ago
        int fromDate = dateAsYYYYMMDD(date);
        //System.out.println("DateHelper: twoWeeksAgo: " + fromDate);
        return fromDate;
    }

    public static int oneMonthAgo()
    {
        GregorianCalendar date = new GregorianCalendar(); // today
        date.add(Calendar.MONTH, -1); // one month ago
        int fromDate = dateAsYYYYMMDD(date);
        //System.out.println("DateHelper: oneMonthAgo: " + fromDate);
        return fromDate;
    }

    public static int twoMonthsAgo()
    {
        GregorianCalendar date = new GregorianCalendar(); // today
        date.add(Calendar.MONTH, -2); // two months ago
        int fromDate = dateAsYYYYMMDD(date);
        return fromDate;
    }

    public static int sixMonthsAgo()
    {
        GregorianCalendar date = new GregorianCalendar(); // today
        date.add(Calendar.MONTH, -6);
        int fromDate = dateAsYYYYMMDD(date);
        return fromDate;
    }

    public static int oneYearAgo()
    {
        GregorianCalendar date = new GregorianCalendar(); // today
        date.add(Calendar.YEAR, -1);
        int fromDate = dateAsYYYYMMDD(date);
        return fromDate;
    }

    public static int dateAsYYYYMMDD(GregorianCalendar date)
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String dateString = dateFormat.format(date.getTime());
        return Integer.parseInt(dateString);
    }
    
    public static int compareDates(String date1, String format1, String date2, String format2) throws ParseException
    {
        DateFormat firstDateFormat;
        if (isStringNullOrEmpty(format1))
        {
            firstDateFormat = createIsoDateFormat();
        }
        else
        {
            firstDateFormat = new SimpleDateFormat(format1);
        }

        DateFormat secondDateFormat;
        if (isStringNullOrEmpty(format2))
        {
            secondDateFormat = createIsoDateFormat();
        }
        else
        {
            secondDateFormat = new SimpleDateFormat(format2);
        }
        Date firstDate = firstDateFormat.parse(date1);
        Date secondDate = secondDateFormat.parse(date2);
        
        return firstDate.compareTo(secondDate);
    }

    /**
     * @param string
     * @return
     */
    private static boolean isStringNullOrEmpty(String string)
    {
        return string == null || string.equals("");
    }

    private static DateFormat createIsoDateFormat()
    {
        DateFormat result;
        result = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
        return result;
    }
    
    
    public static String reformatDate(String date, String sourcePattern, String pattern) throws ParseException
    {
      DateFormat df = new SimpleDateFormat(sourcePattern, Locale.ENGLISH);
      DateFormat df2 = new SimpleDateFormat(pattern);
      String reformattedDate = null;
      Date tempDate = df.parse(date);
      reformattedDate = df2.format(tempDate);
      return reformattedDate;
    }
    
    
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.