Source for org.jfree.formula.util.DateUtil

   1: /**
   2:  * =========================================
   3:  * LibFormula : a free Java formula library
   4:  * =========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/libformula/
   7:  *
   8:  * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  *
  26:  * ------------
  27:  * $Id: DateUtil.java,v 1.5 2007/05/21 18:57:59 mimil Exp $
  28:  * ------------
  29:  * (C) Copyright 2006-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.formula.util;
  32: 
  33: import java.math.BigDecimal;
  34: import java.sql.Time;
  35: import java.util.Calendar;
  36: import java.util.Date;
  37: import java.util.GregorianCalendar;
  38: 
  39: import org.jfree.formula.DefaultLocalizationContext;
  40: import org.jfree.formula.LocalizationContext;
  41: import org.jfree.formula.typing.Type;
  42: import org.jfree.formula.typing.coretypes.DateTimeType;
  43: 
  44: /**
  45:  * 
  46:  * @author Cedric Pronzato
  47:  * 
  48:  */
  49: public class DateUtil
  50: {
  51:   private static final Date ISO8001_TIME = new GregorianCalendar().getTime();
  52: 
  53:   /**
  54:    * Converts a <code>Date</code> value according to the requested
  55:    * <code>Type</code> to the proper <code>Date</code> subclasses (<code>java.sql.Time</code>,
  56:    * <code>java.sql.Date</code>) if needed. If the requested type is unknown,
  57:    * no conversion takes place and the input date is returned.
  58:    * 
  59:    * @param fromDate
  60:    *          The date to convert.
  61:    * @param toType
  62:    *          The requested type of date.
  63:    * @return The converted date.
  64:    */
  65:   public static Date normalizeDate(Date fromDate, Type toType)
  66:   {
  67:     return normalizeDate(fromDate, toType, true);
  68:   }
  69: 
  70:   public static Date normalizeDate(Date fromDate, Type toType,
  71:       boolean convertSerial)
  72:   {
  73:     if (fromDate == null || toType == null)
  74:     {
  75:       throw new IllegalArgumentException();
  76:     }
  77: 
  78:     if (convertSerial)
  79:     {
  80:       Number serial = toSerialDate(fromDate, null);
  81:       serial = normalizeDate(serial, toType);
  82:       fromDate = toJavaDate(serial, null);
  83:     }
  84:     // final GregorianCalendar gc = new GregorianCalendar();
  85:     // gc.setTime(fromDate);
  86:     // gc.set(GregorianCalendar.MILLISECOND, 0);
  87:     // fromDate = gc.getTime();
  88:     if (toType.isFlagSet(Type.TIME_TYPE))
  89:     {
  90:       return new Time(fromDate.getTime());
  91:     }
  92:     else if (toType.isFlagSet(Type.DATE_TYPE))
  93:     {
  94:       return new java.sql.Date(fromDate.getTime());
  95:     }
  96:     else if (toType.isFlagSet(Type.DATETIME_TYPE))
  97:     {
  98:       return new Date(fromDate.getTime());
  99:     }
 100: 
 101:     return fromDate;
 102:   }
 103: 
 104:   public static Number normalizeDate(Number fromSerialDate, Type toType)
 105:   {
 106:     if (fromSerialDate == null || toType == null)
 107:     {
 108:       throw new IllegalArgumentException();
 109:     }
 110: 
 111:     final BigDecimal o = new BigDecimal(fromSerialDate.doubleValue()).setScale(
 112:         5, BigDecimal.ROUND_UP);
 113: 
 114:     if (toType.isFlagSet(Type.TIME_TYPE))
 115:     {
 116:       return o.subtract(new BigDecimal(o.intValue()));
 117:       // only return the decimal part
 118:       // final Double d = new Double(fromSerialDate.doubleValue()
 119:       // - fromSerialDate.intValue());
 120:       // return d;
 121:     }
 122:     else if (toType.isFlagSet(Type.DATE_TYPE))
 123:     {
 124:       return new Integer(fromSerialDate.intValue());
 125:     }
 126:     // datetime (java.util.Date)
 127:     else
 128:     {
 129:       return o;
 130:     }
 131:   }
 132: 
 133:   public static Date toJavaDate(Number serialDate, LocalizationContext context)
 134:   {
 135:     final Date javaDate = HSSFDateUtil.getJavaDate(serialDate.doubleValue());
 136:     // check for null (error)
 137:     final long l = (javaDate.getTime() / 1000) * 1000;
 138:     // final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 139:     // context.getLocale());
 140:     // gc.setTimeInMillis(serialDate.longValue() * MILLISECS_PER_DAY);
 141:     // return gc.getTime();
 142:     return new Date(l);
 143:   }
 144: 
 145:   public static Number toSerialDate(Date date, LocalizationContext context)
 146:   {
 147:     // final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 148:     // context.getLocale());
 149:     // gc.setTime(date);
 150:     // final double fraction = (((gc.get(Calendar.HOUR_OF_DAY) * 60 + gc
 151:     // .get(Calendar.MINUTE)) * 60 + gc.get(Calendar.SECOND)) * 1000 + gc
 152:     // .get(Calendar.MILLISECOND))
 153:     // / (double) MILLISECS_PER_DAY;
 154:     // final long timeInMillis = date.getTime();
 155:     // final long days = timeInMillis / MILLISECS_PER_DAY;
 156:     // return new BigDecimal((double) (days) + fraction);
 157:     final double serial = HSSFDateUtil.getExcelDate(date);
 158:     return new Double(serial);
 159:   }
 160: 
 161:   public static void main(String[] args)
 162:   {
 163:     final DefaultLocalizationContext context = new DefaultLocalizationContext();
 164:     final java.sql.Date createDate = createDate(2006, 05, 01, context);
 165:     final Number serial = toSerialDate(createDate, context);
 166:     System.out.println(createDate);
 167:     System.out.println(serial);
 168:     final Date toJavaDate = toJavaDate(serial, context);
 169:     System.out.println(normalizeDate(toJavaDate, DateTimeType.DATE_TYPE));
 170:     System.out.println(toJavaDate);
 171:     System.out.println(HSSFDateUtil.getJavaDate(serial.doubleValue()));
 172:   }
 173: 
 174:   public static Date now(LocalizationContext context)
 175:   {
 176:     final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 177:         context.getLocale());
 178:     gc.set(Calendar.MILLISECOND, 0);
 179: 
 180:     return gc.getTime();
 181:   }
 182: 
 183:   public static Date createDateTime(int year, int month, int day, int hour,
 184:       int minute, int second, LocalizationContext context)
 185:   {
 186:     final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 187:         context.getLocale());
 188:     gc.set(Calendar.DAY_OF_MONTH, day);
 189:     gc.set(Calendar.MONTH, month);
 190:     gc.set(Calendar.YEAR, year);
 191:     gc.set(Calendar.MILLISECOND, 0);
 192:     gc.set(Calendar.HOUR_OF_DAY, hour);
 193:     gc.set(Calendar.MINUTE, minute);
 194:     gc.set(Calendar.SECOND, second);
 195:     return gc.getTime();
 196:   }
 197: 
 198:   public static Time createTime(int hour, int minute, int second,
 199:       LocalizationContext context)
 200:   {
 201:     final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 202:         context.getLocale());
 203:     gc.setTime(ISO8001_TIME);
 204:     gc.set(Calendar.MILLISECOND, 0);
 205:     gc.set(Calendar.HOUR_OF_DAY, hour);
 206:     gc.set(Calendar.MINUTE, minute);
 207:     gc.set(Calendar.SECOND, second);
 208:     return new Time(gc.getTime().getTime());
 209:   }
 210: 
 211:   public static java.sql.Date createDate(int year, int month, int day,
 212:       LocalizationContext context)
 213:   {
 214:     final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 215:         context.getLocale());
 216:     gc.set(Calendar.DAY_OF_MONTH, day);
 217:     gc.set(Calendar.MONTH, month - 1);
 218:     gc.set(Calendar.YEAR, year);
 219:     gc.set(Calendar.MILLISECOND, 0);
 220:     gc.set(Calendar.HOUR_OF_DAY, 0);
 221:     gc.set(Calendar.MINUTE, 0);
 222:     gc.set(Calendar.SECOND, 0);
 223:     return new java.sql.Date(gc.getTime().getTime());
 224:   }
 225: 
 226:   public static Calendar createCalendar(Date date, LocalizationContext context)
 227:   {
 228:     final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 229:         context.getLocale());
 230:     gc.setTime(date);
 231:     return gc;
 232:   }
 233: 
 234: }