All Downloads are FREE. Search and download functionalities are using the official Maven repository.

toxgene.util.Date Maven / Gradle / Ivy

/** 
 * Implements a simple wrapper for date values, since I couldn't get all
 * that I needed from GregorianCalendars :) 
 *
 * @author Denilson Barbosa
 * @version 0.1
 */

package toxgene.util;

import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class Date implements Comparable{
  public static final int MAX_DATE = 1;
  public static final int MIN_DATE = 2;

  private GregorianCalendar calendar;
  private SimpleDateFormat df;

  /**
   * Default constructor for date objects.
   */
  public Date(){
	df = new SimpleDateFormat("yyyy-MM-dd");
	calendar = new GregorianCalendar();
  }

  /**
   * Constructor for dates from other dates.
   */
  public Date(Date value){
	df = new SimpleDateFormat("yyyy-MM-dd");
	calendar = (GregorianCalendar) value.calendar.clone();
  }
  
  /**
   * Constructor for dates from Strings
   */
  public Date(String value) throws DateFormatException{
	df = new SimpleDateFormat("yyyy-MM-dd");

	try{
	  df.parse(value);
	  calendar = (GregorianCalendar) df.getCalendar();
	}catch (ParseException e){
	  throw new DateFormatException();
	}
  }

  /**
   * Constructor for dates from YYYY/MM/DD parameters
   */
  public Date(int year, int month, int day){
	df = new SimpleDateFormat("yyyy-MM-dd");
	calendar = new GregorianCalendar(year, month, day);
  }

  /**
   * Constructor for special dates
   */
  public Date(int type){
	df = new SimpleDateFormat("yyyy-MM-dd");
	switch(type){
	case MAX_DATE: {calendar = new GregorianCalendar(4000,1,1); break;}
	case MIN_DATE: {calendar = new GregorianCalendar(0,1,1); break;}
	}
  }

  /**
   * Returns a Date object from the String provided
   */
  static public Date parseDate(String value) throws DateFormatException{
	return new Date(value);
  }

  static public Date valueOf(String value) throws DateFormatException{
	return new Date(value);
  }

  static public String toString(Date date){
	return date.toString();
  }

  public boolean before(Date anotherDate){
	return (this.calendar.before(anotherDate.getCalendar()));
  }

  public boolean after(Date anotherDate){
	return (this.calendar.after(anotherDate.getCalendar()));
  }

  public java.util.Date getTime(){
	return calendar.getTime();
  }

  /**
   * Returns true if both dates occur in the same day
   */
  public boolean on(Date anotherDate){
	GregorianCalendar otherCalendar = anotherDate.getCalendar();

	return ((calendar.get(GregorianCalendar.DAY_OF_MONTH) == 
			 otherCalendar.get(GregorianCalendar.DAY_OF_MONTH)) &&
			(calendar.get(GregorianCalendar.MONTH) == 
			 otherCalendar.get(GregorianCalendar.MONTH)) &&
			(calendar.get(GregorianCalendar.YEAR) == 
			 otherCalendar.get(GregorianCalendar.YEAR)));
  }

  public GregorianCalendar getCalendar(){
	return calendar;
  }

  /**
   * Implements a general comparator method.
   */
  public int compareTo(Object other){
	GregorianCalendar otherCalendar = ((Date) other).getCalendar();
	
	if (this.before((Date) other)){
	  return -1;
	}
	if (this.after((Date) other)){
	  return 1;
	}
	return 0;
  }

  /**
   * Adds the specified number of days to this date
   */
  public Date add(int days){
	calendar.add(GregorianCalendar.DAY_OF_MONTH, days);
	return this;
  }

  /**
   * Subtracts the specified number of days to this date
   */
  public Date sub(int days){
	calendar.add(GregorianCalendar.DAY_OF_MONTH, -days);
	return this;
  }

  /**
   * Adds another date to this date
   */
  public Date add(Date other){
	GregorianCalendar otherCalendar = other.getCalendar();

	return new Date((calendar.get(GregorianCalendar.DAY_OF_MONTH) +
					 otherCalendar.get(GregorianCalendar.DAY_OF_MONTH)),
					(calendar.get(GregorianCalendar.MONTH) + 
					 otherCalendar.get(GregorianCalendar.MONTH)),
					(calendar.get(GregorianCalendar.YEAR) +
					 otherCalendar.get(GregorianCalendar.YEAR)));
  }

  /**
   * Adds another date to this date
   */
  public Date sub(Date other){
	GregorianCalendar otherCalendar = other.getCalendar();

	return new Date((calendar.get(GregorianCalendar.DAY_OF_MONTH) -
					 otherCalendar.get(GregorianCalendar.DAY_OF_MONTH)),
					(calendar.get(GregorianCalendar.MONTH) - 
					 otherCalendar.get(GregorianCalendar.MONTH)),
					(calendar.get(GregorianCalendar.YEAR) -
					 otherCalendar.get(GregorianCalendar.YEAR)));
  }

  public String toString(){
	return (df.format(calendar.getTime()));
  }

  /**
   * returns the number of days between this date and the one given.
   */
  public int difference(Date other){
	GregorianCalendar end = other.calendar;

	int years = calendar.get(GregorianCalendar.YEAR) - 
	  end.get(GregorianCalendar.YEAR);
	if (years < 0){
	  years *= -1;
	}

	int days = 	calendar.get(GregorianCalendar.DAY_OF_YEAR) - 
	  end.get(GregorianCalendar.DAY_OF_YEAR);
	if (days < 0){
	  days *= -1;
	}

	//I know I have to take care of leap years
	return (365*years + days);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy