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 3521 2007-10-16 10:55:14Z tmorgner $
  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:   private DateUtil()
  54:   {
  55:   }
  56: 
  57:   /**
  58:    * Converts a <code>Date</code> value according to the requested
  59:    * <code>Type</code> to the proper <code>Date</code> subclasses (<code>java.sql.Time</code>,
  60:    * <code>java.sql.Date</code>) if needed. If the requested type is unknown,
  61:    * no conversion takes place and the input date is returned.
  62:    * 
  63:    * @param fromDate
  64:    *          The date to convert.
  65:    * @param toType
  66:    *          The requested type of date.
  67:    * @return The converted date.
  68:    */
  69:   public static Date normalizeDate(final Date fromDate, final Type toType)
  70:   {
  71:     return normalizeDate(fromDate, toType, true);
  72:   }
  73: 
  74:   public static Date normalizeDate(Date fromDate, final Type toType,
  75:       final boolean convertSerial)
  76:   {
  77:     if (fromDate == null || toType == null)
  78:     {
  79:       throw new IllegalArgumentException();
  80:     }
  81: 
  82:     if (convertSerial)
  83:     {
  84:       Number serial = toSerialDate(fromDate, null);
  85:       serial = normalizeDate(serial, toType);
  86:       fromDate = toJavaDate(serial, null);
  87:     }
  88:     // final GregorianCalendar gc = new GregorianCalendar();
  89:     // gc.setTime(fromDate);
  90:     // gc.set(GregorianCalendar.MILLISECOND, 0);
  91:     // fromDate = gc.getTime();
  92:     if (toType.isFlagSet(Type.TIME_TYPE))
  93:     {
  94:       return new Time(fromDate.getTime());
  95:     }
  96:     else if (toType.isFlagSet(Type.DATE_TYPE))
  97:     {
  98:       return new java.sql.Date(fromDate.getTime());
  99:     }
 100:     else if (toType.isFlagSet(Type.DATETIME_TYPE))
 101:     {
 102:       return new Date(fromDate.getTime());
 103:     }
 104: 
 105:     return fromDate;
 106:   }
 107: 
 108:   public static Number normalizeDate(final Number fromSerialDate, final Type toType)
 109:   {
 110:     if (fromSerialDate == null || toType == null)
 111:     {
 112:       throw new IllegalArgumentException();
 113:     }
 114: 
 115:     final BigDecimal o = new BigDecimal(fromSerialDate.doubleValue()).setScale(
 116:         5, BigDecimal.ROUND_UP);
 117: 
 118:     if (toType.isFlagSet(Type.TIME_TYPE))
 119:     {
 120:       return o.subtract(new BigDecimal(o.intValue()));
 121:       // only return the decimal part
 122:       // final Double d = new Double(fromSerialDate.doubleValue()
 123:       // - fromSerialDate.intValue());
 124:       // return d;
 125:     }
 126:     else if (toType.isFlagSet(Type.DATE_TYPE))
 127:     {
 128:       return new Integer(fromSerialDate.intValue());
 129:     }
 130:     // datetime (java.util.Date)
 131:     else
 132:     {
 133:       return o;
 134:     }
 135:   }
 136: 
 137:   public static Date toJavaDate(final Number serialDate, final LocalizationContext context)
 138:   {
 139:     final Date javaDate = HSSFDateUtil.getJavaDate(serialDate.doubleValue());
 140:     // check for null (error)
 141:     final long l = (javaDate.getTime() / 1000) * 1000;
 142:     // final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 143:     // context.getLocale());
 144:     // gc.setTimeInMillis(serialDate.longValue() * MILLISECS_PER_DAY);
 145:     // return gc.getTime();
 146:     return new Date(l);
 147:   }
 148: 
 149:   public static Number toSerialDate(final Date date, final LocalizationContext context)
 150:   {
 151:     // final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 152:     // context.getLocale());
 153:     // gc.setTime(date);
 154:     // final double fraction = (((gc.get(Calendar.HOUR_OF_DAY) * 60 + gc
 155:     // .get(Calendar.MINUTE)) * 60 + gc.get(Calendar.SECOND)) * 1000 + gc
 156:     // .get(Calendar.MILLISECOND))
 157:     // / (double) MILLISECS_PER_DAY;
 158:     // final long timeInMillis = date.getTime();
 159:     // final long days = timeInMillis / MILLISECS_PER_DAY;
 160:     // return new BigDecimal((double) (days) + fraction);
 161:     final double serial = HSSFDateUtil.getExcelDate(date);
 162:     return new Double(serial);
 163:   }
 164: 
 165:   public static Date now(final LocalizationContext context)
 166:   {
 167:     final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 168:         context.getLocale());
 169:     gc.set(Calendar.MILLISECOND, 0);
 170: 
 171:     return gc.getTime();
 172:   }
 173: 
 174:   public static Date createDateTime(final int year, final int month, final int day, final int hour,
 175:       final int minute, final int second, final LocalizationContext context)
 176:   {
 177:     final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 178:         context.getLocale());
 179:     gc.set(Calendar.DAY_OF_MONTH, day);
 180:     gc.set(Calendar.MONTH, month);
 181:     gc.set(Calendar.YEAR, year);
 182:     gc.set(Calendar.MILLISECOND, 0);
 183:     gc.set(Calendar.HOUR_OF_DAY, hour);
 184:     gc.set(Calendar.MINUTE, minute);
 185:     gc.set(Calendar.SECOND, second);
 186:     return gc.getTime();
 187:   }
 188: 
 189:   public static Time createTime(final int hour, final int minute, final int second,
 190:       final LocalizationContext context)
 191:   {
 192:     final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 193:         context.getLocale());
 194:     gc.setTime(ISO8001_TIME);
 195:     gc.set(Calendar.MILLISECOND, 0);
 196:     gc.set(Calendar.HOUR_OF_DAY, hour);
 197:     gc.set(Calendar.MINUTE, minute);
 198:     gc.set(Calendar.SECOND, second);
 199:     return new Time(gc.getTime().getTime());
 200:   }
 201: 
 202:   public static java.sql.Date createDate(final int year, final int month, final int day,
 203:       final LocalizationContext context)
 204:   {
 205:     final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 206:         context.getLocale());
 207:     gc.set(Calendar.DAY_OF_MONTH, day);
 208:     gc.set(Calendar.MONTH, month - 1);
 209:     gc.set(Calendar.YEAR, year);
 210:     gc.set(Calendar.MILLISECOND, 0);
 211:     gc.set(Calendar.HOUR_OF_DAY, 0);
 212:     gc.set(Calendar.MINUTE, 0);
 213:     gc.set(Calendar.SECOND, 0);
 214:     return new java.sql.Date(gc.getTime().getTime());
 215:   }
 216: 
 217:   public static Calendar createCalendar(final Date date, final LocalizationContext context)
 218:   {
 219:     final GregorianCalendar gc = new GregorianCalendar(context.getTimeZone(),
 220:         context.getLocale());
 221:     gc.setTime(date);
 222:     return gc;
 223:   }
 224: 
 225: 
 226:   public static void main(final String[] args)
 227:   {
 228:     final DefaultLocalizationContext context = new DefaultLocalizationContext();
 229:     final java.sql.Date createDate = createDate(2006, 05, 01, context);
 230:     final Number serial = toSerialDate(createDate, context);
 231:     System.out.println(createDate);
 232:     System.out.println(serial);
 233:     final Date toJavaDate = toJavaDate(serial, context);
 234:     System.out.println(normalizeDate(toJavaDate, DateTimeType.DATE_TYPE));
 235:     System.out.println(toJavaDate);
 236:     System.out.println(HSSFDateUtil.getJavaDate(serial.doubleValue()));
 237:   }
 238:   
 239: }