/*
* MyCalendar
*
* Copyright 2007 Mitra
* Jorge Machado
*/
package jomm.utils;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;
public class MyCalendar
extends java.
util.
1.5.0/docs/api/java/util/GregorianCalendar.html">GregorianCalendar implements java.
io.
1.5.0/docs/api/java/io/Serializable.html">Serializable
{
public static final 1.5.0/docs/api/java/lang/String.html">String simpleDateTimeFormat =
"dd-MM-yyyy HH-mm-ss";
public static final 1.5.0/docs/api/java/lang/String.html">String TimestampFormat =
"yyyyMMddHHmmss";
public static final 1.5.0/docs/api/java/lang/String.html">String GmtTimezone =
"GMT";
public MyCalendar
()
{
super();
}
/**
* Constructs a GregorianCalendar based on the current time
* in the given time zone with the default locale.
* @param zone the given time zone.
*/
public MyCalendar
(1.5.0/docs/api/java/util/TimeZone.html">TimeZone zone
)
{
super(zone
);
}
/**
* Constructs a GregorianCalendar based on the current time
* in the default time zone with the given locale.
* @param aLocale the given locale.
*/
public MyCalendar
(1.5.0/docs/api/java/util/Locale.html">Locale aLocale
)
{
super(aLocale
);
}
/**
* Constructs a GregorianCalendar based on the current time
* in the given time zone with the given locale.
* @param zone the given time zone.
* @param aLocale the given locale.
*/
public MyCalendar
(1.5.0/docs/api/java/util/TimeZone.html">TimeZone zone,
1.5.0/docs/api/java/util/Locale.html">Locale aLocale
)
{
super(zone, aLocale
);
}
/**
* Constructs a GregorianCalendar with the given date set
* in the default time zone with the default locale.
* @param year the value used to set the YEAR time field in the calendar.
* @param month the value used to set the MONTH time field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param day the value used to set the DATE time field in the calendar.
*/
public MyCalendar
(int year,
int month,
int day
)
{
super(year, month - 1, day
);
}
/**
* Constructs a GregorianCalendar with the given date
* and time set for the default time zone with the default locale.
* @param year the value used to set the YEAR time field in the calendar.
* @param month the value used to set the MONTH time field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param day the value used to set the DATE time field in the calendar.
* @param hour the value used to set the HOUR_OF_DAY time field
* in the calendar.
* @param minute the value used to set the MINUTE time field
* in the calendar.
*/
public MyCalendar
(int year,
int month,
int day,
int hour,
int minute
)
{
super(year, month - 1, day, hour, minute
);
}
/**
* Constructs a GregorianCalendar with the given date
* and time set for the default time zone with the default locale.
* @param year the value used to set the YEAR time field in the calendar.
* @param month the value used to set the MONTH time field in the calendar.
* Month value is 0-based. e.g., 0 for January.
* @param day the value used to set the DATE time field in the calendar.
* @param hour the value used to set the HOUR_OF_DAY time field
* in the calendar.
* @param minute the value used to set the MINUTE time field
* in the calendar.
* @param second the value used to set the SECOND time field
* in the calendar.
*/
public MyCalendar
(int year,
int month,
int day,
int hour,
int minute,
int second
)
{
super(year, month - 1, day, hour, minute, second
);
}
public int getDay
()
{
return get
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
DAY_OF_MONTH);
}
public int getMonth
()
{
return get
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
MONTH) +
1;
}
public int getYear
()
{
return get
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
YEAR);
}
public int getHour
()
{
return get
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
HOUR_OF_DAY);
}
public int getMinute
()
{
return get
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
MINUTE);
}
public int getSecond
()
{
return get
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
SECOND);
}
public void setDay
(int value
)
{
set
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
DAY_OF_MONTH, value
);
}
public void setMonth
(int value
)
{
set
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
MONTH, value - 1
);
}
public void setYear
(int value
)
{
set
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
YEAR, value
);
}
public void setHour
(int value
)
{
set
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
HOUR_OF_DAY, value
);
}
public void setMinute
(int value
)
{
set
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
MINUTE, value
);
}
public void setSecond
(int value
)
{
set
(1.5.0/docs/api/java/util/Calendar.html">Calendar.
SECOND, value
);
}
public 1.5.0/docs/api/java/lang/String.html">String getDateString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getDateInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM, locale
);
// DateFormat formatter = new SimpleDateFormat("dd/MMM/yyyy", locale); Can't be this way because of i18n
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getDefault());
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getFullDateString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getDateInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
FULL, locale
);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getDefault());
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getDateTimeString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getDateTimeInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM,
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM, locale
);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getDefault());
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getTimestamp
()
{
return MyCalendar.
getTimestamp(getTime
());
}
public static 1.5.0/docs/api/java/lang/String.html">String getTimestamp
(5+0%2Fdocs%2Fapi+Date">Date date
)
{
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat(MyCalendar.
TimestampFormat);
/**
*/
// formatter.setTimeZone(TimeZone.getTimeZone(GmtTimezone));
1.5.0/docs/api/java/lang/String.html">String ts = formatter.
format(date
);
return ts
;
}
public void setTimestamp
(1.5.0/docs/api/java/lang/String.html">String dateString
) throws 1.5.0/docs/api/java/text/ParseException.html">ParseException {
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat(MyCalendar.
TimestampFormat);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getTimeZone(MyCalendar.
GmtTimezone));
5+0%2Fdocs%2Fapi+Date">Date _date = formatter.
parse(dateString
);
setTime
(_date
);
}
public 1.5.0/docs/api/java/lang/String.html">String getSimpleDateTimeString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getDateTimeInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
SHORT,
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM, locale
);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getDefault());
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getSimpleDateTimeString2
()
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getDateTimeInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
SHORT,
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM,
new 1.5.0/docs/api/java/util/Locale.html">Locale("asdasdasd"));
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getDefault());
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getSimpleDateTime
()
{
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat(MyCalendar.
simpleDateTimeFormat);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getDefault());
return formatter.
format(getTime
());
}
public void setSimpleDateTime
(1.5.0/docs/api/java/lang/String.html">String date
) throws 1.5.0/docs/api/java/text/ParseException.html">ParseException
{
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat(MyCalendar.
simpleDateTimeFormat);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getDefault());
5+0%2Fdocs%2Fapi+Date">Date _date = formatter.
parse(date
);
setTime
(_date
);
}
public 1.5.0/docs/api/java/lang/String.html">String getXMLDateTimeString
()
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
new 1.5.0/docs/api/java/text/SimpleDateFormat.html">SimpleDateFormat("dd-MM-yyyy_HH-mm-ss");
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getDefault());
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getFullDateTimeString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getDateTimeInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
FULL,
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM, locale
);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getDefault());
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getTimeString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/* int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getTimeInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM, locale
);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getDefault());
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getGMTDateString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getDateInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM, locale
);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getTimeZone(MyCalendar.
GmtTimezone));
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getGMTFullDateString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getDateInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
FULL, locale
);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getTimeZone(MyCalendar.
GmtTimezone));
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getGMTDateTimeString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getDateTimeInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM,
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM, locale
);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getTimeZone(MyCalendar.
GmtTimezone));
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getGMTFullDateTimeString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/*
int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getDateTimeInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
FULL,
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM, locale
);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getTimeZone(MyCalendar.
GmtTimezone));
return formatter.
format(getTime
());
}
public 1.5.0/docs/api/java/lang/String.html">String getGMTTimeString
(1.5.0/docs/api/java/util/Locale.html">Locale locale
)
{
/* int[] formats =
{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
*/
1.5.0/docs/api/java/text/DateFormat.html">DateFormat formatter =
1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
getTimeInstance(1.5.0/docs/api/java/text/DateFormat.html">DateFormat.
MEDIUM, locale
);
formatter.
setTimeZone(1.5.0/docs/api/java/util/TimeZone.html">TimeZone.
getTimeZone(MyCalendar.
GmtTimezone));
return formatter.
format(getTime
());
}
// public int compareTo(Calendar obj)
// {
// if (obj instanceof MyCalendar || obj instanceof GregorianCalendar)
// {
// if (this.after(obj))
// {
// return 1;
// }
// else if(this.before(obj))
// {
// return -1;
// }
//
// return 0;
// }
//
// throw new ClassCastException("MyCalendar.compareTo: object is not of type GregorianCalendar: " + obj);
// }
public 5+0%2Fdocs%2Fapi+List">List getWeeks
()
{
5+0%2Fdocs%2Fapi+List">List weeks =
new 1.5.0/docs/api/java/util/ArrayList.html">ArrayList();
5+0%2Fdocs%2Fapi+Object">Object week
[] =
new 5+0%2Fdocs%2Fapi+Object">Object[7
];
MyCalendar calendar =
new MyCalendar
();
calendar.
setTime(getTime
());
int firstDayOfWeek = MyCalendar.
MONDAY;
calendar.
setFirstDayOfWeek(firstDayOfWeek
);
calendar.
set(MyCalendar.
DAY_OF_MONTH, 1
);
int month = calendar.
getMonth();
int currentWeek = calendar.
get(MyCalendar.
WEEK_OF_MONTH);
do
{
if (calendar.
getMonth() != month
)
{
weeks.
add(week
);
break;
}
if (calendar.
get(MyCalendar.
WEEK_OF_MONTH) != currentWeek
)
{
weeks.
add(week
);
currentWeek++
;
week =
new 5+0%2Fdocs%2Fapi+Object">Object[7
];
}
int weekDay = calendar.
get(MyCalendar.
DAY_OF_WEEK);
int pos =
(weekDay - firstDayOfWeek
) % (7
);
if (pos
< 0
) pos +=
7;
week
[pos
] =
("" + calendar.
getDay());
calendar.
add(MyCalendar.
DAY_OF_YEAR, 1
);
}
while (true);
return weeks
;
}
public boolean isSameDay
(MyCalendar calendar
)
{
return (getDay
() == calendar.
getDay() &&
getMonth
() == calendar.
getMonth() &&
getYear
() == calendar.
getYear());
}
public boolean isWeekday
()
{
return (!isWeekday
());
}
public boolean isWeekendDay
()
{
int weekday = get
(MyCalendar.
DAY_OF_WEEK);
return (weekday == MyCalendar.
SATURDAY || weekday == MyCalendar.
SUNDAY);
}
/**
* getMinuteDifference
*
* Returns the minute difference between this calendar and time2: hours(calendar - time2)
*/
public int getMinuteDifference
(1.5.0/docs/api/java/util/Calendar.html">Calendar time2
)
{
long millisDifference = getTimeInMillis
() - time2.
getTimeInMillis();
long millisInOneHour = 60
* 1000;
return (int)1.5.0/docs/api/java/lang/Math.html">Math.
round(millisDifference / millisInOneHour
);
}
}