1   package com.aragost.javahg;
2   
3   import java.util.Date;
4   import java.util.TimeZone;
5   
6   
7   
8   
9   public class DateTime {
10  
11      
12  
13  
14      private long timestamp;
15  
16      
17  
18  
19      private int tzOffset;
20  
21      
22  
23  
24  
25  
26  
27      public DateTime(long timestamp, int tzOffset) {
28          this.timestamp = timestamp;
29          this.tzOffset = tzOffset;
30      }
31  
32      
33  
34  
35  
36  
37  
38      public DateTime(Date date, TimeZone tz) {
39          this.timestamp = date.getTime();
40          this.tzOffset = tz.getOffset(this.timestamp);
41      }
42  
43      
44  
45  
46  
47      @Deprecated
48      public DateTime(Date date) {
49          this(date, TimeZone.getDefault());
50      }
51  
52      
53  
54  
55  
56  
57  
58  
59  
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  
74  
75      public Date getDate() {
76          return new Date(this.timestamp);
77      }
78  
79      
80  
81  
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 }