View Javadoc

1   package com.aragost.javahg;
2   
3   import java.util.Date;
4   import java.util.TimeZone;
5   
6   /**
7    * Represents a timestamp and a timezone offset
8    */
9   public class DateTime {
10  
11      /**
12       * Timestamp in milliseconds since 1970
13       */
14      private long timestamp;
15  
16      /**
17       * Timezone offset in milliseconds
18       */
19      private int tzOffset;
20  
21      /**
22       * Construct a new DateTime with the specified timestamp and timezone
23       * 
24       * @param timestamp
25       * @param tzOffset
26       */
27      public DateTime(long timestamp, int tzOffset) {
28          this.timestamp = timestamp;
29          this.tzOffset = tzOffset;
30      }
31  
32      /**
33       * Construct a new DateTime with the specified date and timezone
34       * 
35       * @param date
36       * @param tz
37       */
38      public DateTime(Date date, TimeZone tz) {
39          this.timestamp = date.getTime();
40          this.tzOffset = tz.getOffset(this.timestamp);
41      }
42  
43      /**
44       * @deprecated Use constructor with tz argument
45       * @param date
46       */
47      @Deprecated
48      public DateTime(Date date) {
49          this(date, TimeZone.getDefault());
50      }
51  
52      /**
53       * Create a new instance by parsing the given String.
54       * <p>
55       * The string should be in standard Mercurial format, i.e.
56       * '<seconds since 1970> <timezone offset in seconds>'
57       * 
58       * @param string
59       * @return
60       */
61      public static DateTime parse(String string) {
62          String[] parts = string.split(" ");
63          if (parts.length != 2) {
64              throw new IllegalArgumentException("Wrong date format: " + string);
65          }
66          long millis = 1000L * Long.valueOf(parts[0]);
67          int timezoneOffset = 1000 * Integer.valueOf(parts[1]);
68          return new DateTime(millis, timezoneOffset);
69      }
70  
71      /**
72       * 
73       * @return the timestamp as a java.util.Date object
74       */
75      public Date getDate() {
76          return new Date(this.timestamp);
77      }
78  
79      /**
80       * 
81       * @return the time/zone formatted for Mercurial
82       */
83      public String getHgString() {
84          return "" + (this.timestamp / 1000) + " " + this.tzOffset / 1000;
85      }
86  
87      @Override
88      public String toString() {
89          return getHgString();
90      }
91  
92      @Override
93      public int hashCode() {
94          final int prime = 31;
95          int result = 1;
96          result = prime * result + (int) (timestamp ^ (timestamp >>> 32));
97          result = prime * result + tzOffset;
98          return result;
99      }
100 
101     @Override
102     public boolean equals(Object obj) {
103         if (this == obj)
104             return true;
105         if (obj == null)
106             return false;
107         if (getClass() != obj.getClass())
108             return false;
109         DateTime other = (DateTime) obj;
110         if (timestamp != other.timestamp)
111             return false;
112         if (tzOffset != other.tzOffset)
113             return false;
114         return true;
115     }
116 
117 }