Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

RCalendar

By: ace on Jan 24th, 2010  |  syntax: None  |  size: 4.08 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package org.tiedyedfreaks.ace.briefbot1;
  2.  
  3. import java.sql.Date;
  4. import java.sql.Timestamp;
  5. import java.util.Calendar;
  6. import java.util.GregorianCalendar;
  7.  
  8. /**
  9.  * Java's Calendar class assumes that months have a 0 offset (January = 0).
  10.  * RCalendar converts between normal months (January = 1)
  11.  * and Calendar class months, and presents the date in YYYY-MM-DD format.  
  12.  * It also ensures that hours, minutes, seconds, and milliseconds are set to 0.
  13.  * @author S. Medlock
  14.  */
  15.  
  16. public class RCalendar extends GregorianCalendar{
  17.  
  18.         /**
  19.          * Serializable UID as required by all serializable objects
  20.          */
  21.         private static final long serialVersionUID = 1218007959948474671L;
  22.  
  23.         /**
  24.          * Constructor for a default RCalendar object with today's year, month, and day;
  25.          * with hr:min:sec set to OO:OO:OO.
  26.          */
  27.         public RCalendar(){
  28.                 int year = this.get(Calendar.YEAR);
  29.                 int month = this.get(Calendar.MONTH);
  30.                 int day = this.get(Calendar.DAY_OF_MONTH);
  31.                 this.clear();
  32.                 this.set(year, month, day);
  33.         }
  34.        
  35.         public RCalendar(String rCal) {
  36.                 try {
  37.                         String[] rCalArray = rCal.split("-");
  38.                         String yearString = rCalArray[0];
  39.                         int year = Integer.parseInt(yearString);
  40.                         int month = Integer.parseInt(rCalArray[1]);
  41.                         int day = Integer.parseInt(rCalArray[2]);
  42.                         if(yearString.length()==4
  43.                                         &&(month>0&&month<=12)
  44.                                         &&(day>0&&day<=31)){           
  45.                                 this.clear();
  46.                                 this.set(year, month-1, day);}
  47.                         else{throw new IllegalArgumentException();}
  48.                 } catch (NumberFormatException e) {
  49.                         throw new IllegalArgumentException(rCal);
  50.                 }
  51.                
  52.         }
  53.        
  54.         public RCalendar(int year, int month, int day){
  55.                 if((year<=9999 && year>=1000)
  56.                                 &&(month>0 && month<=12)
  57.                                 &&(day>0 && day<=31)){         
  58.                         this.clear();
  59.                         this.set(year, month-1, day);}
  60.                 else{throw new IllegalArgumentException(year+"-"+month+"-"+day);}
  61.         }
  62.        
  63.         /**
  64.          * Construct an RCalendar object from an (SQL) Date object
  65.          * @param date
  66.          */
  67.         public RCalendar(Date date){
  68.                 Calendar cal = Calendar.getInstance();
  69.                 cal.clear();
  70.                 cal.setTime(date);
  71.                 this.clear();
  72.                 int year = cal.get(Calendar.YEAR);
  73.                 int month = cal.get(Calendar.MONTH);
  74.                 int day = cal.get(Calendar.DAY_OF_MONTH);
  75.                 this.set(year, month, day);
  76.         }
  77.        
  78.         /**
  79.          * Construct an RCalendar object from a Calendar object
  80.          * @param cal
  81.          */
  82.         public RCalendar(Calendar cal) {
  83.                 this.clear();
  84.                 int year = cal.get(Calendar.YEAR);
  85.                 int month = cal.get(Calendar.MONTH);
  86.                 int day = cal.get(Calendar.DAY_OF_MONTH);
  87.                 this.set(year, month, day);
  88.         }
  89.        
  90.         /**
  91.          * Construct and RCalendar object from an SQL Timestamp object
  92.          * @param Timestamp
  93.          */
  94.         public RCalendar(Timestamp t){
  95.                 long milliseconds = t.getTime();
  96.                 Calendar cal = Calendar.getInstance();
  97.                 cal.clear();
  98.                 cal.setTime(new java.util.Date(milliseconds));
  99.                 this.clear();
  100.                 int year = cal.get(Calendar.YEAR);
  101.                 int month = cal.get(Calendar.MONTH);
  102.                 int day = cal.get(Calendar.DAY_OF_MONTH);
  103.                 this.set(year, month, day);
  104.         }
  105.        
  106.         /**
  107.          * Construct an RCalendar which is equal to today's date
  108.          * plus some integer number of DATE (days), WEEK_OF_MONTH,
  109.          * MONTH, or YEAR.  Use negative numbers to subtract.
  110.          * @param String d, w, m, or y indicates that the amount
  111.          * should be added to days, weeks, months, or years
  112.          * @param amount is the number of d,w,m, or y to be added
  113.          */
  114.         public RCalendar(String dwmy, int amount){
  115.                 int year = this.get(Calendar.YEAR);
  116.                 int month = this.get(Calendar.MONTH);
  117.                 int day = this.get(Calendar.DATE);
  118.                 this.clear();
  119.                 this.set(year, month, day);
  120.                 if(dwmy.equals("d")){this.add(Calendar.DATE, amount);}
  121.                 if(dwmy.equals("w")){this.add(Calendar.WEEK_OF_YEAR, amount);}
  122.                 if(dwmy.equals("m")){this.add(Calendar.MONTH, amount);}
  123.                 if(dwmy.equals("y")){this.add(Calendar.YEAR, amount);}
  124.         }
  125.        
  126.        
  127.         /**
  128.          * Overrides toString so that RCalendar objects print in the usual real-world
  129.          * year-month-day form.  Does not use leading 0's for single-digit months and days.
  130.          * @return String YYYY-M(M)-D(D)
  131.          */
  132.         public String toString(){
  133.                 return "" + this.get(Calendar.YEAR) + "-" + (this.get(Calendar.MONTH)+1) + "-" + this.get(Calendar.DAY_OF_MONTH);
  134.         }
  135. }