extensions/net.sf.basedb.labenv/trunk/src/net/sf/basedb/labenv/util/ReportTableUtil.java

Code
Comments
Other
Rev Date Author Line
2303 02 Apr 14 olle 1 package net.sf.basedb.labenv.util;
2303 02 Apr 14 olle 2
2307 03 Apr 14 olle 3 import java.text.DateFormat;
2303 02 Apr 14 olle 4 import java.text.SimpleDateFormat;
2303 02 Apr 14 olle 5 import java.util.Calendar;
2303 02 Apr 14 olle 6 import java.util.Date;
2303 02 Apr 14 olle 7 import java.util.GregorianCalendar;
2303 02 Apr 14 olle 8 import java.util.HashMap;
2303 02 Apr 14 olle 9 import java.util.ArrayList;
2303 02 Apr 14 olle 10 import java.util.List;
2303 02 Apr 14 olle 11 import java.util.HashSet;
2303 02 Apr 14 olle 12 import java.util.Set;
2303 02 Apr 14 olle 13
2303 02 Apr 14 olle 14 import net.sf.basedb.labenv.converter.DateToStringConverter;
2303 02 Apr 14 olle 15
2303 02 Apr 14 olle 16 import org.json.simple.JSONObject;
2303 02 Apr 14 olle 17
2303 02 Apr 14 olle 18 public class ReportTableUtil
2303 02 Apr 14 olle 19 {
2303 02 Apr 14 olle 20   public static final String weekView = "WEEK";
2303 02 Apr 14 olle 21   public static final String monthView = "MONTH";
2303 02 Apr 14 olle 22   public static final String quarterView = "QUARTER";
2303 02 Apr 14 olle 23   public static final String yearView = "YEAR";
2303 02 Apr 14 olle 24   public static final String autoView = "AUTO";
2303 02 Apr 14 olle 25   
2303 02 Apr 14 olle 26   public static final int weekViewLimit = 3;
2303 02 Apr 14 olle 27   public static final int monthViewLimit = 13;
2303 02 Apr 14 olle 28   public static final int quarterViewLimit= 3;
2303 02 Apr 14 olle 29   
2303 02 Apr 14 olle 30   public static final String alphabeticalOrder = "ALPHABETICAL_ORDER";
2303 02 Apr 14 olle 31   public static final String namePrefixOrder = "NAME_PREFIX_ORDER";
2303 02 Apr 14 olle 32   public static final String startDateOrder = "START_DATE_ORDER";
2303 02 Apr 14 olle 33   public static final String nameLengthOrder = "NAME_LENGTH_ORDER";
2303 02 Apr 14 olle 34
2401 06 May 14 olle 35   DateFormat yearMonthDayPeriodDateFormat = null;
2401 06 May 14 olle 36
2401 06 May 14 olle 37   public DateFormat getYearMonthDayPeriodDateFormat()
2401 06 May 14 olle 38   {
2401 06 May 14 olle 39     if (this.yearMonthDayPeriodDateFormat == null)
2401 06 May 14 olle 40     {
2401 06 May 14 olle 41       this.yearMonthDayPeriodDateFormat = new SimpleDateFormat("YYYYMMdd");
2401 06 May 14 olle 42     }
2401 06 May 14 olle 43     return this.yearMonthDayPeriodDateFormat;
2401 06 May 14 olle 44   }
2401 06 May 14 olle 45
2308 03 Apr 14 olle 46   DateFormat hourPeriodDateFormat = null;
2308 03 Apr 14 olle 47
2308 03 Apr 14 olle 48   public DateFormat getHourPeriodDateFormat()
2308 03 Apr 14 olle 49   {
2308 03 Apr 14 olle 50     if (this.hourPeriodDateFormat == null)
2308 03 Apr 14 olle 51     {
2308 03 Apr 14 olle 52       this.hourPeriodDateFormat = new SimpleDateFormat("HH");
2308 03 Apr 14 olle 53     }
2308 03 Apr 14 olle 54     return this.hourPeriodDateFormat;
2308 03 Apr 14 olle 55   }
2308 03 Apr 14 olle 56
2401 06 May 14 olle 57   DateFormat hourMinutePeriodDateFormat = null;
2401 06 May 14 olle 58
2401 06 May 14 olle 59   public DateFormat getHourMinutePeriodDateFormat()
2401 06 May 14 olle 60   {
2401 06 May 14 olle 61     if (this.hourMinutePeriodDateFormat == null)
2401 06 May 14 olle 62     {
2401 06 May 14 olle 63       this.hourMinutePeriodDateFormat = new SimpleDateFormat("HHmm");
2401 06 May 14 olle 64     }
2401 06 May 14 olle 65     return this.hourMinutePeriodDateFormat;
2401 06 May 14 olle 66   }
2401 06 May 14 olle 67
2303 02 Apr 14 olle 68   public ReportTableUtil()
2303 02 Apr 14 olle 69   {}
2303 02 Apr 14 olle 70
2303 02 Apr 14 olle 71   /**
2303 02 Apr 14 olle 72    *  Returns true if a given date exists in the HashMap date set for the specified string key, else false.
2303 02 Apr 14 olle 73    *
2303 02 Apr 14 olle 74    *  @param stringDateSetHashMap HashMap<String, Set<Date>> The HashMap to check.
2303 02 Apr 14 olle 75    *  @param string String The string key.
2303 02 Apr 14 olle 76    *  @param date Date The date to check if it exists in the HashMap date set for the specified string key.
2303 02 Apr 14 olle 77    *  @return Boolean Returns true if the date exists in the HashMap date set for the specified string key, else false.
2303 02 Apr 14 olle 78    */
2303 02 Apr 14 olle 79   public Boolean inStringDateSetHashMap(HashMap<String, Set<Date>> stringDateSetHashMap, String string, Date date)
2303 02 Apr 14 olle 80   {
2303 02 Apr 14 olle 81     Boolean inHashMap = false;
2303 02 Apr 14 olle 82     if (stringDateSetHashMap != null && string != null && !string.equals("") && date != null)
2303 02 Apr 14 olle 83     {
2303 02 Apr 14 olle 84       Set<Date> dateSet = stringDateSetHashMap.get(string);
2303 02 Apr 14 olle 85       if (dateSet != null)
2303 02 Apr 14 olle 86       {
2303 02 Apr 14 olle 87         for (Date d: dateSet)
2303 02 Apr 14 olle 88         {
2303 02 Apr 14 olle 89           if (date.equals(d))
2303 02 Apr 14 olle 90           {
2303 02 Apr 14 olle 91             inHashMap = true;
2303 02 Apr 14 olle 92             break;
2303 02 Apr 14 olle 93           }
2303 02 Apr 14 olle 94         }
2303 02 Apr 14 olle 95       }
2303 02 Apr 14 olle 96     }
2303 02 Apr 14 olle 97     return inHashMap;
2303 02 Apr 14 olle 98   }
2303 02 Apr 14 olle 99
2303 02 Apr 14 olle 100   /**
2303 02 Apr 14 olle 101    *  Returns a HashMap updated with the new date in the date set for the specified string key.
2303 02 Apr 14 olle 102    *
2303 02 Apr 14 olle 103    *  @param stringDateSetHashMap HashMap<String, Set<Date>> The HashMap to update.
2303 02 Apr 14 olle 104    *  @param string String The string key for the sample.
2303 02 Apr 14 olle 105    *  @param date Date The date to update the date set for the specified string key in the HashMap with.
2303 02 Apr 14 olle 106    *  @return HashMap<String, Sample> A HashMap updated with the new date in the date set for the specified string key.
2303 02 Apr 14 olle 107    */
2303 02 Apr 14 olle 108   public HashMap<String, Set<Date>> updateStringDateSetHashMap(HashMap<String, Set<Date>> stringDateSetHashMap, String string, Date date)
2303 02 Apr 14 olle 109   {
2303 02 Apr 14 olle 110     if (stringDateSetHashMap == null)
2303 02 Apr 14 olle 111     {
2303 02 Apr 14 olle 112       stringDateSetHashMap = new HashMap<String, Set<Date>>();
2303 02 Apr 14 olle 113     }
2303 02 Apr 14 olle 114     if (string != null && !string.equals("") && date != null)
2303 02 Apr 14 olle 115     {
2303 02 Apr 14 olle 116       Set<Date> dateSet = stringDateSetHashMap.get(string);
2303 02 Apr 14 olle 117       Boolean dateInSet = false;
2303 02 Apr 14 olle 118       if (dateSet != null)
2303 02 Apr 14 olle 119       {
2303 02 Apr 14 olle 120         for (Date d: dateSet)
2303 02 Apr 14 olle 121         {
2303 02 Apr 14 olle 122           if (date.equals(d))
2303 02 Apr 14 olle 123           {
2303 02 Apr 14 olle 124             dateInSet = true;
2303 02 Apr 14 olle 125             break;
2303 02 Apr 14 olle 126           }
2303 02 Apr 14 olle 127         }
2303 02 Apr 14 olle 128       }
2303 02 Apr 14 olle 129       if (!dateInSet)
2303 02 Apr 14 olle 130       {
2303 02 Apr 14 olle 131         if (dateSet == null)
2303 02 Apr 14 olle 132         {
2303 02 Apr 14 olle 133           dateSet = new HashSet<Date>();
2303 02 Apr 14 olle 134         }
2303 02 Apr 14 olle 135         dateSet.add(date);
2303 02 Apr 14 olle 136         stringDateSetHashMap.put(string,  dateSet);
2303 02 Apr 14 olle 137       }
2303 02 Apr 14 olle 138     }
2303 02 Apr 14 olle 139     return stringDateSetHashMap;
2303 02 Apr 14 olle 140   }
2303 02 Apr 14 olle 141
2303 02 Apr 14 olle 142   /**
2303 02 Apr 14 olle 143    *  Returns JSONObject with incremented integer value counter for specified key.
2303 02 Apr 14 olle 144    *
2303 02 Apr 14 olle 145    *  @param jsonObject JSONObject The JSONObject with integer value counter for specified key.
2303 02 Apr 14 olle 146    *  @param key String The JSON key for the integer value counter.
2303 02 Apr 14 olle 147    *  @return JSONObject The JSONObject with incremented integer value counter for specified key.
2303 02 Apr 14 olle 148    */
2303 02 Apr 14 olle 149   public JSONObject updateJSONObjectCounter(JSONObject jsonObject, String key)
2303 02 Apr 14 olle 150   {
2303 02 Apr 14 olle 151     // Increase the counter for current key for the site
2303 02 Apr 14 olle 152     Integer counter = (Integer)jsonObject.get(key);
2303 02 Apr 14 olle 153     if (counter == null)
2303 02 Apr 14 olle 154     {
2303 02 Apr 14 olle 155       counter = 0;
2303 02 Apr 14 olle 156     }
2303 02 Apr 14 olle 157     counter++;
2303 02 Apr 14 olle 158     jsonObject.put(key, counter);
2303 02 Apr 14 olle 159     return jsonObject;
2303 02 Apr 14 olle 160   }
2303 02 Apr 14 olle 161
2303 02 Apr 14 olle 162   /**
2303 02 Apr 14 olle 163    *  Returns a JSONObject with period strings as keys and values set to 0 for periods after a given data start date.
2303 02 Apr 14 olle 164    *
2303 02 Apr 14 olle 165    *  @param jsonObject JSONObject The optional JSONObject to use (if null, a new JSONObject will be created).
2303 02 Apr 14 olle 166    *  @param startDate Date The start date for the JSONObject periods.
2303 02 Apr 14 olle 167    *  @param endDate Date The end date for the JSONObject periods.
2303 02 Apr 14 olle 168    *  @param dataStartDate Date The first date after which period date values will be initialized to 0. 
2303 02 Apr 14 olle 169    *  @param viewType String The view type to use.
2303 02 Apr 14 olle 170    *  @return JSONObject A JSONObject with period strings as keys and values set to 0.
2303 02 Apr 14 olle 171    */
2303 02 Apr 14 olle 172   public JSONObject initializeJSONPeriodData(JSONObject jsonObject, Date startDate, Date endDate, Date dataStartDate, String viewType)
2303 02 Apr 14 olle 173   {
2303 02 Apr 14 olle 174     if (jsonObject == null)
2303 02 Apr 14 olle 175     {
2303 02 Apr 14 olle 176       jsonObject = new JSONObject();
2303 02 Apr 14 olle 177     }
2303 02 Apr 14 olle 178     if (startDate != null && endDate != null)
2303 02 Apr 14 olle 179     {
2303 02 Apr 14 olle 180       // Fill period slots with zeroes beginning with the dataStartDate
2303 02 Apr 14 olle 181       List<Date> sortedPeriodEndDateList = createSortedPeriodEndDateList(startDate, endDate, viewType);
2303 02 Apr 14 olle 182       for (Date d: sortedPeriodEndDateList)
2303 02 Apr 14 olle 183       {
2303 02 Apr 14 olle 184         if (dataStartDate != null)
2303 02 Apr 14 olle 185         {
2303 02 Apr 14 olle 186           if (d.after(dataStartDate))
2303 02 Apr 14 olle 187           {
2303 02 Apr 14 olle 188             // Set data for period to 0
4526 19 Jun 17 nicklas 189             String currentPeriod = getCurrentPeriod(d, viewType, startDate);
2303 02 Apr 14 olle 190             jsonObject.put(currentPeriod, 0);
2303 02 Apr 14 olle 191           }
2303 02 Apr 14 olle 192         }
2303 02 Apr 14 olle 193       }
2303 02 Apr 14 olle 194     }
2303 02 Apr 14 olle 195     return jsonObject;
2303 02 Apr 14 olle 196   }
2303 02 Apr 14 olle 197
2303 02 Apr 14 olle 198   /**
2303 02 Apr 14 olle 199    *  Gets period string given date and view type.
2303 02 Apr 14 olle 200    *
2303 02 Apr 14 olle 201    *  @param currentDateTime Date The dateTime to get period string for.
2303 02 Apr 14 olle 202    *  @param viewType String The view type to use.
2303 02 Apr 14 olle 203    *  @return String The period string for the given date and view type.
2303 02 Apr 14 olle 204    */
4526 19 Jun 17 nicklas 205   public String getCurrentPeriod(Date currentDateTime, String viewType, Date startDate) 
2303 02 Apr 14 olle 206   {    
2303 02 Apr 14 olle 207     String currentPeriod = null;
4526 19 Jun 17 nicklas 208     if (viewType.equals("last24hours"))
2303 02 Apr 14 olle 209     {
4526 19 Jun 17 nicklas 210       // Period string is minutes since start date (whole hour), 0 - 1440 (24 * 60)
4526 19 Jun 17 nicklas 211       long startHour = startDate.getTime() / 1000L / 3600L;
4526 19 Jun 17 nicklas 212       long currentMinutes = currentDateTime.getTime() / 1000L / 60L;
4526 19 Jun 17 nicklas 213       long periodMinutes = currentMinutes - startHour * 60;
4526 19 Jun 17 nicklas 214       if (periodMinutes < 10)
4526 19 Jun 17 nicklas 215       {
4526 19 Jun 17 nicklas 216         currentPeriod = "000" + periodMinutes;
4526 19 Jun 17 nicklas 217       }
4526 19 Jun 17 nicklas 218       else if (periodMinutes < 100)
4526 19 Jun 17 nicklas 219       {
4526 19 Jun 17 nicklas 220         currentPeriod = "00" + periodMinutes;
4526 19 Jun 17 nicklas 221       }
4526 19 Jun 17 nicklas 222       else if (periodMinutes < 1000)
4526 19 Jun 17 nicklas 223       {
4526 19 Jun 17 nicklas 224         currentPeriod = "0" + periodMinutes;
4526 19 Jun 17 nicklas 225       }
4526 19 Jun 17 nicklas 226       else
4526 19 Jun 17 nicklas 227       {
4526 19 Jun 17 nicklas 228         currentPeriod = Long.toString(periodMinutes);
4526 19 Jun 17 nicklas 229       }
4526 19 Jun 17 nicklas 230     }
4526 19 Jun 17 nicklas 231     else if (viewType.equals("dailyDistribution"))
4526 19 Jun 17 nicklas 232     {
2307 03 Apr 14 olle 233       // Period string is hour after midnight, with initial '0' if needed
2308 03 Apr 14 olle 234       // This algorithm will hopefully be correct also at start and end of daylight saving time
2308 03 Apr 14 olle 235       DateFormat dateFormat = getHourPeriodDateFormat();
2303 02 Apr 14 olle 236       currentPeriod = "";
2307 03 Apr 14 olle 237       if (currentDateTime != null)
2303 02 Apr 14 olle 238       {
2307 03 Apr 14 olle 239         currentPeriod = dateFormat.format(currentDateTime);
2303 02 Apr 14 olle 240       }
2307 03 Apr 14 olle 241       
2303 02 Apr 14 olle 242     }    
2303 02 Apr 14 olle 243     else if (viewType.equals("weeklyDistribution"))
2303 02 Apr 14 olle 244     {
2401 06 May 14 olle 245       currentPeriod = getDayOfWeekString(currentDateTime, true);
2401 06 May 14 olle 246     }
2401 06 May 14 olle 247     else if (viewType.equals("dailyDisplay"))
2401 06 May 14 olle 248     {
2401 06 May 14 olle 249       // Period string is minutes after midnight, 0 - 1440 (24 * 60)
2401 06 May 14 olle 250       // This algorithm will hopefully be correct also at start and end of daylight saving time
2401 06 May 14 olle 251       DateFormat dateFormat = getHourMinutePeriodDateFormat();
2401 06 May 14 olle 252       currentPeriod = "";
2401 06 May 14 olle 253       if (currentDateTime != null)
2401 06 May 14 olle 254       {
2401 06 May 14 olle 255         currentPeriod = dateFormat.format(currentDateTime);
2401 06 May 14 olle 256         // Get hour string without initial 0
2401 06 May 14 olle 257         String hourStr = currentPeriod.substring(0,2);
2401 06 May 14 olle 258         if (hourStr.startsWith("0"))
2401 06 May 14 olle 259         {
2401 06 May 14 olle 260           hourStr = hourStr.substring(1);
2401 06 May 14 olle 261         }
2401 06 May 14 olle 262         // Get minute string without initial 0
2401 06 May 14 olle 263         String minStr = currentPeriod.substring(2);
2401 06 May 14 olle 264         if (minStr.startsWith("0"))
2401 06 May 14 olle 265         {
2401 06 May 14 olle 266           minStr = minStr.substring(1);
2401 06 May 14 olle 267         }
2401 06 May 14 olle 268         int hour = Integer.parseInt(hourStr);
2401 06 May 14 olle 269         int min = Integer.parseInt(minStr);
2401 06 May 14 olle 270         int minutesSinceMidnight = 60*hour + min;
2401 06 May 14 olle 271         currentPeriod = "" + minutesSinceMidnight;
2401 06 May 14 olle 272         if (minutesSinceMidnight < 10)
2401 06 May 14 olle 273         {
2401 06 May 14 olle 274           currentPeriod = "000" + currentPeriod;
2401 06 May 14 olle 275         }
2401 06 May 14 olle 276         else if (minutesSinceMidnight < 100)
2401 06 May 14 olle 277         {
2401 06 May 14 olle 278           currentPeriod = "00" + currentPeriod;
2401 06 May 14 olle 279         }
2401 06 May 14 olle 280         else if (minutesSinceMidnight < 1000)
2401 06 May 14 olle 281         {
2401 06 May 14 olle 282           currentPeriod = "0" + currentPeriod;
2401 06 May 14 olle 283         }
2401 06 May 14 olle 284       }      
2401 06 May 14 olle 285     }    
2401 06 May 14 olle 286     else if (viewType.equals("weeklyDisplay"))
2401 06 May 14 olle 287     {
2401 06 May 14 olle 288       // Period string is number of minutes after 1970-01-01 00:00:00
2401 06 May 14 olle 289       // 2000-01-01 00:00:00 ~ 15 778 080 min, 2100-01-01 00:00:00 ~ 68 374 080; no initial zeroes needed until 2160-02-18 10:40:00
2401 06 May 14 olle 290       currentPeriod = "";
2401 06 May 14 olle 291       if (currentDateTime != null)
2401 06 May 14 olle 292       {
2401 06 May 14 olle 293         long minutesSinceEpoch = currentDateTime.getTime()/(1000L*60L);
2401 06 May 14 olle 294         currentPeriod = "" + minutesSinceEpoch;
2401 06 May 14 olle 295       }      
2401 06 May 14 olle 296     }
2401 06 May 14 olle 297     else if (viewType.equals("overviewDisplay"))
2401 06 May 14 olle 298     {
2401 06 May 14 olle 299       // Period string is number of minutes after 1970-01-01 00:00:00
2401 06 May 14 olle 300       // 2000-01-01 00:00:00 ~ 15 778 080 min, 2100-01-01 00:00:00 ~ 68 374 080; no initial zeroes needed until 2160-02-18 10:40:00
2401 06 May 14 olle 301       currentPeriod = "";
2401 06 May 14 olle 302       if (currentDateTime != null)
2401 06 May 14 olle 303       {
2401 06 May 14 olle 304         long minutesSinceEpoch = currentDateTime.getTime()/(1000L*60L);
2401 06 May 14 olle 305         currentPeriod = "" + minutesSinceEpoch;
2401 06 May 14 olle 306       }      
2401 06 May 14 olle 307     }
2401 06 May 14 olle 308     return currentPeriod;
2401 06 May 14 olle 309   }
2401 06 May 14 olle 310
2401 06 May 14 olle 311   /**
2401 06 May 14 olle 312    *  Gets day of week string given date. Returns `null` if date is `null`. 
2401 06 May 14 olle 313    *
2401 06 May 14 olle 314    *  @param date Date The date to get day of week string for.
2401 06 May 14 olle 315    *  @param addNumberPrefix boolean Flag indicating if a number prefix should be added for sorting purposes.
2401 06 May 14 olle 316    *  @return String The day of week string for the given date, with optional number prefix.
2401 06 May 14 olle 317    */
2401 06 May 14 olle 318   public String getDayOfWeekString(Date date, boolean addNumberPrefix)
2401 06 May 14 olle 319   {    
2401 06 May 14 olle 320     String dayOfWeekStr = null;
2401 06 May 14 olle 321     if (date != null)
2401 06 May 14 olle 322     {
2303 02 Apr 14 olle 323       Calendar cal = GregorianCalendar.getInstance();
2401 06 May 14 olle 324       cal.setTime(date);
2303 02 Apr 14 olle 325       int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
2303 02 Apr 14 olle 326       switch (dayOfWeek)
2303 02 Apr 14 olle 327       {
2303 02 Apr 14 olle 328         case Calendar.MONDAY:
2401 06 May 14 olle 329           dayOfWeekStr = "1_Mon";
2303 02 Apr 14 olle 330           break;
2303 02 Apr 14 olle 331         case Calendar.TUESDAY:
2401 06 May 14 olle 332           dayOfWeekStr = "2_Tue";
2303 02 Apr 14 olle 333           break;
2303 02 Apr 14 olle 334         case Calendar.WEDNESDAY:
2401 06 May 14 olle 335           dayOfWeekStr = "3_Wed";
2303 02 Apr 14 olle 336           break;
2303 02 Apr 14 olle 337         case Calendar.THURSDAY:
2401 06 May 14 olle 338           dayOfWeekStr = "4_Thu";
2303 02 Apr 14 olle 339           break;
2303 02 Apr 14 olle 340         case Calendar.FRIDAY:
2401 06 May 14 olle 341           dayOfWeekStr = "5_Fri";
2303 02 Apr 14 olle 342           break;
2303 02 Apr 14 olle 343         case Calendar.SATURDAY:
2401 06 May 14 olle 344           dayOfWeekStr = "6_Sat";
2303 02 Apr 14 olle 345           break;
2303 02 Apr 14 olle 346         case Calendar.SUNDAY:
2401 06 May 14 olle 347           dayOfWeekStr = "7_Sun";
2303 02 Apr 14 olle 348           break;
2303 02 Apr 14 olle 349       }
2401 06 May 14 olle 350       if (!addNumberPrefix)
2401 06 May 14 olle 351       {
2401 06 May 14 olle 352         // Remove number prefix
2401 06 May 14 olle 353         dayOfWeekStr = dayOfWeekStr.substring(2);
2401 06 May 14 olle 354       }
2303 02 Apr 14 olle 355     }
2401 06 May 14 olle 356     return dayOfWeekStr;
2303 02 Apr 14 olle 357   }
2303 02 Apr 14 olle 358
2303 02 Apr 14 olle 359   /**
2303 02 Apr 14 olle 360    *  Gets period string given date and view type.
2303 02 Apr 14 olle 361    *
2303 02 Apr 14 olle 362    *  @param currentDate Date The date to get period string for.
2303 02 Apr 14 olle 363    *  @param viewType String The view type to use.
2303 02 Apr 14 olle 364    *  @return String The period string for the given date and view type.
2303 02 Apr 14 olle 365    */
2303 02 Apr 14 olle 366   public String getCurrentPeriodOld(Date currentDate, String viewType) 
2303 02 Apr 14 olle 367   {    
2303 02 Apr 14 olle 368     String currentPeriod = null;
2303 02 Apr 14 olle 369     if (viewType.equals(quarterView))
2303 02 Apr 14 olle 370     {
2303 02 Apr 14 olle 371       DateToStringConverter converter = new DateToStringConverter(new SimpleDateFormat("yyyy"));
2303 02 Apr 14 olle 372       currentPeriod = converter.convert(currentDate);
2303 02 Apr 14 olle 373       Calendar cal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 374       cal.setTime(currentDate);
2303 02 Apr 14 olle 375       int month = cal.get(Calendar.MONTH);
2303 02 Apr 14 olle 376       if (month < 3) 
2303 02 Apr 14 olle 377       {
2303 02 Apr 14 olle 378         currentPeriod += "1";
2303 02 Apr 14 olle 379       }
2303 02 Apr 14 olle 380       else if (month < 6) 
2303 02 Apr 14 olle 381       {
2303 02 Apr 14 olle 382         currentPeriod += "2";
2303 02 Apr 14 olle 383       }
2303 02 Apr 14 olle 384       else if (month < 9) 
2303 02 Apr 14 olle 385       {
2303 02 Apr 14 olle 386         currentPeriod +="3";
2303 02 Apr 14 olle 387       }
2303 02 Apr 14 olle 388       else
2303 02 Apr 14 olle 389       {
2303 02 Apr 14 olle 390         currentPeriod += "4";
2303 02 Apr 14 olle 391       }
2303 02 Apr 14 olle 392     }
2303 02 Apr 14 olle 393     else if (viewType.equals(weekView))
2303 02 Apr 14 olle 394     {
2303 02 Apr 14 olle 395       // Get year number
2303 02 Apr 14 olle 396       DateToStringConverter converter = new DateToStringConverter(new SimpleDateFormat("yyyy"));
2303 02 Apr 14 olle 397       currentPeriod = converter.convert(currentDate);
2303 02 Apr 14 olle 398       // Use Calendar to get ISO week number
2303 02 Apr 14 olle 399       Calendar cal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 400       cal.setFirstDayOfWeek(Calendar.MONDAY);
2303 02 Apr 14 olle 401       cal.setMinimalDaysInFirstWeek(4);
2303 02 Apr 14 olle 402       cal.setTime(currentDate);
2303 02 Apr 14 olle 403       int week = cal.get(Calendar.WEEK_OF_YEAR);
2303 02 Apr 14 olle 404       // Add week number to year number to get week in "yyyyww" format  with leading 0 if needed
2303 02 Apr 14 olle 405       if (week < 10)
2303 02 Apr 14 olle 406       {
2303 02 Apr 14 olle 407         currentPeriod += "0" + Integer.toString(week);
2303 02 Apr 14 olle 408       }
2303 02 Apr 14 olle 409       else
2303 02 Apr 14 olle 410       {
2303 02 Apr 14 olle 411         currentPeriod += Integer.toString(week);
2303 02 Apr 14 olle 412       }
2303 02 Apr 14 olle 413     }
2303 02 Apr 14 olle 414     else
2303 02 Apr 14 olle 415     {      
2303 02 Apr 14 olle 416       String dateFormat = null;
2303 02 Apr 14 olle 417       if (viewType.equals(monthView)) 
2303 02 Apr 14 olle 418       {
2303 02 Apr 14 olle 419         dateFormat = "yyyyMM";
2303 02 Apr 14 olle 420       }
2303 02 Apr 14 olle 421       else 
2303 02 Apr 14 olle 422       {
2303 02 Apr 14 olle 423         dateFormat = "yyyy";
2303 02 Apr 14 olle 424       }
2303 02 Apr 14 olle 425       DateToStringConverter converter = new DateToStringConverter(new SimpleDateFormat(dateFormat));
2303 02 Apr 14 olle 426       currentPeriod = converter.convert(currentDate);
2303 02 Apr 14 olle 427     }
2303 02 Apr 14 olle 428     
2303 02 Apr 14 olle 429     return currentPeriod;
2303 02 Apr 14 olle 430   }
2303 02 Apr 14 olle 431
2303 02 Apr 14 olle 432   /**
2303 02 Apr 14 olle 433    *  Gets view type string for given start and end dates.
2303 02 Apr 14 olle 434    *  The view type is determined by the first of the following conditions, that is true:<BR>
2303 02 Apr 14 olle 435    *  1. Period shorter than 3 months - week view.<BR>
2303 02 Apr 14 olle 436    *  2. Period shorter than 13 months - month view.<BR>
2303 02 Apr 14 olle 437    *  3. Period shorter than 3 years - quarter view.<BR>
2303 02 Apr 14 olle 438    *  4. Period is 3 years or greater - year view.<BR>
2303 02 Apr 14 olle 439    *
2303 02 Apr 14 olle 440    *  @param start Date The start date.
2303 02 Apr 14 olle 441    *  @param end Date The end date.
2303 02 Apr 14 olle 442    *  @return String The view type string for the given start and end dates.
2303 02 Apr 14 olle 443    */
2303 02 Apr 14 olle 444   public String getViewType(Date start, Date end)
2303 02 Apr 14 olle 445   {
2303 02 Apr 14 olle 446     String viewType = null;
2303 02 Apr 14 olle 447     
2303 02 Apr 14 olle 448     Calendar cal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 449     cal.setTime(start);
2303 02 Apr 14 olle 450     
2303 02 Apr 14 olle 451     Calendar endCal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 452     endCal.setTime(end);
2303 02 Apr 14 olle 453     
2303 02 Apr 14 olle 454     Calendar limitWeekView = (Calendar)cal.clone();    
2303 02 Apr 14 olle 455     limitWeekView.add(Calendar.MONTH, weekViewLimit);
2303 02 Apr 14 olle 456     int daysToWeekend = 9-limitWeekView.get(Calendar.DAY_OF_WEEK);
2303 02 Apr 14 olle 457     limitWeekView.add(Calendar.DATE, daysToWeekend);
2303 02 Apr 14 olle 458     
2303 02 Apr 14 olle 459     Calendar limitMonthView = (Calendar)cal.clone();
2303 02 Apr 14 olle 460     limitMonthView.add(Calendar.MONTH, monthViewLimit+1);
2303 02 Apr 14 olle 461     limitMonthView.add(Calendar.DATE, -1);
2303 02 Apr 14 olle 462     
2303 02 Apr 14 olle 463     Calendar limitQuarterView = (Calendar)cal.clone();    
2303 02 Apr 14 olle 464     limitQuarterView.add(Calendar.YEAR, quarterViewLimit);
2303 02 Apr 14 olle 465     int currentQuarter = limitQuarterView.get(Calendar.MONTH)/3;
2303 02 Apr 14 olle 466     limitQuarterView.add(Calendar.MONTH, (3*currentQuarter)-limitQuarterView.get(Calendar.MONTH));
2303 02 Apr 14 olle 467                 
2303 02 Apr 14 olle 468     if (limitWeekView.after(endCal)) 
2303 02 Apr 14 olle 469     {
2303 02 Apr 14 olle 470       viewType = weekView;
2303 02 Apr 14 olle 471     }
2303 02 Apr 14 olle 472     else if (limitMonthView.after(endCal)) 
2303 02 Apr 14 olle 473     {
2303 02 Apr 14 olle 474       viewType = monthView;
2303 02 Apr 14 olle 475     }
2303 02 Apr 14 olle 476     else if (limitQuarterView.after(endCal)) 
2303 02 Apr 14 olle 477     {
2303 02 Apr 14 olle 478       viewType = quarterView;
2303 02 Apr 14 olle 479     }
2303 02 Apr 14 olle 480     else 
2303 02 Apr 14 olle 481     {
2303 02 Apr 14 olle 482       viewType = yearView;
2303 02 Apr 14 olle 483     }
2303 02 Apr 14 olle 484     
2303 02 Apr 14 olle 485     return viewType;
2303 02 Apr 14 olle 486   }
2303 02 Apr 14 olle 487
2303 02 Apr 14 olle 488   /**
2303 02 Apr 14 olle 489    *  Creates a sorted list of period start dates for given start date, end date, and view type.
2303 02 Apr 14 olle 490    *  The returned start dates will have its time set to midnight 00:00:00.
2303 02 Apr 14 olle 491    *
2303 02 Apr 14 olle 492    *  @param startDate Date The start date.
2303 02 Apr 14 olle 493    *  @param endDate Date The end date.
2303 02 Apr 14 olle 494    *  @param viewType String The view type to use.
2303 02 Apr 14 olle 495    *  @return List<Date> A sorted list of period start dates for given start date, end date, and view type.
2303 02 Apr 14 olle 496    */
2303 02 Apr 14 olle 497   public List<Date> createSortedPeriodStartDateList(Date startDate, Date endDate, String viewType)
2303 02 Apr 14 olle 498   {
2303 02 Apr 14 olle 499     List<Date> sortedPeriodStartDateList = new ArrayList<Date>();
2303 02 Apr 14 olle 500     Date periodStartDate = calculatePeriodStartDate(startDate, viewType);
2303 02 Apr 14 olle 501     Calendar cal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 502     cal.setTime(periodStartDate);
2303 02 Apr 14 olle 503     Calendar endCal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 504     endCal.setTime(endDate);
2303 02 Apr 14 olle 505     while (cal.before(endCal) || cal.equals(endCal))
2303 02 Apr 14 olle 506     {
2303 02 Apr 14 olle 507       sortedPeriodStartDateList.add(cal.getTime());
2303 02 Apr 14 olle 508       // Advance to start of next time period
2303 02 Apr 14 olle 509       int year = cal.get(Calendar.YEAR);
2303 02 Apr 14 olle 510       int month = cal.get(Calendar.MONTH);
2303 02 Apr 14 olle 511       int day = cal.get(Calendar.DAY_OF_MONTH);
2303 02 Apr 14 olle 512       if (viewType.equals(yearView))
2303 02 Apr 14 olle 513       {
2303 02 Apr 14 olle 514         cal.set(year+1, month, day);
2303 02 Apr 14 olle 515       }
2303 02 Apr 14 olle 516       else if (viewType.equals(quarterView))
2303 02 Apr 14 olle 517       {
2303 02 Apr 14 olle 518         cal.set(year, month+3, day);
2303 02 Apr 14 olle 519       }
2303 02 Apr 14 olle 520       else if (viewType.equals(monthView))
2303 02 Apr 14 olle 521       {
2303 02 Apr 14 olle 522         cal.set(year, month+1, day);
2303 02 Apr 14 olle 523       }
2303 02 Apr 14 olle 524       else if (viewType.equals(weekView))
2303 02 Apr 14 olle 525       {
2303 02 Apr 14 olle 526         cal.set(year, month, day+7);
2303 02 Apr 14 olle 527       }
2303 02 Apr 14 olle 528     }
2303 02 Apr 14 olle 529     return sortedPeriodStartDateList;
2303 02 Apr 14 olle 530   }
2303 02 Apr 14 olle 531   
2303 02 Apr 14 olle 532   /**
2303 02 Apr 14 olle 533    *  Creates a sorted list of period end dates for given start date, end date, and view type.
2303 02 Apr 14 olle 534    *  The returned end dates will have its time set to one second before midnight 23:59:59.
2303 02 Apr 14 olle 535    *
2303 02 Apr 14 olle 536    *  @param startDate Date The start date.
2303 02 Apr 14 olle 537    *  @param endDate Date The end date.
2303 02 Apr 14 olle 538    *  @param viewType String The view type to use.
2303 02 Apr 14 olle 539    *  @return List<Date> A sorted list of period start dates for given start date, end date, and view type.
2303 02 Apr 14 olle 540    */
2303 02 Apr 14 olle 541   public List<Date> createSortedPeriodEndDateList(Date startDate, Date endDate, String viewType)
2303 02 Apr 14 olle 542   {
2303 02 Apr 14 olle 543     List<Date> sortedPeriodEndDateList = new ArrayList<Date>();
2303 02 Apr 14 olle 544     Date periodStartDate = calculatePeriodStartDate(startDate, viewType);
2303 02 Apr 14 olle 545     Calendar cal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 546     cal.setTime(periodStartDate);
2303 02 Apr 14 olle 547     Calendar endCal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 548     endCal.setTime(endDate);
2303 02 Apr 14 olle 549     while (cal.before(endCal) || cal.equals(endCal))
2303 02 Apr 14 olle 550     {
2303 02 Apr 14 olle 551       // Advance to start of next time period
2303 02 Apr 14 olle 552       int year = cal.get(Calendar.YEAR);
2303 02 Apr 14 olle 553       int month = cal.get(Calendar.MONTH);
2303 02 Apr 14 olle 554       int day = cal.get(Calendar.DAY_OF_MONTH);
2303 02 Apr 14 olle 555       if (viewType.equals(yearView))
2303 02 Apr 14 olle 556       {
2303 02 Apr 14 olle 557         cal.set(year+1, month, day);
2303 02 Apr 14 olle 558       }
2303 02 Apr 14 olle 559       else if (viewType.equals(quarterView))
2303 02 Apr 14 olle 560       {
2303 02 Apr 14 olle 561         cal.set(year, month+3, day);
2303 02 Apr 14 olle 562       }
2303 02 Apr 14 olle 563       else if (viewType.equals(monthView))
2303 02 Apr 14 olle 564       {
2303 02 Apr 14 olle 565         cal.set(year, month+1, day);
2303 02 Apr 14 olle 566       }
2303 02 Apr 14 olle 567       else if (viewType.equals(weekView))
2303 02 Apr 14 olle 568       {
2303 02 Apr 14 olle 569         cal.set(year, month, day+7);
2303 02 Apr 14 olle 570       }
2303 02 Apr 14 olle 571       // Get original period end date by subtracting one second from start of next period
2303 02 Apr 14 olle 572       long timeInMs = cal.getTimeInMillis();
2303 02 Apr 14 olle 573       Calendar periodEndCal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 574       periodEndCal.setTimeInMillis(timeInMs - 1000);      
2303 02 Apr 14 olle 575       sortedPeriodEndDateList.add(periodEndCal.getTime());
2303 02 Apr 14 olle 576     }
2303 02 Apr 14 olle 577     return sortedPeriodEndDateList;
2303 02 Apr 14 olle 578   }
2303 02 Apr 14 olle 579   
2303 02 Apr 14 olle 580   /**
2303 02 Apr 14 olle 581    *  Calculates the first date in a period for given date and view type.
2303 02 Apr 14 olle 582    *  The returned start date will have its time reset to midnight 00:00:00.
2303 02 Apr 14 olle 583    *
2303 02 Apr 14 olle 584    *  @param date Date The date.
2303 02 Apr 14 olle 585    *  @param viewType String The view type to use.
2303 02 Apr 14 olle 586    *  @return Date The first date in a period for given date and view type, with time reset to midnight 00:00:00.
2303 02 Apr 14 olle 587    */
2303 02 Apr 14 olle 588   public Date calculatePeriodStartDate(Date date, String viewType)
2303 02 Apr 14 olle 589   {
2303 02 Apr 14 olle 590     Date tmpDate = null;
2303 02 Apr 14 olle 591     if (viewType.equals(weekView)) 
2303 02 Apr 14 olle 592     {
2303 02 Apr 14 olle 593       tmpDate = mondayInISOWeek(date);
2303 02 Apr 14 olle 594     }
2303 02 Apr 14 olle 595     else if (viewType.equals(monthView))
2303 02 Apr 14 olle 596     {
2303 02 Apr 14 olle 597       tmpDate = firstDayInMonth(date);
2303 02 Apr 14 olle 598     }
2303 02 Apr 14 olle 599     else if (viewType.equals(quarterView))
2303 02 Apr 14 olle 600     {
2303 02 Apr 14 olle 601       tmpDate = firstDayInQuarter(date);
2303 02 Apr 14 olle 602     }
2303 02 Apr 14 olle 603     else if (viewType.equals(yearView))
2303 02 Apr 14 olle 604     {
2303 02 Apr 14 olle 605       tmpDate = firstDayInYear(date);
2303 02 Apr 14 olle 606     }
2303 02 Apr 14 olle 607     Date periodDate = adjustDayTime(tmpDate, 0, 0, 0);
2303 02 Apr 14 olle 608     return periodDate;
2303 02 Apr 14 olle 609   }
2303 02 Apr 14 olle 610
2303 02 Apr 14 olle 611   /**
2303 02 Apr 14 olle 612    *  Returns the date for the Monday in the ISO week containing the given date.
2303 02 Apr 14 olle 613    *  The time of day of the returned date is equal to that of the input date.
2303 02 Apr 14 olle 614    *
2303 02 Apr 14 olle 615    *  @param date Date The date.
2303 02 Apr 14 olle 616    *  @return Date The date for the Monday in the ISO week containing the given date, with retained time of day.
2303 02 Apr 14 olle 617    */
2303 02 Apr 14 olle 618   public Date mondayInISOWeek(Date date)
2303 02 Apr 14 olle 619   {
2303 02 Apr 14 olle 620     // Get weekday number; 1 => Sunday, 2 => Monday, ... , 7 => Saturday
2303 02 Apr 14 olle 621     Calendar cal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 622     cal.setTime(date);
2303 02 Apr 14 olle 623     int weekdayNumber = cal.get(Calendar.DAY_OF_WEEK);
2303 02 Apr 14 olle 624     // Get weekday offset from Monday; 6 => Sunday, 0 => Monday, ... , 5 => Saturday
2303 02 Apr 14 olle 625     int offset = weekdayNumber - 2;
2303 02 Apr 14 olle 626     if (offset < 0)
2303 02 Apr 14 olle 627     {
2303 02 Apr 14 olle 628       offset = offset + 7;
2303 02 Apr 14 olle 629     }
2303 02 Apr 14 olle 630     // Get date for Monday in ISO week
2303 02 Apr 14 olle 631     long msSince19700101 = date.getTime() - offset*24*60*60*1000;
2303 02 Apr 14 olle 632     Date dateMondayInWeek = new Date(msSince19700101);
2303 02 Apr 14 olle 633     return dateMondayInWeek;
2303 02 Apr 14 olle 634   }
2303 02 Apr 14 olle 635
2303 02 Apr 14 olle 636   /**
2303 02 Apr 14 olle 637    *  Returns the date for the first day of the month containing the given date.
2303 02 Apr 14 olle 638    *  The time of day of the returned date is equal to that of the input date.
2303 02 Apr 14 olle 639    *
2303 02 Apr 14 olle 640    *  @param date Date The date.
2303 02 Apr 14 olle 641    *  @return Date The date for the first day of the month containing the given date, with retained time of day.
2303 02 Apr 14 olle 642    */
2303 02 Apr 14 olle 643   public Date firstDayInMonth(Date date)
2303 02 Apr 14 olle 644   {
2303 02 Apr 14 olle 645     // Get year and month;
2303 02 Apr 14 olle 646     Calendar cal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 647     cal.setTime(date);
2303 02 Apr 14 olle 648     int year = cal.get(Calendar.YEAR);
2303 02 Apr 14 olle 649     int month = cal.get(Calendar.MONTH);
2303 02 Apr 14 olle 650     // Get date for first day in month
2303 02 Apr 14 olle 651     cal.set(year, month, 1);
2303 02 Apr 14 olle 652     Date dateFirstDayInMonth = cal.getTime();
2303 02 Apr 14 olle 653     return dateFirstDayInMonth;
2303 02 Apr 14 olle 654   }
2303 02 Apr 14 olle 655
2303 02 Apr 14 olle 656   /**
2303 02 Apr 14 olle 657    *  Returns the date for the first day of the quarter containing the given date.
2303 02 Apr 14 olle 658    *  The time of day of the returned date is equal to that of the input date.
2303 02 Apr 14 olle 659    *
2303 02 Apr 14 olle 660    *  @param date Date The date.
2303 02 Apr 14 olle 661    *  @return Date The date for the first day of the quarter containing the given date, with retained time of day.
2303 02 Apr 14 olle 662    */
2303 02 Apr 14 olle 663   public Date firstDayInQuarter(Date date)
2303 02 Apr 14 olle 664   {
2303 02 Apr 14 olle 665     // Get year and month;
2303 02 Apr 14 olle 666     Calendar cal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 667     cal.setTime(date);
2303 02 Apr 14 olle 668     int year = cal.get(Calendar.YEAR);
2303 02 Apr 14 olle 669     int month = cal.get(Calendar.MONTH);
2303 02 Apr 14 olle 670     // Get first month of quarter;  0 => January, 1 => February, ... , 11 => December
2303 02 Apr 14 olle 671     if (month < 3)
2303 02 Apr 14 olle 672     {
2303 02 Apr 14 olle 673       month = 0;
2303 02 Apr 14 olle 674     }
2303 02 Apr 14 olle 675     else if (month < 6)
2303 02 Apr 14 olle 676     {
2303 02 Apr 14 olle 677       month = 3;
2303 02 Apr 14 olle 678     }
2303 02 Apr 14 olle 679     else if (month < 9)
2303 02 Apr 14 olle 680     {
2303 02 Apr 14 olle 681       month = 6;
2303 02 Apr 14 olle 682     }
2303 02 Apr 14 olle 683     else
2303 02 Apr 14 olle 684     {
2303 02 Apr 14 olle 685       month = 9;
2303 02 Apr 14 olle 686     }
2303 02 Apr 14 olle 687     // Get date for first day in quarter
2303 02 Apr 14 olle 688     cal.set(year, month, 1);
2303 02 Apr 14 olle 689     Date dateFirstDayInQuarter = cal.getTime();
2303 02 Apr 14 olle 690     return dateFirstDayInQuarter;
2303 02 Apr 14 olle 691   }
2303 02 Apr 14 olle 692
2303 02 Apr 14 olle 693   /**
2303 02 Apr 14 olle 694    *  Returns the date for the first day of the year containing the given date.
2303 02 Apr 14 olle 695    *  The time of day of the returned date is equal to that of the input date.
2303 02 Apr 14 olle 696    *
2303 02 Apr 14 olle 697    *  @param date Date The date.
2303 02 Apr 14 olle 698    *  @return Date The date for the first day of the year containing the given date, with retained time of day.
2303 02 Apr 14 olle 699    */
2303 02 Apr 14 olle 700   public Date firstDayInYear(Date date)
2303 02 Apr 14 olle 701   {
2303 02 Apr 14 olle 702     // Get year;
2303 02 Apr 14 olle 703     Calendar cal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 704     cal.setTime(date);
2303 02 Apr 14 olle 705     int year = cal.get(Calendar.YEAR);
2303 02 Apr 14 olle 706     // Get date for first day in year
2303 02 Apr 14 olle 707     cal.set(year, 1, 1);
2303 02 Apr 14 olle 708     Date dateFirstDayInYear = cal.getTime();
2303 02 Apr 14 olle 709     return dateFirstDayInYear;
2303 02 Apr 14 olle 710   }
2303 02 Apr 14 olle 711
2303 02 Apr 14 olle 712   /**
2303 02 Apr 14 olle 713    *  Returns a date equal to the input date, but with time of day set to given time.
2303 02 Apr 14 olle 714    *
2303 02 Apr 14 olle 715    *  @param date Date The input Date object to get year, month, and day from.  
2303 02 Apr 14 olle 716    *  @param hour int The hour of day to use. 
2303 02 Apr 14 olle 717    *  @param min int The minute to use. 
2303 02 Apr 14 olle 718    *  @param sec int The second to use.
2303 02 Apr 14 olle 719    *  @return A date equal to the input date, but with time of day set to given time.
2303 02 Apr 14 olle 720    */
2303 02 Apr 14 olle 721   public Date adjustDayTime(Date date, int hour, int min, int sec)
2303 02 Apr 14 olle 722   {
2303 02 Apr 14 olle 723     // Get year, month, and date
2303 02 Apr 14 olle 724     Calendar cal = GregorianCalendar.getInstance();
2303 02 Apr 14 olle 725     cal.setTime(date);
2303 02 Apr 14 olle 726     int year = cal.get(Calendar.YEAR);
2303 02 Apr 14 olle 727     int month = cal.get(Calendar.MONTH);
2303 02 Apr 14 olle 728     int dateInMonth = cal.get(Calendar.DATE);
2303 02 Apr 14 olle 729     // Adjust time of day for date
2303 02 Apr 14 olle 730     cal.clear();
2303 02 Apr 14 olle 731     cal.set(year, month, dateInMonth, hour, min, sec);
2303 02 Apr 14 olle 732     Date adjustedDate = cal.getTime();
2303 02 Apr 14 olle 733     return adjustedDate;
2303 02 Apr 14 olle 734   }
2303 02 Apr 14 olle 735 }