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.data; 18 19 import android.annotation.TargetApi; 20 import android.os.Build; 21 22 import java.text.DateFormat; 23 import java.util.Calendar; 24 25 /** 26 * Represents a date (year, month, day) 27 */ 28 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1) 29 public class SimpleDate implements Comparable<SimpleDate> { 30 public int month; // MM 31 public int day; // DD 32 public int year; // YYYY 33 private long timestamp; 34 private String mCachedStringRepresentation; 35 SimpleDate()36 public SimpleDate() { 37 } 38 SimpleDate(long timestamp)39 public SimpleDate(long timestamp) { 40 setTimestamp(timestamp); 41 } 42 43 private static Calendar sCalendarInstance = Calendar.getInstance(); 44 setTimestamp(long timestamp)45 public void setTimestamp(long timestamp) { 46 synchronized (sCalendarInstance) { 47 // TODO(georgescu): find a more efficient way to convert a timestamp to a date? 48 sCalendarInstance.setTimeInMillis(timestamp); 49 this.day = sCalendarInstance.get(Calendar.DATE); 50 this.month = sCalendarInstance.get(Calendar.MONTH); 51 this.year = sCalendarInstance.get(Calendar.YEAR); 52 this.timestamp = timestamp; 53 mCachedStringRepresentation = 54 DateFormat.getDateInstance(DateFormat.SHORT).format(timestamp); 55 } 56 } 57 58 @Override hashCode()59 public int hashCode() { 60 final int prime = 31; 61 int result = 1; 62 result = prime * result + day; 63 result = prime * result + month; 64 result = prime * result + year; 65 return result; 66 } 67 68 @Override equals(Object obj)69 public boolean equals(Object obj) { 70 if (this == obj) { 71 return true; 72 } 73 if (obj == null) { 74 return false; 75 } 76 if (!(obj instanceof SimpleDate)) { 77 return false; 78 } 79 SimpleDate other = (SimpleDate) obj; 80 if (year != other.year) { 81 return false; 82 } 83 if (month != other.month) { 84 return false; 85 } 86 if (day != other.day) { 87 return false; 88 } 89 return true; 90 } 91 92 @Override compareTo(SimpleDate other)93 public int compareTo(SimpleDate other) { 94 int yearDiff = this.year - other.getYear(); 95 if (yearDiff != 0) { 96 return yearDiff; 97 } else { 98 int monthDiff = this.month - other.getMonth(); 99 if (monthDiff != 0) { 100 return monthDiff; 101 } else { 102 return this.day - other.getDay(); 103 } 104 } 105 } 106 getDay()107 public int getDay() { 108 return day; 109 } 110 getMonth()111 public int getMonth() { 112 return month; 113 } 114 getYear()115 public int getYear() { 116 return year; 117 } 118 119 @Override toString()120 public String toString() { 121 if (mCachedStringRepresentation == null) { 122 mCachedStringRepresentation = 123 DateFormat.getDateInstance(DateFormat.SHORT).format(timestamp); 124 } 125 return mCachedStringRepresentation; 126 } 127 } 128