View Javadoc

1   package org.inigma.utopia;
2   
3   import java.util.regex.Matcher;
4   import java.util.regex.Pattern;
5   
6   public class UtopiaDate implements Comparable<UtopiaDate> {
7       public static final UtopiaDate UNKNOWN = new UtopiaDate(Month.January, 0, 0);
8       public static enum Month {January, February, March, April, May, June, July}
9   
10      private static final Pattern PATTERN_DATE = Pattern
11              .compile("(January|February|March|April|May|June|July) (\\d+)\\w*, YR(\\d+)");
12  
13      private int date; // hour of day
14      private Month month; // day of week
15      private int year; // weeks into age
16  
17      public UtopiaDate() {
18          this.month = Month.January;
19          this.date = 0;
20          this.year = 0;
21      }
22  
23      public UtopiaDate(Month month, int date, int year) {
24          this.month = month;
25          this.date = date;
26          this.year = year;
27      }
28  
29      public UtopiaDate(String text) {
30          Matcher matcher = PATTERN_DATE.matcher(text);
31          if (matcher.find()) {
32              this.month = Month.valueOf(matcher.group(1));
33              this.date = Integer.parseInt(matcher.group(2));
34              this.year = Integer.parseInt(matcher.group(3));
35          } else {
36              this.month = Month.January;
37              this.date = 0;
38              this.year = 0;
39          }
40      }
41  
42      public int getDate() {
43          return date;
44      }
45  
46      public Month getMonth() {
47          return month;
48      }
49  
50      public int getYear() {
51          return year;
52      }
53  
54      @Override
55      public String toString() {
56          StringBuilder sb = new StringBuilder();
57          sb.append(month.name());
58          sb.append(" ").append(date);
59          if (date == 1) {
60              sb.append("st");
61          } else if (date == 2) {
62              sb.append("nd");
63          } else if (date == 3) {
64              sb.append("rd");
65          } else {
66              sb.append("th");
67          }
68          sb.append(", YR");
69          sb.append(year);
70          return sb.toString();
71      }
72  
73      public int compareTo(UtopiaDate ud) {
74          int diff = year - ud.year;
75          diff *= 7 * 24;
76          if (diff == 0) {
77              diff = month.ordinal() - ud.month.ordinal();
78              diff *= 24;
79          }
80          if (diff == 0) {
81              diff = date - ud.date;
82          }
83          return diff;
84      }
85  
86      public void setDate(int date) {
87          this.date = date;
88      }
89  
90      public void setMonth(Month month) {
91          this.month = month;
92      }
93  
94      public void setYear(int year) {
95          this.year = year;
96      }
97  }