1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 package com.android.gallery3d.ingest; 18 19 import java.text.DateFormat; 20 import java.util.Calendar; 21 22 /** 23 * Represents a date (year, month, day) 24 */ 25 public class SimpleDate implements Comparable<SimpleDate> { 26 public int month; // MM 27 public int day; // DD 28 public int year; // YYYY 29 private long timestamp; 30 private String mCachedStringRepresentation; 31 32 public SimpleDate() { 33 } 34 35 public SimpleDate(long timestamp) { 36 setTimestamp(timestamp); 37 } 38 39 private static Calendar sCalendarInstance = Calendar.getInstance(); 40 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); 50 } 51 } 52 53 @Override 54 public int hashCode() { 55 final int prime = 31; 56 int result = 1; 57 result = prime * result + day; 58 result = prime * result + month; 59 result = prime * result + year; 60 return result; 61 } 62 63 @Override 64 public boolean equals(Object obj) { 65 if (this == obj) 66 return true; 67 if (obj == null) 68 return false; 69 if (!(obj instanceof SimpleDate)) 70 return false; 71 SimpleDate other = (SimpleDate) obj; 72 if (year != other.year) 73 return false; 74 if (month != other.month) 75 return false; 76 if (day != other.day) 77 return false; 78 return true; 79 } 80 81 @Override 82 public int compareTo(SimpleDate other) { 83 int yearDiff = this.year - other.getYear(); 84 if (yearDiff != 0) 85 return yearDiff; 86 else { 87 int monthDiff = this.month - other.getMonth(); 88 if (monthDiff != 0) 89 return monthDiff; 90 else 91 return this.day - other.getDay(); 92 } 93 } 94 95 public int getDay() { 96 return day; 97 } 98 99 public int getMonth() { 100 return month; 101 } 102 103 public int getYear() { 104 return year; 105 } 106 107 @Override 108 public String toString() { 109 if (mCachedStringRepresentation == null) { 110 mCachedStringRepresentation = DateFormat.getDateInstance(DateFormat.SHORT).format(timestamp); 111 } 112 return mCachedStringRepresentation; 113 } 114 } 115