package communitycommons;_x000D_
_x000D_
import java.util.Calendar;_x000D_
import java.util.Date;_x000D_
_x000D_
import communitycommons.proxies.DatePartSelector;_x000D_
_x000D_
public class DateTime_x000D_
{_x000D_
	/**_x000D_
	 * @author mwe_x000D_
	 * Berekent aantal jaar sinds een bepaalde datum. Als einddatum == null, het huidige tijdstip wordt gebruikt_x000D_
	 * Code is gebaseerd op http://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java _x000D_
	 */_x000D_
	public static long yearsBetween(Date birthdate, Date comparedate) {_x000D_
		if (birthdate == null)_x000D_
			return -1L; _x000D_
		_x000D_
		Calendar now = Calendar.getInstance();_x000D_
		if (comparedate != null)_x000D_
			now.setTime(comparedate);_x000D_
		Calendar dob = Calendar.getInstance();_x000D_
		dob.setTime(birthdate);_x000D_
		if (dob.after(now)) _x000D_
			return -1L;_x000D_
		_x000D_
		int year1 = now.get(Calendar.YEAR);_x000D_
		int year2 = dob.get(Calendar.YEAR);_x000D_
		long age = year1 - year2;_x000D_
		int month1 = now.get(Calendar.MONTH);_x000D_
		int month2 = dob.get(Calendar.MONTH);_x000D_
		if (month2 > month1) {_x000D_
		  age--;_x000D_
		} else if (month1 == month2) {_x000D_
		  int day1 = now.get(Calendar.DAY_OF_MONTH);_x000D_
		  int day2 = dob.get(Calendar.DAY_OF_MONTH);_x000D_
		  if (day2 > day1) {_x000D_
		    age--;_x000D_
		  }_x000D_
		}_x000D_
		return age;		_x000D_
	}_x000D_
_x000D_
	public static long dateTimeToLong(Date date)_x000D_
	{_x000D_
		return date.getTime();_x000D_
	}_x000D_
_x000D_
	public static Date longToDateTime(Long value)_x000D_
	{_x000D_
		return new Date(value);_x000D_
	}_x000D_
	_x000D_
	public static long dateTimeToInteger(Date date, DatePartSelector selectorObj)_x000D_
	{_x000D_
		Calendar newDate = Calendar.getInstance();_x000D_
		newDate.setTime(date);_x000D_
		int value = -1;_x000D_
		switch (selectorObj) {_x000D_
			case year : value = newDate.get(Calendar.YEAR); break;_x000D_
			case month : value = newDate.get(Calendar.MONTH)+1; break; // Return starts at 0_x000D_
			case day : value = newDate.get(Calendar.DAY_OF_MONTH); break;_x000D_
			default : break;_x000D_
		}_x000D_
		return value;_x000D_
	}_x000D_
		_x000D_
}_x000D_

/**_x000D_
	 * @author mwe_x000D_
	 * Berekent aantal jaar sinds een bepaalde datum. Als einddatum == null, het huidige tijdstip wordt gebruikt_x000D_
	 * Code is gebaseerd op http://stackoverflow.com/questions/1116123/how-do-i-calculate-someones-age-in-java _x000D_
	 */
package communitycommons;_x000D_
_x000D_
import java.util.Calendar;_x000D_
import java.util.Date;_x000D_
_x000D_
import communitycommons.proxies.DatePartSelector;_x000D_
_x000D_
public class DateTime_x000D_
{_x000D_
	/**_x000D_
	 * @author mwe_x000D_
<SUF>*/_x000D_
	public static long yearsBetween(Date birthdate, Date comparedate) {_x000D_
		if (birthdate == null)_x000D_
			return -1L; _x000D_
		_x000D_
		Calendar now = Calendar.getInstance();_x000D_
		if (comparedate != null)_x000D_
			now.setTime(comparedate);_x000D_
		Calendar dob = Calendar.getInstance();_x000D_
		dob.setTime(birthdate);_x000D_
		if (dob.after(now)) _x000D_
			return -1L;_x000D_
		_x000D_
		int year1 = now.get(Calendar.YEAR);_x000D_
		int year2 = dob.get(Calendar.YEAR);_x000D_
		long age = year1 - year2;_x000D_
		int month1 = now.get(Calendar.MONTH);_x000D_
		int month2 = dob.get(Calendar.MONTH);_x000D_
		if (month2 > month1) {_x000D_
		  age--;_x000D_
		} else if (month1 == month2) {_x000D_
		  int day1 = now.get(Calendar.DAY_OF_MONTH);_x000D_
		  int day2 = dob.get(Calendar.DAY_OF_MONTH);_x000D_
		  if (day2 > day1) {_x000D_
		    age--;_x000D_
		  }_x000D_
		}_x000D_
		return age;		_x000D_
	}_x000D_
_x000D_
	public static long dateTimeToLong(Date date)_x000D_
	{_x000D_
		return date.getTime();_x000D_
	}_x000D_
_x000D_
	public static Date longToDateTime(Long value)_x000D_
	{_x000D_
		return new Date(value);_x000D_
	}_x000D_
	_x000D_
	public static long dateTimeToInteger(Date date, DatePartSelector selectorObj)_x000D_
	{_x000D_
		Calendar newDate = Calendar.getInstance();_x000D_
		newDate.setTime(date);_x000D_
		int value = -1;_x000D_
		switch (selectorObj) {_x000D_
			case year : value = newDate.get(Calendar.YEAR); break;_x000D_
			case month : value = newDate.get(Calendar.MONTH)+1; break; // Return starts at 0_x000D_
			case day : value = newDate.get(Calendar.DAY_OF_MONTH); break;_x000D_
			default : break;_x000D_
		}_x000D_
		return value;_x000D_
	}_x000D_
		_x000D_
}_x000D_
