1 package edu.rice.rubis.servlets; 2 3 import java.util.GregorianCalendar ; 4 5 9 10 public class TimeManagement 11 { 12 13 public TimeManagement() 14 { 15 } 16 17 23 public static String currentDateToString() 24 { 25 GregorianCalendar d = new GregorianCalendar (); 26 return dateToString(d); 27 } 28 29 35 public static String dateToString(GregorianCalendar d) 36 { 37 String result; 38 int year, month, day, hour, minute, second; 39 40 year = d.get(d.YEAR); 41 month = d.get(d.MONTH) + 1; 42 day = d.get(d.DATE); 43 hour = d.get(d.HOUR_OF_DAY); 44 minute = d.get(d.MINUTE); 45 second = d.get(d.SECOND); 46 result = 47 year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; 48 return result; 49 } 50 51 60 public static String diffTime( 61 GregorianCalendar startDate, 62 GregorianCalendar endDate) 63 { 64 int year, month, day, hour, minute, second, millis; 65 String result = ""; 66 67 year = endDate.get(endDate.YEAR) - startDate.get(startDate.YEAR); 68 month = endDate.get(endDate.MONTH) - startDate.get(startDate.MONTH); 69 day = endDate.get(endDate.DATE) - startDate.get(startDate.DATE); 70 hour = 71 endDate.get(endDate.HOUR_OF_DAY) - startDate.get(startDate.HOUR_OF_DAY); 72 minute = endDate.get(endDate.MINUTE) - startDate.get(startDate.MINUTE); 73 second = endDate.get(endDate.SECOND) - startDate.get(startDate.SECOND); 74 millis = 75 endDate.get(endDate.MILLISECOND) - startDate.get(startDate.MILLISECOND); 76 77 if (millis < 0) 78 { 79 millis = millis + 1000; 80 second = second - 1; 81 } 82 if (second < 0) 83 { 84 second = second + 60; 85 minute = minute - 1; 86 } 87 if (minute < 0) 88 { 89 minute = minute + 60; 90 hour = hour - 1; 91 } 92 if (hour < 0) 93 { 94 hour = hour + 24; 95 day = day - 1; 96 } 97 if (day < 0) 98 { 99 day = day + startDate.getActualMaximum(startDate.DAY_OF_MONTH); 100 month = month - 1; 102 } 103 if (month < 0) 104 { 105 month = month + 12; 106 year = year - 1; 107 } 108 109 if (year > 0) 110 result = year + " year"; 111 if (year > 1) 112 result = result + "s "; 113 else 114 result = result + " "; 115 if (month > 0) 116 result = result + month + " month"; 117 if (month > 1) 118 result = result + "s "; 119 else 120 result = result + " "; 121 if (day > 0) 122 result = result + day + " day"; 123 if (day > 1) 124 result = result + "s "; 125 else 126 result = result + " "; 127 if (hour > 0) 128 result = result + hour + " hour"; 129 if (hour > 1) 130 result = result + "s "; 131 else 132 result = result + " "; 133 if (minute > 0) 134 result = result + minute + " minute"; 135 if (minute > 1) 136 result = result + "s "; 137 else 138 result = result + " "; 139 if (second > 0) 140 result = result + second + " second"; 141 if (second > 1) 142 result = result + "s "; 143 else 144 result = result + " "; 145 result = result + millis + " millisecond"; 146 if (millis > 1) 147 result = result + "s "; 148 else 149 result = result + " "; 150 return result; 151 } 152 153 161 public static GregorianCalendar addDays( 162 GregorianCalendar startDate, 163 int durationInDays) 164 { 165 GregorianCalendar date = (GregorianCalendar ) startDate.clone(); 166 date.add(date.DATE, durationInDays); 167 return date; 168 } 169 } 170 | Popular Tags |