1 package edu.rice.rubbos.beans; 2 3 import java.util.GregorianCalendar ; 4 import java.lang.String ; 5 6 12 13 public class TimeManagement extends GregorianCalendar 14 { 15 16 20 public TimeManagement() 21 { 22 } 23 24 25 31 public static String currentDateToString() 32 { 33 GregorianCalendar d = new GregorianCalendar (); 34 return dateToString(d); 35 } 36 37 38 46 public static String dateToString(GregorianCalendar d) 47 { 48 String result; 49 int year, month, day, hour, minute, second; 50 51 year = d.get(d.YEAR); 52 month = d.get(d.MONTH) + 1; 53 day = d.get(d.DATE); 54 hour = d.get(d.HOUR_OF_DAY); 55 minute = d.get(d.MINUTE); 56 second = d.get(d.SECOND); 57 result = year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second; 58 return result; 59 } 60 61 62 70 public static long diffTimeInMs(GregorianCalendar startDate, GregorianCalendar endDate) 71 { 72 long year, month, day, hour, minute, second, millis; 73 74 year = endDate.get(endDate.YEAR)-startDate.get(startDate.YEAR); 75 month = endDate.get(endDate.MONTH)-startDate.get(startDate.MONTH); 76 day = endDate.get(endDate.DATE)-startDate.get(startDate.DATE); 77 hour = endDate.get(endDate.HOUR_OF_DAY)-startDate.get(startDate.HOUR_OF_DAY); 78 minute = endDate.get(endDate.MINUTE)-startDate.get(startDate.MINUTE); 79 second = endDate.get(endDate.SECOND)-startDate.get(startDate.SECOND); 80 millis = endDate.get(endDate.MILLISECOND)-startDate.get(startDate.MILLISECOND); 81 82 if (millis < 0) 83 { 84 millis = millis + 1000; 85 second = second - 1; 86 } 87 if (second < 0) 88 { 89 second = second + 60; 90 minute = minute - 1; 91 } 92 if (minute < 0) 93 { 94 minute = minute + 60; 95 hour = hour - 1; 96 } 97 if (hour < 0) 98 { 99 hour = hour + 24; 100 day = day - 1; 101 } 102 if (day < 0) 103 { 104 day = day + startDate.getActualMaximum(startDate.DAY_OF_MONTH); month = month - 1; 106 } 107 if (month < 0) 108 { 109 month = month + 12; 110 year = year - 1; 111 } 112 return millis+(second*1000)+(minute*60000)+(hour*3600000)+(day*24*3600000)+(month*30*24*3600000); } 114 115 116 125 public static String diffTime(GregorianCalendar startDate, GregorianCalendar endDate) 126 { 127 int year, month, day, hour, minute, second, millis; 128 String result = ""; 129 130 year = endDate.get(endDate.YEAR)-startDate.get(startDate.YEAR); 131 month = endDate.get(endDate.MONTH)-startDate.get(startDate.MONTH); 132 day = endDate.get(endDate.DATE)-startDate.get(startDate.DATE); 133 hour = endDate.get(endDate.HOUR_OF_DAY)-startDate.get(startDate.HOUR_OF_DAY); 134 minute = endDate.get(endDate.MINUTE)-startDate.get(startDate.MINUTE); 135 second = endDate.get(endDate.SECOND)-startDate.get(startDate.SECOND); 136 millis = endDate.get(endDate.MILLISECOND)-startDate.get(startDate.MILLISECOND); 137 138 if (millis < 0) 139 { 140 millis = millis + 1000; 141 second = second - 1; 142 } 143 if (second < 0) 144 { 145 second = second + 60; 146 minute = minute - 1; 147 } 148 if (minute < 0) 149 { 150 minute = minute + 60; 151 hour = hour - 1; 152 } 153 if (hour < 0) 154 { 155 hour = hour + 24; 156 day = day - 1; 157 } 158 if (day < 0) 159 { 160 day = day + startDate.getActualMaximum(startDate.DAY_OF_MONTH); month = month - 1; 162 } 163 if (month < 0) 164 { 165 month = month + 12; 166 year = year - 1; 167 } 168 169 if (year > 0) 170 result = year+" year"; 171 if (year > 1) 172 result = result+"s "; 173 else 174 result = result+" "; 175 if (month > 0) 176 result = result+month+" month"; 177 if (month > 1) 178 result = result+"s "; 179 else 180 result = result+" "; 181 if (day > 0) 182 result = result+day+" day"; 183 if (day > 1) 184 result = result+"s "; 185 else 186 result = result+" "; 187 if (hour > 0) 188 result = result+hour+" hour"; 189 if (hour > 1) 190 result = result+"s "; 191 else 192 result = result+" "; 193 if (minute > 0) 194 result = result+minute+" minute"; 195 if (minute > 1) 196 result = result+"s "; 197 else 198 result = result+" "; 199 if (second > 0) 200 result = result+second+" second"; 201 if (second > 1) 202 result = result+"s "; 203 else 204 result = result+" "; 205 result = result+millis+" millisecond"; 206 if (millis > 1) 207 result = result+"s "; 208 else 209 result = result+" "; 210 return result; 211 } 212 213 214 222 public static GregorianCalendar addDays(GregorianCalendar startDate, int durationInDays) 223 { 224 GregorianCalendar date = (GregorianCalendar )startDate.clone(); 225 date.add(date.DATE, durationInDays); 226 return date; 227 } 228 } 229 | Popular Tags |