- package org.tiedyedfreaks.ace.briefbot1;
- import java.sql.Date;
- import java.sql.Timestamp;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- /**
- * Java's Calendar class assumes that months have a 0 offset (January = 0).
- * RCalendar converts between normal months (January = 1)
- * and Calendar class months, and presents the date in YYYY-MM-DD format.
- * It also ensures that hours, minutes, seconds, and milliseconds are set to 0.
- * @author S. Medlock
- */
- public class RCalendar extends GregorianCalendar{
- /**
- * Serializable UID as required by all serializable objects
- */
- private static final long serialVersionUID = 1218007959948474671L;
- /**
- * Constructor for a default RCalendar object with today's year, month, and day;
- * with hr:min:sec set to OO:OO:OO.
- */
- public RCalendar(){
- int year = this.get(Calendar.YEAR);
- int month = this.get(Calendar.MONTH);
- int day = this.get(Calendar.DAY_OF_MONTH);
- this.clear();
- this.set(year, month, day);
- }
- public RCalendar(String rCal) {
- try {
- String[] rCalArray = rCal.split("-");
- String yearString = rCalArray[0];
- int year = Integer.parseInt(yearString);
- int month = Integer.parseInt(rCalArray[1]);
- int day = Integer.parseInt(rCalArray[2]);
- if(yearString.length()==4
- &&(month>0&&month<=12)
- &&(day>0&&day<=31)){
- this.clear();
- this.set(year, month-1, day);}
- else{throw new IllegalArgumentException();}
- } catch (NumberFormatException e) {
- throw new IllegalArgumentException(rCal);
- }
- }
- public RCalendar(int year, int month, int day){
- if((year<=9999 && year>=1000)
- &&(month>0 && month<=12)
- &&(day>0 && day<=31)){
- this.clear();
- this.set(year, month-1, day);}
- else{throw new IllegalArgumentException(year+"-"+month+"-"+day);}
- }
- /**
- * Construct an RCalendar object from an (SQL) Date object
- * @param date
- */
- public RCalendar(Date date){
- Calendar cal = Calendar.getInstance();
- cal.clear();
- cal.setTime(date);
- this.clear();
- int year = cal.get(Calendar.YEAR);
- int month = cal.get(Calendar.MONTH);
- int day = cal.get(Calendar.DAY_OF_MONTH);
- this.set(year, month, day);
- }
- /**
- * Construct an RCalendar object from a Calendar object
- * @param cal
- */
- public RCalendar(Calendar cal) {
- this.clear();
- int year = cal.get(Calendar.YEAR);
- int month = cal.get(Calendar.MONTH);
- int day = cal.get(Calendar.DAY_OF_MONTH);
- this.set(year, month, day);
- }
- /**
- * Construct and RCalendar object from an SQL Timestamp object
- * @param Timestamp
- */
- public RCalendar(Timestamp t){
- long milliseconds = t.getTime();
- Calendar cal = Calendar.getInstance();
- cal.clear();
- cal.setTime(new java.util.Date(milliseconds));
- this.clear();
- int year = cal.get(Calendar.YEAR);
- int month = cal.get(Calendar.MONTH);
- int day = cal.get(Calendar.DAY_OF_MONTH);
- this.set(year, month, day);
- }
- /**
- * Construct an RCalendar which is equal to today's date
- * plus some integer number of DATE (days), WEEK_OF_MONTH,
- * MONTH, or YEAR. Use negative numbers to subtract.
- * @param String d, w, m, or y indicates that the amount
- * should be added to days, weeks, months, or years
- * @param amount is the number of d,w,m, or y to be added
- */
- public RCalendar(String dwmy, int amount){
- int year = this.get(Calendar.YEAR);
- int month = this.get(Calendar.MONTH);
- int day = this.get(Calendar.DATE);
- this.clear();
- this.set(year, month, day);
- if(dwmy.equals("d")){this.add(Calendar.DATE, amount);}
- if(dwmy.equals("w")){this.add(Calendar.WEEK_OF_YEAR, amount);}
- if(dwmy.equals("m")){this.add(Calendar.MONTH, amount);}
- if(dwmy.equals("y")){this.add(Calendar.YEAR, amount);}
- }
- /**
- * Overrides toString so that RCalendar objects print in the usual real-world
- * year-month-day form. Does not use leading 0's for single-digit months and days.
- * @return String YYYY-M(M)-D(D)
- */
- public String toString(){
- return "" + this.get(Calendar.YEAR) + "-" + (this.get(Calendar.MONTH)+1) + "-" + this.get(Calendar.DAY_OF_MONTH);
- }
- }