1 /*** 2 * The contents of this file are subject to the Mozilla Public 3 * License Version 1.1 (the "License"); you may not use this file 4 * except in compliance with the License. You may obtain a copy of 5 * the License at http://www.mozilla.org/MPL/ 6 * 7 * Software distributed under the License is distributed on an "AS 8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or 9 * implied. See the License for the specific language governing 10 * rights and limitations under the License. 11 * 12 * The Original Code is pow2toolkit library. 13 * 14 * The Initial Owner of the Original Code is 15 * Power Of Two S.R.L. (www.pow2.com) 16 * 17 * Portions created by Power Of Two S.R.L. are 18 * Copyright (C) Power Of Two S.R.L. 19 * All Rights Reserved. 20 * 21 * Contributor(s): 22 */ 23 24 package com.pow2.util; 25 26 import java.text.*; 27 import java.util.*; 28 29 import org.apache.log4j.Category; 30 31 32 /*** 33 * Date utility class. 34 * 35 * @author Luca Fossato 36 */ 37 public class DateUtil 38 { 39 /*** Log4j category. */ 40 private static Category cat = Category.getInstance(DateUtil.class); 41 42 43 /*** 44 * Get the current date string representation. 45 * 46 * @param dateFormat the input dateFormat. 47 * See the <code>java.text.SimpleDateFormat</code> API for date format 48 * string examples 49 */ 50 public static String getCurrentDateString(String dateFormat) 51 { 52 Calendar cal = Calendar.getInstance(TimeZone.getDefault()); 53 SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); 54 sdf.setTimeZone(TimeZone.getDefault()); 55 56 return sdf.format(cal.getTime()); 57 } 58 59 60 /*** 61 * Get the string representation of the input Date object 62 * 63 * @param date the input Date object 64 * @param dateFormat a date format string like "dd/MM/yyyy" 65 * @return the string representation of the input Date object 66 */ 67 public static String getDateString(Date date, String dateFormat) 68 { 69 SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); 70 return sdf.format(date); 71 } 72 73 74 /*** 75 * Get a java Date object from an input date string representation. 76 * <br> 77 * See the <code>java.text.SimpleDateFormat</code> API for date format string 78 * examples. 79 * 80 * @param sDate the date string representation 81 * @param dateFormat a date format string like "dd/MM/yyyy" 82 * @return the Date object corresponding to the input date string, 83 * or null if the conversion fails 84 */ 85 public static Date getDate(String sDate, String dateFormat) 86 { 87 SimpleDateFormat fmt = new SimpleDateFormat(dateFormat); 88 ParsePosition pos = new ParsePosition(0); 89 90 return fmt.parse(sDate, pos); 91 } 92 93 94 /*** 95 * Add the input number of days to the startDate string representation. 96 * 97 * @param startDate the start date string representation 98 * @param dateFormat the start date format 99 * @param days the number of days to add to the startDate 100 * @return the Date object representing the resulting date 101 */ 102 public static Date addDays(String startDate, String dateFormat, int days) 103 { 104 return addDays(getDate(startDate, dateFormat), days); 105 } 106 107 108 /*** 109 * Add the input number of days to the start Date object. 110 * 111 * @param startDate the start Date object 112 * @param days the number of days to add to the startDate object 113 * @return the Date object representing the resulting date 114 */ 115 public static Date addDays(Date startDate, int days) 116 { 117 GregorianCalendar gCal = new GregorianCalendar(); 118 gCal.setTime(startDate); 119 gCal.add(Calendar.DATE, days); 120 121 return gCal.getTime(); 122 } 123 124 125 /*** 126 * Check if the <code>d</code> input date is between <code>d1</code> and 127 * <code>d2</code>. 128 * 129 * @param d the date to check 130 * @param d1 the lower boundary date 131 * @param d2 the upper boundary date 132 * @return true if d1 <= d <= d2, false otherwise 133 */ 134 public static boolean isDateBetween(Date d, Date d1, Date d2) 135 { 136 return ((d1.before(d) || d1.equals(d)) && 137 (d.before(d2) || d.equals(d2))); 138 } 139 }