- package bruce.lib;
-
- import java.io.Serializable;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Arrays;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Locale;
- import java.util.TimeZone;
- import java.util.TreeSet;
-
- public class DateTime implements Serializable, Cloneable, Comparable<DateTime> {
-
- public static DateTime now() {
- return new DateTime();
- }
-
- public static DateTime fromDate(Date date) {
- DateTime d = new DateTime();
- d.c.setTime(date);
- return d;
- }
-
- public static DateTime fromTimestamp(long stamp) {
- DateTime d = new DateTime();
- d.c.setTimeInMillis(stamp);
- return d;
- }
-
- public static DateTime fromCalendar(Calendar c) {
- DateTime d = new DateTime();
- d.c = c;
- return d;
- }
-
- public static DateTime fromString(String date, String format) throws ParseException {
- DateTime d = new DateTime();
- d.c.setTime(new SimpleDateFormat(format).parse(date));
- return d;
- }
-
- public static DateTime fromString(String date, String format, Locale locale) throws ParseException {
- DateTime d = new DateTime();
- d.c.setTime(new SimpleDateFormat(format, locale).parse(date));
- return d;
- }
-
- public static DateTime latest(DateTime... dates) {
- TreeSet<DateTime> set = new TreeSet<DateTime>();
- set.addAll(Arrays.asList(dates));
- return set.last();
- }
-
- public static DateTime oldest(DateTime... dates) {
- TreeSet<DateTime> set = new TreeSet<DateTime>();
- set.addAll(Arrays.asList(dates));
- return set.first();
- }
-
- private Calendar c;
-
- public enum Field {
- ERA, // AD/BC
- YEAR, //
- MONTH, //
- WEEK_OF_YEAR, //
- WEEK_OF_MONTH, //
- AM_PM_MARKER, // AM/PM
- DATE, //
- DAY_OF_MONTH, //
- DAY_OF_WEEK, //
- DAY_OF_WEEK_IN_MONTH, //
- DAY_OF_YEAR, //
- HOUR, //
- MINUTE, //
- SECOND, //
- MILLISECOND, //
- TIMEZONE
- }
-
- public class Month {
- public static final int JANUARY = 0;
- public static final int FEBRUARY = 1;
- public static final int MARCH = 2;
- public static final int APRIL = 3;
- public static final int MAY = 4;
- public static final int JUNE = 5;
- public static final int JULY = 6;
- public static final int AUGUST = 7;
- public static final int SEPTEMBER = 8;
- public static final int OCTOBER = 9;
- public static final int NOVEMBER = 10;
- public static final int DECEMBER = 11;
- public static final int UNDECIMBER = 12;
- }
-
- public class Day {
- public static final int SUNDAY = 1;
- public static final int MONDAY = 2;
- public static final int TUESDAY = 3;
- public static final int WEDNESDAY = 4;
- public static final int THURSDAY = 5;
- public static final int FRIDAY = 6;
- public static final int SATURDAY = 7;
- }
-
- private DateTime() {
- this.c = Calendar.getInstance();
- }
-
- public boolean after(DateTime datetime) {
- return this.toTimeInMillis() > datetime.toTimeInMillis();
- }
-
- public boolean before(DateTime datetime) {
- return this.toTimeInMillis() < datetime.toTimeInMillis();
- }
-
- public DateTime add(Field field, int amount) {
- switch (field) {
- case YEAR:
- this.c.add(Calendar.YEAR, amount);
- return this;
- case MONTH:
- this.c.add(Calendar.MONTH, amount);
- return this;
- case WEEK_OF_YEAR:
- case WEEK_OF_MONTH:
- this.c.add(Calendar.DATE, amount * 7);
- return this;
- case DATE:
- case DAY_OF_MONTH:
- case DAY_OF_WEEK:
- case DAY_OF_WEEK_IN_MONTH:
- case DAY_OF_YEAR:
- this.c.add(Calendar.DATE, amount);
- return this;
- case MINUTE:
- this.c.add(Calendar.MINUTE, amount);
- return this;
- case SECOND:
- this.c.add(Calendar.SECOND, amount);
- return this;
- case HOUR:
- this.c.add(Calendar.HOUR, amount);
- return this;
- case MILLISECOND:
- this.c.add(Calendar.MILLISECOND, amount);
- return this;
- default:
- throw new IllegalArgumentException("Unaccepted field.");
- }
- }
-
- public DateTime addYear(int amount) {
- return this.add(Field.YEAR, amount);
- }
-
- public DateTime addMonth(int amount) {
- return this.add(Field.MONTH, amount);
- }
-
- public DateTime addWeek(int amount) {
- return this.add(Field.WEEK_OF_YEAR, amount);
- }
-
- public DateTime addDay(int amount) {
- return this.add(Field.DAY_OF_YEAR, amount);
- }
-
- public DateTime addMinute(int amount) {
- return this.add(Field.MINUTE, amount);
- }
-
- public DateTime addSecond(int amount) {
- return this.add(Field.SECOND, amount);
- }
-
- public DateTime addHour(int amount) {
- return this.add(Field.HOUR, amount);
- }
-
- public DateTime addMilliSecond(int amount) {
- return this.add(Field.MILLISECOND, amount);
- }
-
- public DateTime set(Field field, int value) {
- switch (field) {
- // case ERA:
- case YEAR:
- this.c.set(Calendar.YEAR, value);
- return this;
- case MONTH:
- this.c.set(Calendar.MONTH, value);
- return this;
- // case WEEK_OF_YEAR:
- // case WEEK_OF_MONTH:
- // case AM_PM_MARKER:
- case DATE:
- this.c.set(Calendar.DATE, value);
- return this;
- // case DAY_OF_MONTH:
- // case DAY_OF_WEEK:
- // case DAY_OF_WEEK_IN_MONTH:
- // case DAY_OF_YEAR:
- case HOUR:
- this.c.set(Calendar.HOUR, value);
- return this;
- case MINUTE:
- this.c.set(Calendar.MINUTE, value);
- return this;
- case SECOND:
- this.c.set(Calendar.SECOND, value);
- return this;
- case MILLISECOND:
- this.c.set(Calendar.MILLISECOND, value);
- return this;
- // case TIMEZONE:
- default:
- throw new IllegalArgumentException("Unaccepted field.");
- }
- }
-
- public DateTime setYear(int value) {
- return this.set(Field.YEAR, value);
- }
-
- public DateTime setMonth(int value) {
- return this.set(Field.MONTH, value);
- }
-
- public DateTime setDate(int value) {
- return this.set(Field.DATE, value);
- }
-
- public DateTime setHour(int value) {
- return this.set(Field.HOUR, value);
- }
-
- public DateTime setMinute(int value) {
- return this.set(Field.MINUTE, value);
- }
-
- public DateTime setSecond(int value) {
- return this.set(Field.SECOND, value);
- }
-
- public DateTime setMilliSecond(int value) {
- return this.set(Field.MILLISECOND, value);
- }
-
- public int get(Field field) {
- switch (field) {
- case YEAR:
- return this.c.get(Calendar.YEAR);
- case MONTH:
- return this.c.get(Calendar.MONTH);
- case WEEK_OF_YEAR:
- return this.c.get(Calendar.WEEK_OF_YEAR);
- case WEEK_OF_MONTH:
- return this.c.get(Calendar.WEEK_OF_MONTH);
- case DATE:
- return this.c.get(Calendar.DATE);
- case DAY_OF_MONTH:
- return this.c.get(Calendar.DAY_OF_MONTH);
- case DAY_OF_WEEK:
- return this.c.get(Calendar.DAY_OF_WEEK);
- case DAY_OF_WEEK_IN_MONTH:
- return this.c.get(Calendar.DAY_OF_WEEK_IN_MONTH);
- case DAY_OF_YEAR:
- return this.c.get(Calendar.DAY_OF_YEAR);
- case MINUTE:
- return this.c.get(Calendar.MINUTE);
- case SECOND:
- return this.c.get(Calendar.SECOND);
- case HOUR:
- return this.c.get(Calendar.HOUR);
- case MILLISECOND:
- return this.c.get(Calendar.MILLISECOND);
- default:
- throw new IllegalArgumentException("Unaccepted field.");
- }
- }
-
- public int getYear() {
- return this.get(Field.YEAR);
- }
-
- public int getMonth() {
- return this.get(Field.MONTH);
- }
-
- public int getWeekOfYear() {
- return this.get(Field.WEEK_OF_YEAR);
- }
-
- public int getWeekOfMonth() {
- return this.get(Field.WEEK_OF_MONTH);
- }
-
- public int getDate() {
- return this.get(Field.DATE);
- }
-
- public int getDayOfMonth() {
- return this.get(Field.DAY_OF_MONTH);
- }
-
- public int getDayOfWeek() {
- return this.get(Field.DAY_OF_WEEK);
- }
-
- public int getDayOfWeekInMonth() {
- return this.get(Field.DAY_OF_WEEK_IN_MONTH);
- }
-
- public int getDayOfYear() {
- return this.get(Field.DAY_OF_YEAR);
- }
-
- public int getHour() {
- return this.get(Field.HOUR);
- }
-
- public int getMinute() {
- return this.get(Field.MINUTE);
- }
-
- public int getSecond() {
- return this.get(Field.SECOND);
- }
-
- public int getMilliSecond() {
- return this.get(Field.MILLISECOND);
- }
-
- public TimeZone getTimeZone() {
- return this.c.getTimeZone();
- }
-
- public DateTime setTimeZone(TimeZone zone) {
- this.c.setTimeZone(zone);
- return this;
- }
-
- public long toTimeInMillis() {
- return this.c.getTimeInMillis();
- }
-
- public Date toDate() {
- return this.c.getTime();
- }
-
- public Calendar toCalendar() {
- return this.c;
- }
-
- @Override
- public String toString() {
- return this.c.getTime().toString();
- }
-
- public String toString(String format) {
- SimpleDateFormat f = new SimpleDateFormat(format);
- return f.format(this.c.getTime());
- }
-
- public String toString(String format, Locale locale) {
- SimpleDateFormat f = new SimpleDateFormat(format, locale);
- return f.format(this.c.getTime());
- }
-
- @Override
- public int hashCode() {
- return c.getTime().hashCode();
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- DateTime other = (DateTime) obj;
- return this.c.getTimeInMillis() == other.toTimeInMillis();
- }
-
- @Override
- public int compareTo(DateTime o) {
- return (int) (this.toTimeInMillis() - o.toTimeInMillis());
- }
-
- }