2 * Copyright (C) 2013 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.gallery3d.ingest;
19 import java.text.DateFormat;
20 import java.util.Calendar;
23 * Represents a date (year, month, day)
25 public class SimpleDate implements Comparable<SimpleDate> {
26 public int month; // MM
28 public int year; // YYYY
29 private long timestamp;
30 private String mCachedStringRepresentation;
35 public SimpleDate(long timestamp) {
36 setTimestamp(timestamp);
39 private static Calendar sCalendarInstance = Calendar.getInstance();
41 public void setTimestamp(long timestamp) {
42 synchronized (sCalendarInstance) {
43 // TODO find a more efficient way to convert a timestamp to a date?
44 sCalendarInstance.setTimeInMillis(timestamp);
45 this.day = sCalendarInstance.get(Calendar.DATE);
46 this.month = sCalendarInstance.get(Calendar.MONTH);
47 this.year = sCalendarInstance.get(Calendar.YEAR);
48 this.timestamp = timestamp;
49 mCachedStringRepresentation = DateFormat.getDateInstance(DateFormat.SHORT).format(timestamp);
54 public int hashCode() {
57 result = prime * result + day;
58 result = prime * result + month;
59 result = prime * result + year;
64 public boolean equals(Object obj) {
69 if (!(obj instanceof SimpleDate))
71 SimpleDate other = (SimpleDate) obj;
72 if (year != other.year)
74 if (month != other.month)
82 public int compareTo(SimpleDate other) {
83 int yearDiff = this.year - other.getYear();
87 int monthDiff = this.month - other.getMonth();
91 return this.day - other.getDay();
99 public int getMonth() {
103 public int getYear() {
108 public String toString() {
109 if (mCachedStringRepresentation == null) {
110 mCachedStringRepresentation = DateFormat.getDateInstance(DateFormat.SHORT).format(timestamp);
112 return mCachedStringRepresentation;