import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* DateCalculator fo JAVA
* @author Angelo Ferrante
*
*/
public class DateCalculator {
public static GregorianCalendar gc = new GregorianCalendar();
static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
/**
*
* @return La data di oggi nel formato Data
*/
public static Date getToday()
{
gc.setTimeInMillis(new Date().getTime());
return gc.getTime();
}
/**
*
* @return La data di oggi nel formato numerico aaaammgg
*/
public static int getToday_int()
{
gc.setTimeInMillis(new Date().getTime());
int today = DateToInt(gc.getTime());
return today;
}
/**
*
* @return La data di oggi nel formato stringa gg/mm/aaaa
*/
public static String getToday_String()
{
gc.setTimeInMillis(new Date().getTime());
String today = DateToString(gc.getTime());
return today;
}
/**
*
* @return La data di ieri nel formato stringa gg/mm/aaaa
*/
public static String getYesterday_String()
{
gc.setTimeInMillis(new Date().getTime() - (24*60*60*1000));
return DateToString(gc.getTime());
}
public static String getLastWorkDay() throws ParseException {
String returnDate = "";
boolean dateFound = false;
int d = 1;
while (!dateFound){
gc.setTimeInMillis(new Date().getTime() - d*(24*60*60*1000));
returnDate = DateToString(gc.getTime());
if (DateCalculator.IsWeekend(returnDate))
d++;
else if (DateCalculator.IsHoliday(returnDate))
d++;
else
dateFound = true;
}
return returnDate;
}
public static int getThisMonth()
{
gc.setTimeInMillis(new Date().getTime());
long date1 = gc.getTime().getTime();
gc.setTimeInMillis(date1);
int year = gc.get(Calendar.YEAR);
int month = 1 + gc.get(Calendar.MONTH);
int giorno = gc.get(Calendar.DAY_OF_MONTH);
return month;
}
public static int getThisYear()
{
gc.setTimeInMillis(new Date().getTime());
long date1 = gc.getTime().getTime();
gc.setTimeInMillis(date1);
int year = gc.get(Calendar.YEAR);
int month = 1 + gc.get(Calendar.MONTH);
int giorno = gc.get(Calendar.DAY_OF_MONTH);
return year;
}
public static boolean IsWeekend(String dtmDate) throws ParseException
{
//****************************************
//Created By: Angelo Ferrante
//Mod Date: 20 /11/ 2007
//Purpose: Determine if the date provided is a weekend
//In: dteDate is the date to be checked
//Out: Returns either True if the date is a Saturday or Sunday and False if any other
// day of the week
//Example: IsWeekend("19-02-03") returns False
//****************************************
long date1 = sdf.parse(dtmDate).getTime();
gc.setTimeInMillis(date1);
int day = gc.get(Calendar.DAY_OF_WEEK);
if ((day == 1)|| (day == 7)) {return true;}
else return false;
}
public static boolean IsSaturday(String dtmDate)
{
//****************************************
//Created By: Angelo Ferrante
//Mod Date: 20 /11/ 2007
//Purpose: Determine if the date provided is a saturday
//In: dteDate is the date to be checked
//Out: Returns either True if the date is a Saturday or Sunday and False if any other
// day of the week
//Example: IsWeekend("19-02-03") returns False
//****************************************
long date1 = 0;
try {
date1 = sdf.parse(dtmDate).getTime();} catch (ParseException e) {e.printStackTrace();}
gc.setTimeInMillis(date1);
int day = gc.get(Calendar.DAY_OF_WEEK);
if (day == 7) {return true;}
else return false;
}
public static boolean IsSunday(String dtmDate)
{
//****************************************
//Created By: Angelo Ferrante
//Mod Date: 20 /11/ 2007
//Purpose: Determine if the date provided is a saturday
//In: dteDate is the date to be checked
//Out: Returns either True if the date is a Saturday or Sunday and False if any other
// day of the week
//Example: IsWeekend("19-02-03") returns False
//****************************************
long date1 = 0;
try {
date1 = sdf.parse(dtmDate).getTime();} catch (ParseException e) {e.printStackTrace();}
gc.setTimeInMillis(date1);
int day = gc.get(Calendar.DAY_OF_WEEK);
if (day == 1) {return true;}
else return false;
}
public static boolean IsHoliday(String data) throws ParseException
{
long date1 = sdf.parse(data).getTime();
gc.setTimeInMillis(date1);
int anno = gc.get(Calendar.YEAR);
int mese = 1 + gc.get(Calendar.MONTH);
int giorno = gc.get(Calendar.DAY_OF_MONTH);
if ((mese == 1 && giorno == 1) ||
(mese == 1 && giorno == 6) ||
(mese == 4 && giorno == 25) ||
(mese == 5 && giorno == 1) ||
(mese == 6 && giorno == 2) ||
(mese == 8 && giorno == 15) ||
(mese == 11 && giorno == 1) ||
(mese == 12 && giorno == 8) ||
(mese == 12 && giorno == 25)||
(mese == 12 && giorno == 26) ||
data.equalsIgnoreCase(EasterDate(anno)) ||
data.equalsIgnoreCase(EasterMondayDate(anno)))
{return true;}
else {return false;}
}
// Returns the Easter date in the specified year
public static String EasterDate( int anno)
{
int G, C, H, i, j, L;
int m, d;
String m_s;
if (anno == 0) {anno = gc.get(Calendar.YEAR);}
if (anno != 0)
{
G = anno % 19;
C = anno / 100;
H = ((C - (C / 4) - ((8 * C + 13) / 25) + (19 * G) + 15) % 30);
i = H - ((H / 28) * (1 - (H / 28) * (29 / (H + 1)) * ((21 - G) / 11)));
j = ((anno + (anno / 4) + i + 2 - C + (C / 4)) % 7);
L = i - j;
m = 3 + ((L + 40) / 44);
m_s = m +"";
if (m<10) m_s = "0"+m;
d = L + 28 - (31 * (m / 4));
String dt1 = d + "/" + m_s + "/" + anno;
return dt1;
}
else {
System.err.println("Easter not found");
return null;
}
}
public static String EasterMondayDate( int anno)
{
int G, C, H, i, j, L;
int m, d;
String m_s,d_s;
if (anno == 0) {anno = gc.get(Calendar.YEAR);}
if (anno != 0)
{
G = anno % 19;
C = anno / 100;
H = ((C - (C / 4) - ((8 * C + 13) / 25) + (19 * G) + 15) % 30);
i = H - ((H / 28) * (1 - (H / 28) * (29 / (H + 1)) * ((21 - G) / 11)));
j = ((anno + (anno / 4) + i + 2 - C + (C / 4)) % 7);
L = i - j;
m = 3 + ((L + 40) / 44);
m_s = m +"";
if (m<10) m_s = "0"+m;
d = L + 28 - (31 * (m / 4));
d = d+1;
d_s = d + "";
if (d<10) d_s = "0"+d;
String dt1 = d_s + "/" + m_s + "/" + anno;
return dt1;
}
else {
System.err.println("Easter not found");
return null;
}
}
/****************************************
* Created By: Angelo Ferrante
* Mod Date: 20/11/2007
* Purpose: Determine the number of business days between two dates
* In: dteStartDate is the first date, dtmEndDate is the last date
* Out: Returns the number of business between start and end date
* Example: CountWorkDays(12/31/02, 1/3/03) returns 3
(1/1/01 is a holiday (New Year's Day))
****************************************
* @param dtmStartDate
* @return
* @throws ParseException
*/
public static int CountWorkDays(String dtmStartDate, String dtmEndDate)
{
int intWorkDayCount = 0;
long intDaysBetweenDates = 0;
int i;
Date dt_dtmStartDate=null;
Date dt_dtmEndDate = null;
try
{
if (StringToDate(dtmStartDate).getTime() > StringToDate(dtmEndDate).getTime())
{
String dtmTemp = dtmStartDate;
dtmStartDate = dtmEndDate;
dtmEndDate = dtmTemp;
}
dt_dtmStartDate = StringToDate(dtmStartDate);
dt_dtmEndDate = StringToDate(dtmEndDate);
intWorkDayCount = 0;
intDaysBetweenDates = (dt_dtmEndDate.getTime() - dt_dtmStartDate.getTime())/ (1000L*60L*60L*24L);
Calendar yearDate = Calendar.getInstance();
yearDate.set(dt_dtmStartDate.getYear()+1900,dt_dtmStartDate.getMonth(),dt_dtmStartDate.getDate());
//System.out.println(intDaysBetweenDates);
for (i = 0; i< intDaysBetweenDates;i++)
{
String wrkStartDate = DateToString(yearDate.getTime());
//System.out.println(wrkStartDate);
if ((!IsHoliday(wrkStartDate)) && (!IsWeekend(wrkStartDate)))
{intWorkDayCount++;}
yearDate.roll(Calendar.DAY_OF_YEAR, true);
}
}
catch (ParseException e)
{
System.err.println("\nIl dato della data non è nel formato corretto\n");
e.printStackTrace();
System.out.println("Il dato della data di una pratica non è nel formato corretto");
intWorkDayCount = 1;
}
return intWorkDayCount + 1;
}
static long TimeDifference_ms(String str_TimeIn, String str_TimeOut)
{
Date dt_dateIn = new Date("01/01/1970 " + str_TimeIn + ":00");;
Date dt_dateOut = new Date("01/01/1970 " + str_TimeOut + ":00");
long msec = dt_dateOut.getTime() - dt_dateIn.getTime();
return msec;
}
public static String DateToString(Date dt_Date)
{
String mese_s, giorno_s;
long date1 = dt_Date.getTime();
gc.setTimeInMillis(date1);
int anno = gc.get(Calendar.YEAR);
int mese = 1 + gc.get(Calendar.MONTH);
int giorno = gc.get(Calendar.DAY_OF_MONTH);
if (mese < 10) mese_s = "0" + mese;
else mese_s = "" + mese;
if (giorno < 10) giorno_s = "0" + giorno;
else giorno_s = "" + giorno;
String str_Date = giorno_s + "/" + mese_s + "/" + anno;
return str_Date;
}
static Date StringToDate(String str_Date) throws ParseException
{
Date dt_date;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
dt_date = sdf.parse(str_Date);
return dt_date;
}
public static int DateToInt(Date dt_Date) {
String mese_s;
String giorno_s;
long date1 = dt_Date.getTime();
gc.setTimeInMillis(date1);
int anno = gc.get(Calendar.YEAR);
int mese = 1 + gc.get(Calendar.MONTH);
int giorno = gc.get(Calendar.DAY_OF_MONTH);
if (mese < 10) mese_s = "0" + mese;
else mese_s = "" + mese;
if (giorno < 10) giorno_s = "0" + mese;
else giorno_s = "" + giorno;
String str_Date = anno + mese_s + giorno_s;
int int_Date = Integer.parseInt(str_Date);
return int_Date;
}
static double approx(double number, int decimalApprox){
int a = (int)Math.pow(10,decimalApprox);
double numberInt = Math.round(number*a);
return numberInt/a;
}
public static String IntegerDateToStringDate(int int_Date)
{
String str_Date_tmp = int_Date + "";
return str_Date_tmp.substring(6,8) + "/" + str_Date_tmp.substring(4,6) + "/" + str_Date_tmp.substring(0,4);
}
}