Wednesday, May 30, 2012

Date Utility - Java Application




import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

public class EnvDate
{

public EnvDate()
{
calendar = null;
setCalendar(new GregorianCalendar());
}

public EnvDate(Calendar calendar)
{
this.calendar = null;
setCalendar(calendar);
}

public EnvDate(Locale locale)
{
calendar = null;
setCalendar(new GregorianCalendar(locale));
}

public EnvDate(TimeZone timeZone)
{
calendar = null;
setCalendar(new GregorianCalendar(timeZone));
}

public EnvDate(TimeZone timeZone, Locale locale)
{
calendar = null;
setCalendar(new GregorianCalendar(timeZone, locale));
}

public Date addDay(Date inputDate, int amount)
{
getCalendar().setTime(inputDate);
getCalendar().add(5, amount);
return getCalendar().getTime();
}

public Date addHour(Date inputDate, int amount)
{
getCalendar().setTime(inputDate);
getCalendar().add(10, amount);
return getCalendar().getTime();
}

public Date addMillisecond(Date inputDate, int amount)
{
getCalendar().setTime(inputDate);
getCalendar().add(14, amount);
return getCalendar().getTime();
}

public Date addMinute(Date inputDate, int amount)
{
getCalendar().setTime(inputDate);
getCalendar().add(12, amount);
return getCalendar().getTime();
}

public Date addMonth(Date inputDate, int amount)
{
getCalendar().setTime(inputDate);
getCalendar().add(2, amount);
return getCalendar().getTime();
}

public Date addSecond(Date inputDate, int amount)
{
getCalendar().setTime(inputDate);
getCalendar().add(13, amount);
return getCalendar().getTime();
}

public Date addYear(Date inputDate, int amount)
{
getCalendar().setTime(inputDate);
getCalendar().add(1, amount);
return getCalendar().getTime();
}

public String convertDateToString(Date inputDate, String dateFormat)
{
SimpleDateFormat realDateFormat = new SimpleDateFormat(dateFormat);
String outputDate = null;
if(inputDate != null)
outputDate = realDateFormat.format(inputDate);
return outputDate;
}

public Date convertStringToDate(String inputDate, String dateFormat)
{
SimpleDateFormat realDateFormat = new SimpleDateFormat(dateFormat);
Date outputDate = null;
String newString = null;
if(inputDate != null)
{
outputDate = realDateFormat.parse(inputDate, new ParsePosition(0));
newString = convertDateToString(outputDate, dateFormat);
if(!newString.equals(inputDate))
{
IllegalArgumentException exception = new IllegalArgumentException("Data in string is not a valid date.");
throw exception;
}
}
return outputDate;
}

public Date createDate(int inputYear, int inputMonth, int inputDay)
{
getCalendar().clear();
getCalendar().set(inputYear, inputMonth, inputDay);
return getCalendar().getTime();
}

public Date createDate(int inputYear, int inputMonth, int inputDay, int inputHour, int inputMinute, int inputSecond)
{
getCalendar().clear();
getCalendar().set(inputYear, inputMonth, inputDay, inputHour, inputMinute, inputSecond);
return getCalendar().getTime();
}

public Date createDate(int inputYear, int inputMonth, int inputDay, int inputHour, int inputMinute, int inputSecond, int inputMillisecond)
{
getCalendar().clear();
getCalendar().set(inputYear, inputMonth, inputDay, inputHour, inputMinute, inputSecond);
getCalendar().set(14, inputMillisecond);
return getCalendar().getTime();
}

private Calendar getCalendar()
{
return calendar;
}

public int getCalendarDaysAfter(Date date1, Date date2)
{
Date newDate1 = createDate(getYear(date1), getMonth(date1), getDayOfMonth(date1));
Date newDate2 = createDate(getYear(date2), getMonth(date2), getDayOfMonth(date2));
int daysAfter = (new Double(getDaysAfter(newDate1, newDate2))).intValue();
return daysAfter;
}

public int getCalendarMonthsAfter(Date date1, Date date2)
{
int month1 = getMonth(date1);
int month2 = getMonth(date2);
int year1 = getYear(date1);
int year2 = getYear(date2);
int difference = 0;
if(year1 == year2)
difference = month2 - month1;
else
if(year1 < year2)
{
int yearCtr = year2 - 1;
difference = month2;
for(; yearCtr > year1; yearCtr--)
difference += 12;

difference = (difference + 12) - month1;
} else
if(year1 > year2)
{
int yearCtr = year1 - 1;
difference = month1;
for(; yearCtr > year2; yearCtr--)
difference += 12;

difference = (difference + 12) - month2;
difference *= -1;
}
return difference;
}

public int getCalendarYearsAfter(Date date1, Date date2)
{
int year1 = getYear(date1);
int year2 = getYear(date2);
int difference = year2 - year1;
return difference;
}

public Date getCurrentDate()
{
GregorianCalendar _calendar = new GregorianCalendar();
return _calendar.getTime();
}

public String getDayAbrv(Date inputDate)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE");
String dayAbrv = dateFormat.format(inputDate);
return dayAbrv;
}

public String getDayName(Date inputDate)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE");
String dayName = dateFormat.format(inputDate);
return dayName;
}

public int getDayOfMonth(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(5);
}

public int getDayOfWeek(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(7);
}

public int getDayOfYear(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(6);
}

public double getDaysAfter(Date date1, Date date2)
{
double amount1 = date1.getTime();
double amount2 = date2.getTime();
double difference = (amount2 - amount1) / 86400000D;
return difference;
}

public int getFirstDayOfWeek(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().getFirstDayOfWeek();
}

public int getHour(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(10);
}

public int getHourOfDay(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(11);
}

public double getHoursAfter(Date date1, Date date2)
{
double amount1 = date1.getTime();
double amount2 = date2.getTime();
double difference = (amount2 - amount1) / 3600000D;
return difference;
}

public Date getLastDateOfMonth(Date inputDate)
{
Date nextMonthDate = addMonth(inputDate, 1);
getCalendar().setTime(nextMonthDate);
getCalendar().set(5, 1);
Date lastDayOfMonth = addDay(getCalendar().getTime(), -1);
return lastDayOfMonth;
}

public int getMillisecond(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(14);
}

public long getMillisecondsAfter(Date date1, Date date2)
{
long amount1 = date1.getTime();
long amount2 = date2.getTime();
long difference = amount2 - amount1;
return difference;
}

public int getMinute(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(12);
}

public double getMinutesAfter(Date date1, Date date2)
{
double amount1 = date1.getTime();
double amount2 = date2.getTime();
double difference = (amount2 - amount1) / 60000D;
return difference;
}

public int getMonth(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(2);
}

public String getMonthAbrv(Date inputDate)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM");
String monthAbrv = dateFormat.format(inputDate);
return monthAbrv;
}

public String getMonthName(Date inputDate)
{
SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM");
String monthName = dateFormat.format(inputDate);
return monthName;
}

public int getSecond(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(13);
}

public double getSecondsAfter(Date date1, Date date2)
{
double amount1 = date1.getTime();
double amount2 = date2.getTime();
double difference = (amount2 - amount1) / 1000D;
return difference;
}

public int getTimeZoneOffset(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(15);
}

public int getWeekOfMonth(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(4);
}

public int getWeekOfYear(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(3);
}

public int getYear(Date inputDate)
{
getCalendar().setTime(inputDate);
return getCalendar().get(1);
}

public boolean isAfter(Date date1, Date date2)
{
boolean rtnValue = false;
if(date1.getTime() > date2.getTime())
rtnValue = true;
return rtnValue;
}

public boolean isAM(Date inputDate)
{
getCalendar().setTime(inputDate);
boolean rtnValue;
switch(getCalendar().get(9))
{
case 0: // '\0'
rtnValue = true;
break;

default:
rtnValue = false;
break;
}
return rtnValue;
}

public boolean isBefore(Date date1, Date date2)
{
boolean rtnValue = false;
if(date1.getTime() < date2.getTime())
rtnValue = true;
return rtnValue;
}

public boolean isEqualTo(Date date1, Date date2)
{
boolean rtnValue = false;
if(date1.getTime() == date2.getTime())
rtnValue = true;
return rtnValue;
}

public boolean isLeapYear(Date inputDate)
{
boolean rtnValue = false;
getCalendar().setTime(inputDate);
rtnValue = ((GregorianCalendar)getCalendar()).isLeapYear(getYear(inputDate));
return rtnValue;
}

public boolean isPM(Date inputDate)
{
getCalendar().setTime(inputDate);
boolean rtnValue;
switch(getCalendar().get(9))
{
case 1: // '\001'
rtnValue = true;
break;

default:
rtnValue = false;
break;
}
return rtnValue;
}

private void setCalendar(Calendar calendar)
{
this.calendar = calendar;
}

public Date setDayOfMonth(Date inputDate, int dayOfMonth)
{
getCalendar().setTime(inputDate);
getCalendar().set(5, dayOfMonth);
return getCalendar().getTime();
}

public Date setDayOfWeek(Date inputDate, int dayOfWeek)
{
getCalendar().setTime(inputDate);
getCalendar().set(7, dayOfWeek);
return getCalendar().getTime();
}

public Date setDayOfYear(Date inputDate, int dayOfYear)
{
getCalendar().setTime(inputDate);
getCalendar().set(6, dayOfYear);
return getCalendar().getTime();
}

public Date setHour(Date inputDate, int hour)
{
getCalendar().setTime(inputDate);
getCalendar().set(10, hour);
return getCalendar().getTime();
}

public Date setHourOfDay(Date inputDate, int hourOfDay)
{
getCalendar().setTime(inputDate);
getCalendar().set(11, hourOfDay);
return getCalendar().getTime();
}

public Date setMillisecond(Date inputDate, int millisecond)
{
getCalendar().setTime(inputDate);
getCalendar().set(14, millisecond);
return getCalendar().getTime();
}

public Date setMinute(Date inputDate, int minute)
{
getCalendar().setTime(inputDate);
getCalendar().set(12, minute);
return getCalendar().getTime();
}

public Date setMonth(Date inputDate, int month)
{
getCalendar().setTime(inputDate);
getCalendar().set(2, month);
return getCalendar().getTime();
}

public Date setSecond(Date inputDate, int second)
{
getCalendar().setTime(inputDate);
getCalendar().set(13, second);
return getCalendar().getTime();
}

public Date setWeekOfMonth(Date inputDate, int weekOfMonth)
{
getCalendar().setTime(inputDate);
getCalendar().set(4, weekOfMonth);
return getCalendar().getTime();
}

public Date setWeekOfYear(Date inputDate, int weekOfYear)
{
getCalendar().setTime(inputDate);
getCalendar().set(3, weekOfYear);
return getCalendar().getTime();
}

public Date setYear(Date inputDate, int year)
{
getCalendar().setTime(inputDate);
getCalendar().set(1, year);
return getCalendar().getTime();
}
public Date getFirstDayOfYear(Date dateVal){
Calendar calendar=Calendar.getInstance();
calendar.setTime(dateVal);
calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMinimum((Calendar.DAY_OF_YEAR)));
calendar = setZeroHours(calendar);
return calendar.getTime();
}
public Date getFirstDayOfMonth(Date dateVal){
Calendar calendar=Calendar.getInstance();
calendar.setTime(dateVal);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum((Calendar.DAY_OF_MONTH)));
calendar = setZeroHours(calendar);
return calendar.getTime();
}
public Date getFirstDayOfPrevMonth(Date dateVal){
Calendar calendar=Calendar.getInstance();
calendar.setTime(dateVal);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum((Calendar.DAY_OF_MONTH)));
calendar = setZeroHours(calendar);
calendar.add(Calendar.MONTH, -1);
return calendar.getTime();
}
private Calendar setZeroHours(Calendar calendar){
calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
return calendar;
}
private Calendar calendar;
}

No comments: