Here is our date utility class. Hope it helps.
import java.util.GregorianCalendar;
import com.webobjects.foundation.NSMutableArray;
import com.webobjects.foundation.NSTimeZone;
import com.webobjects.foundation.NSTimestamp;
public class DateUtilities extends Object {
public static String calcHM(int timeInSeconds) {
int hours, minutes;
hours = timeInSeconds / 3600;
timeInSeconds = timeInSeconds - (hours * 3600);
minutes = timeInSeconds / 60;
return hours + "h " + minutes + "m";
}
public static String calcHMS(int timeInSeconds) {
int hours, minutes, seconds;
hours = timeInSeconds / 3600;
timeInSeconds = timeInSeconds - (hours * 3600);
minutes = timeInSeconds / 60;
timeInSeconds = timeInSeconds - (minutes * 60);
seconds = timeInSeconds;
return hours + "h " + minutes + "m " + seconds + "s";
}
public static NSTimestamp createTimestamp(NSTimestamp date, NSTimestamp time) {
int year = year(date);
int month = month(date);
int day = dayOfMonth(date);
int hours = hourOfDay(time);
int minutes = minute(time);
int seconds = second(time);
NSTimeZone tz = NSTimeZone.systemTimeZone();
return new NSTimestamp(year, month, day, hours, minutes, seconds, tz);
}
public static NSMutableArray dateArray(NSTimestamp from, NSTimestamp to) {
NSMutableArray dateList = new NSMutableArray();
NSTimestamp d = normalisedDate(from);
while (d.compare(normalisedDate(to)) <= 0) {
dateList.insertObjectAtIndex(d, dateList.count());
d = d.timestampByAddingGregorianUnits(0, 0, 1, 0, 0, 0);
}
return dateList;
}
public static NSMutableArray dateArrayFromDateWithLength(NSTimestamp date, int length) {
NSMutableArray dateList = new NSMutableArray();
NSTimestamp d = normalisedDate(date);
while (dateList.count() < length) {
dateList.insertObjectAtIndex(d, dateList.count());
d = d.timestampByAddingGregorianUnits(0, 0, 1, 0, 0, 0);
}
return dateList;
}
public static int dayOfMonth(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
return gc.get(GregorianCalendar.DAY_OF_MONTH);
}
public static int dayOfWeek(NSTimestamp aDate) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(aDate);
return gc.get(GregorianCalendar.DAY_OF_WEEK);
}
public static String dayOfWeek(NSTimestamp aDate, boolean longName) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(aDate);
int d = gc.get(GregorianCalendar.DAY_OF_WEEK);
if (d == 1) {
return longName ? "Sunday" : "Sun";
}
if (d == 2) {
return longName ? "Monday" : "Mon";
}
if (d == 3) {
return longName ? "Tuesday" : "Tue";
}
if (d == 4) {
return longName ? "Wednesday" : "Wed";
}
if (d == 5) {
return longName ? "Thursday" : "Thur";
}
if (d == 6) {
return longName ? "Friday" : "Fri";
}
if (d == 7) {
return longName ? "Saturday" : "Sat";
}
return null;
}
public static String dayOfWeekFromInteger(String aDay, boolean longName) {
if (aDay == "1") {
return longName ? "Sunday" : "Sun";
}
if (aDay == "2") {
return longName ? "Monday" : "Mon";
}
if (aDay == "3") {
return longName ? "Tuesday" : "Tue";
}
if (aDay == "4") {
return longName ? "Wednesday" : "Wed";
}
if (aDay == "5") {
return longName ? "Thursday" : "Thur";
}
if (aDay == "6") {
return longName ? "Friday" : "Fri";
}
if (aDay == "7") {
return longName ? "Saturday" : "Sat";
}
return null;
}
public static int dayOfYear(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
return gc.get(GregorianCalendar.DAY_OF_YEAR);
}
public static int daysBetweenDates(NSTimestamp t1, NSTimestamp t2) {
if (t1 == null || t2 == null) {
return 0;
}
GregorianCalendar gc1 = new GregorianCalendar();
gc1.setTime(DateUtilities.normalisedDateGMT(t1));
GregorianCalendar gc2 = new GregorianCalendar();
gc2.setTime(DateUtilities.normalisedDateGMT(t2));
int diff = (int) ((gc1.getTime().getTime() - gc2.getTime().getTime()) / (1000 * 60 * 60 * 24));
return diff >= 0 ? diff : -1 * diff;
}
public static int daysInMonth(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
return gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
}
public static NSTimestamp endDate(NSTimestamp aDate) {
return (DateUtilities.startDate(aDate)).timestampByAddingGregorianUnits(0, 1, -1, 0, 0, 0);
}
public static NSTimestamp gmtDate(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
int d = gc.get(GregorianCalendar.DAY_OF_MONTH);
int m = gc.get(GregorianCalendar.MONTH) + 1;
int y = gc.get(GregorianCalendar.YEAR);
int h = gc.get(GregorianCalendar.HOUR_OF_DAY);
int n = gc.get(GregorianCalendar.MINUTE);
int s = gc.get(GregorianCalendar.SECOND);
NSTimeZone tz = NSTimeZone.timeZoneWithName("Etc/GMT", true);
return new NSTimestamp(y, m, d, h, n, s, tz);
}
public static int hourOfDay(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
return gc.get(GregorianCalendar.HOUR_OF_DAY);
}
public static int intOfWeek(NSTimestamp aDate) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(aDate);
return gc.get(GregorianCalendar.DAY_OF_WEEK);
}
public static int minute(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
return gc.get(GregorianCalendar.MINUTE);
}
public static int month(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
return gc.get(GregorianCalendar.MONTH) + 1;
}
public static NSTimestamp normalisedDate(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
int d = gc.get(GregorianCalendar.DAY_OF_MONTH);
int m = gc.get(GregorianCalendar.MONTH) + 1;
int y = gc.get(GregorianCalendar.YEAR);
NSTimeZone tz = NSTimeZone.systemTimeZone();
return new NSTimestamp(y, m, d, 12, 0, 0, tz);
}
public static NSTimestamp normalisedDateGMT(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
int d = gc.get(GregorianCalendar.DAY_OF_MONTH);
int m = gc.get(GregorianCalendar.MONTH) + 1;
int y = gc.get(GregorianCalendar.YEAR);
NSTimeZone tz = NSTimeZone.timeZoneWithName("Etc/GMT", true);
return new NSTimestamp(y, m, d, 12, 0, 0, tz);
}
public static NSTimestamp normalisedTime(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
int h = gc.get(GregorianCalendar.HOUR_OF_DAY);
int m = gc.get(GregorianCalendar.MINUTE);
NSTimeZone tz = NSTimeZone.systemTimeZone();
return new NSTimestamp(2000, 1, 1, h, m, 0, tz);
}
public static int second(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
return gc.get(GregorianCalendar.SECOND);
}
public static int secondsBetweenDates(NSTimestamp t1, NSTimestamp t2) {
GregorianCalendar gc1 = new GregorianCalendar();
gc1.setTime(t1);
GregorianCalendar gc2 = new GregorianCalendar();
gc2.setTime(t2);
int diff = (int) ((gc1.getTime().getTime() - gc2.getTime().getTime()) / 1000);
return diff >= 0 ? diff : -1 * diff;
}
public static NSTimestamp startDate(NSTimestamp aDate) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(aDate);
int y = gc.get(GregorianCalendar.YEAR);
int m = gc.get(GregorianCalendar.MONTH) + 1;
NSTimeZone tz = NSTimeZone.timeZoneWithName("Etc/GMT", true);
return new NSTimestamp(y, m, 1, 0, 0, 0, tz);
}
public static int workingDaysBetweenDates(NSTimestamp start, NSTimestamp end) {
NSTimestamp day = DateUtilities.normalisedDate(start);
int workingDays = 0;
for (int i = 0; i < DateUtilities.daysBetweenDates(DateUtilities.normalisedDate(start), DateUtilities
.normalisedDate(end)); i++) {
if (DateUtilities.dayOfWeek(day, false).equals("Mon") || DateUtilities.dayOfWeek(day, false).equals("Tue")
|| DateUtilities.dayOfWeek(day, false).equals("Wed")
|| DateUtilities.dayOfWeek(day, false).equals("Thur")
|| DateUtilities.dayOfWeek(day, false).equals("Fri")) {
workingDays++;
}
day = day.timestampByAddingGregorianUnits(0, 0, 1, 0, 0, 0);
}
return workingDays;
}
public static int year(NSTimestamp date) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
return gc.get(GregorianCalendar.YEAR);
}