package com.kiasoft.util; /* ------------------------------------------------------------------ * Kiasoft, Inc. * http://kiasoft.com * * Copyright (c) 2004-2006 Kiasoft, Inc. * * ------------------------------------------------------------------ */ /* * Date manipulation and testing */ import java.util.*; import java.text.*; // import org.apache.turbine.util.Log; public class DateManip { private Calendar cal; // 1 hour, 1 day in milliseconds private static long HOUR_MS = 3600000; private static long MINUTE_MS = ( HOUR_MS / 60 ); private static long DAY_MS = ( 24 * HOUR_MS ); private static long WEEK_MS = ( 7 * DAY_MS ); public DateManip() { this.cal = Calendar.getInstance(); return; } public void setDate( Date n ) { this.cal.setTime( n ); } public Date getDate() { return this.cal.getTime(); } public Calendar getCalendar() { return this.cal; } public void addMinutes( int x ) { this.cal.add(Calendar.MINUTE, x); return; } public void addHours( int x ) { this.cal.add(Calendar.HOUR, x); return; } /* * set to an exact hour */ public void setHour( int x ) { this.cal.set(Calendar.HOUR_OF_DAY, x ); this.cal.set(Calendar.MINUTE, 0 ); this.cal.set(Calendar.SECOND, 0 ); } public void addDays( int x ) { this.cal.add(Calendar.DAY_OF_YEAR, x); return; } /* * what is the day of the week */ public int getDayOfWeek() { return this.cal.get(Calendar.DAY_OF_WEEK); } /* * routine to help get a date object which will match * our pt notice */ public void setDateThisWeek( int dayOfWeek, int hour ) { // reset to now Date now = new Date(); this.setDate( now ); // we know the day of week and the time, so // compose a new date object this week // that matches this data int today = this.getDayOfWeek(); // figure out how many days to add to set the // correct day in the calendar if ( dayOfWeek < today ) { // we need to get the date for next week on this day this.addDays( ( (dayOfWeek - today) + 7 ) ); } else { // just figure out what day it is this week this.addDays( (dayOfWeek - today) ); } this.setHour( hour ); return; } public void addWeeks( int x ) { this.cal.add(Calendar.DAY_OF_YEAR, ( x * 7 )); return; } public boolean withinHour( Date start, Date end ) { long diff = start.getTime() - end.getTime(); if ( diff < 0 ) { diff = diff * (-1); } if ( diff <= HOUR_MS ) { return true; } else { return false; } } public boolean withinDay( Date start, Date end ) { long diff = start.getTime() - end.getTime(); if ( diff < 0 ) { diff = diff * (-1); } if ( diff <= DAY_MS ) { return true; } else { return false; } } public boolean withinWeek( Date start, Date end ) { long diff = start.getTime() - end.getTime(); if ( diff < 0 ) { diff = diff * (-1); } if ( diff <= WEEK_MS ) { return true; } else { return false; } } // rounds to nearest whole number for week public int secondsToWeeks( long seconds ) { long weeks = seconds / WEEK_MS; return Math.round( weeks ); } /** * parseCalObject() returns a date from * our javascript calendar control input * expecting MM/DD/YYYY */ public static Date parseCalObject( String cal ) { // trime whitespace from input string cal.trim(); try { DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT ); if ( cal != null && !cal.equals("") && !cal.equals(" ") ) { return df.parse( cal ); } else { return null; } } catch ( Exception e ) { // Log.error("DateManip parseCalObject error: " + e.toString() ); return null; } } }