1 24 25 package edu.rice.rubbos.servlets; 26 27 import java.util.GregorianCalendar ; 28 29 34 35 public class TimeManagement 36 { 37 38 public TimeManagement() 39 { 40 } 41 42 48 public static String currentDateToString() 49 { 50 GregorianCalendar d = new GregorianCalendar (); 51 return dateToString(d); 52 } 53 54 60 public static String dateToString(GregorianCalendar d) 61 { 62 String result; 63 int year, month, day, hour, minute, second; 64 65 year = d.get(d.YEAR); 66 month = d.get(d.MONTH) + 1; 67 day = d.get(d.DATE); 68 hour = d.get(d.HOUR_OF_DAY); 69 minute = d.get(d.MINUTE); 70 second = d.get(d.SECOND); 71 result = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" 72 + second; 73 return result; 74 } 75 76 85 public static String diffTime(GregorianCalendar startDate, 86 GregorianCalendar endDate) 87 { 88 int year, month, day, hour, minute, second, millis; 89 String result = ""; 90 91 year = endDate.get(endDate.YEAR) - startDate.get(startDate.YEAR); 92 month = endDate.get(endDate.MONTH) - startDate.get(startDate.MONTH); 93 day = endDate.get(endDate.DATE) - startDate.get(startDate.DATE); 94 hour = endDate.get(endDate.HOUR_OF_DAY) 95 - startDate.get(startDate.HOUR_OF_DAY); 96 minute = endDate.get(endDate.MINUTE) - startDate.get(startDate.MINUTE); 97 second = endDate.get(endDate.SECOND) - startDate.get(startDate.SECOND); 98 millis = endDate.get(endDate.MILLISECOND) 99 - startDate.get(startDate.MILLISECOND); 100 101 if (millis < 0) 102 { 103 millis = millis + 1000; 104 second = second - 1; 105 } 106 if (second < 0) 107 { 108 second = second + 60; 109 minute = minute - 1; 110 } 111 if (minute < 0) 112 { 113 minute = minute + 60; 114 hour = hour - 1; 115 } 116 if (hour < 0) 117 { 118 hour = hour + 24; 119 day = day - 1; 120 } 121 if (day < 0) 122 { 123 day = day + startDate.getActualMaximum(startDate.DAY_OF_MONTH); month = month - 1; 127 } 128 if (month < 0) 129 { 130 month = month + 12; 131 year = year - 1; 132 } 133 134 if (year > 0) 135 result = year + " year"; 136 if (year > 1) 137 result = result + "s "; 138 else 139 result = result + " "; 140 if (month > 0) 141 result = result + month + " month"; 142 if (month > 1) 143 result = result + "s "; 144 else 145 result = result + " "; 146 if (day > 0) 147 result = result + day + " day"; 148 if (day > 1) 149 result = result + "s "; 150 else 151 result = result + " "; 152 if (hour > 0) 153 result = result + hour + " hour"; 154 if (hour > 1) 155 result = result + "s "; 156 else 157 result = result + " "; 158 if (minute > 0) 159 result = result + minute + " minute"; 160 if (minute > 1) 161 result = result + "s "; 162 else 163 result = result + " "; 164 if (second > 0) 165 result = result + second + " second"; 166 if (second > 1) 167 result = result + "s "; 168 else 169 result = result + " "; 170 result = result + millis + " millisecond"; 171 if (millis > 1) 172 result = result + "s "; 173 else 174 result = result + " "; 175 return result; 176 } 177 178 186 public static GregorianCalendar addDays(GregorianCalendar startDate, 187 int durationInDays) 188 { 189 GregorianCalendar date = (GregorianCalendar ) startDate.clone(); 190 date.add(date.DATE, durationInDays); 191 return date; 192 } 193 } | Popular Tags |