Webdings Tools (1.0.1) | ||
Frames | No Frames |
1: /* TimeSince.java - Copyright (c) 2005 by Stefan Thesing 2: <p>This file is part of Webdings Tools.</p> 3: <p>Webdings Tools is free software; you can redistribute it and/or modify 4: it under the terms of the GNU General Public License as published by 5: the Free Software Foundation; either version 2 of the License, or 6: (at your option) any later version.</p> 7: <p>Webdings Tools is distributed in the hope that it will be useful, 8: but WITHOUT ANY WARRANTY; without even the implied warranty of 9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10: GNU General Public License for more details.</p> 11: <p>You should have received a copy of the GNU General Public License 12: along with Webdings Tools; if not, write to the<br> 13: Free Software Foundation, Inc.,<br> 14: 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA<br> 15: */ 16: package de.webdings.tools.time; 17: 18: import java.util.Calendar; 19: import java.util.GregorianCalendar; 20: 21: /** 22: * TimeSince is used to tell how much time has passed 23: * between two points in time. 24: * 25: * @author Stefan Thesing<br> 26: * Website: <a href="http://www.webdings.de">http://www.webdings.de</a> 27: * @version 1.0 23.05.2005 28: */ 29: public class TimeSince { 30: // Operations 31: /** 32: * Returns the timespan between two points in time 33: * in full milliseconds. 34: * @param start Beginning of the measured time-span. 35: * @param stop End of the measured time-span. 36: * @return Time-span in full milliseconds 37: */ 38: public long getMilliSecondsSince(GregorianCalendar start, GregorianCalendar stop) { 39: return (stop.getTimeInMillis() - start.getTimeInMillis()); 40: } 41: 42: /** 43: * Returns the timespan between 44: * 1. a specified past point of time and<br> 45: * 2. the moment this method is called (now) 46: * in full milliseconds 47: * @param start past point 48: * @return Time-span in full milliseconds 49: */ 50: public long getMilliSecondsSince(GregorianCalendar start) { 51: return this.getMilliSecondsSince(start, new GregorianCalendar()); 52: } 53: 54: /** 55: * Returns the timespan between two specified 56: * points in time in full seconds. 57: * @param start Beginning of the measured time-span. 58: * @param stop End of the measured time-span. 59: * @return Time-span in full seconds 60: */ 61: public long getSecondsSince(GregorianCalendar start, GregorianCalendar stop) { 62: return (this.getMilliSecondsSince(start, stop)/1000); 63: } 64: 65: /** 66: * Returns the timespan between 67: * 1. a specified past point of time and<br> 68: * 2. the moment this method is called (now) 69: * in full seconds 70: * @param start past point 71: * @return Time-span in full seconds 72: */ 73: public long getSecondsSince(GregorianCalendar start) { 74: return this.getSecondsSince(start, new GregorianCalendar()); 75: } 76: 77: /** 78: * Returns the timespan between two specified 79: * points in time in full minutes 80: * @param start Beginning of the measured time-span. 81: * @param stop End of the measured time-span. 82: * @return Time-span in full minutes 83: */ 84: public long getMinutesSince(GregorianCalendar start, GregorianCalendar stop) { 85: return (this.getSecondsSince(start, stop)/60); 86: } 87: 88: /** 89: * Returns the timespan between 90: * 1. a specified past point of time and<br> 91: * 2. the moment this method is called (now) 92: * in full minutes 93: * @param start past point 94: * @return Time-span in full minutes 95: */ 96: public long getMinutesSince(GregorianCalendar start) { 97: return this.getMinutesSince(start, new GregorianCalendar()); 98: } 99: 100: /** 101: * Returns the timespan between two specified 102: * points in time in full hours 103: * @param start Beginning of the measured time-span. 104: * @param stop End of the measured time-span. 105: * @return Time-span in full hours 106: */ 107: public long getHoursSince(GregorianCalendar start, GregorianCalendar stop) { 108: return (this.getMinutesSince(start, stop)/60); 109: } 110: 111: /** 112: * Returns the timespan between 113: * 1. a specified past point of time and<br> 114: * 2. the moment this method is called (now) 115: * in full hours 116: * @param start past point 117: * @return Time-span in full hours 118: */ 119: public long getHoursSince(GregorianCalendar start) { 120: return this.getHoursSince(start, new GregorianCalendar()); 121: } 122: 123: /** 124: * Returns the timespan between two specified 125: * points in time in full days 126: * @param start Beginning of the measured time-span. 127: * @param stop End of the measured time-span. 128: * @return Time-span in full days 129: */ 130: public int getDaysSince(GregorianCalendar start, GregorianCalendar stop) { 131: Long hours = new Long(this.getHoursSince(start, stop)); 132: return (hours.intValue()/24); 133: } 134: 135: /** 136: * Returns the timespan between 137: * 1. a specified past point of time and<br> 138: * 2. the moment this method is called (now) 139: * in full days 140: * @param start past point 141: * @return Time-span in full days 142: */ 143: public int getDaysSince(GregorianCalendar start) { 144: return this.getDaysSince(start, new GregorianCalendar()); 145: } 146: 147: /** 148: * Returns the timespan between two specified 149: * points in time in full weeks 150: * @param start Beginning of the measured time-span. 151: * @param stop End of the measured time-span. 152: * @return Time-span in full weeks 153: */ 154: public int getWeeksSince(GregorianCalendar start, GregorianCalendar stop) { 155: return this.getDaysSince(start, stop)/7; 156: } 157: 158: /** 159: * Returns the timespan between 160: * 1. a specified past point of time and<br> 161: * 2. the moment this method is called (now) 162: * in full weeks 163: * @param start past point 164: * @return Time-span in full weeks 165: */ 166: public int getWeeksSince(GregorianCalendar start) { 167: return this.getWeeksSince(start, new GregorianCalendar()); 168: } 169: 170: /** 171: * Returns the timespan between two specified 172: * points in time in full months 173: * @param start Beginning of the measured time-span. 174: * @param stop End of the measured time-span. 175: * @return Time-span in full months 176: */ 177: public int getMonthsSince(GregorianCalendar start, GregorianCalendar stop) { 178: GregorianCalendar startClone = new GregorianCalendar 179: (start.get(Calendar.YEAR), start.get(Calendar.MONTH), 180: start.get(Calendar.DAY_OF_MONTH)); 181: start = startClone; 182: int months = 0; 183: while(start.get(Calendar.MONTH) != stop.get(Calendar.MONTH)) { 184: start.roll(Calendar.MONTH, true); 185: if(start.getTime().compareTo(stop.getTime())<0){ 186: ++months; 187: } 188: start.roll(Calendar.MONTH, true); 189: if(start.getTime().compareTo(stop.getTime())>0) { 190: start.roll(Calendar.MONTH, false); 191: break; 192: } 193: start.roll(Calendar.MONTH, false); 194: } 195: return months+(this.getYearsSince(start, stop)*12); 196: } 197: 198: /** 199: * Returns the timespan between 200: * 1. a specified past point of time and<br> 201: * 2. the moment this method is called (now) 202: * in full months 203: * @param start past point 204: * @return Time-span in full months 205: */ 206: public int getMonthsSince(GregorianCalendar start) { 207: return this.getMonthsSince(start, new GregorianCalendar()); 208: } 209: 210: /** 211: * Returns the timespan between two specified 212: * points in time in full years 213: * @param start Beginning of the measured time-span. 214: * @param stop End of the measured time-span. 215: * @return Time-span in full years 216: */ 217: public int getYearsSince(GregorianCalendar start, GregorianCalendar stop) { 218: GregorianCalendar startClone = new GregorianCalendar 219: (start.get(Calendar.YEAR), start.get(Calendar.MONTH), 220: start.get(Calendar.DAY_OF_MONTH)); 221: start = startClone; 222: int years = 0; 223: while(start.get(Calendar.YEAR) != stop.get(Calendar.YEAR)) { 224: start.roll(Calendar.YEAR, true); 225: if(start.getTime().compareTo(stop.getTime())<0){ 226: ++years; 227: } 228: start.roll(Calendar.YEAR, true); 229: if(start.getTime().compareTo(stop.getTime())>0) { 230: start.roll(Calendar.YEAR, false); 231: break; 232: } 233: start.roll(Calendar.YEAR, false); 234: } 235: return years; 236: } 237: 238: /** 239: * Returns the timespan between 240: * 1. a specified past point of time and<br> 241: * 2. the moment this method is called (now) 242: * in full years 243: * @param start past point 244: * @return Time-span in full years 245: */ 246: public int getYearsSince(GregorianCalendar start) { 247: return this.getYearsSince(start, new GregorianCalendar()); 248: } 249: 250: /** 251: * Returns the timespan between two specified 252: * points in time as an array containing 253: * <table> 254: * <tr><th>Index</th><th>Information</th></tr> 255: * <tr><td>0</td><td>full years</td></tr> 256: * <tr><td>1</td><td>full months</td></tr> 257: * <tr><td>2</td><td>full days</td></tr> 258: * @param start Beginning of the measured time-span. 259: * @param stop End of the measured time-span. 260: * @return Time-span in full years, full months, full days as 261: * an array containing <code>int</code>s. 262: */ 263: public int[] getDaysMonthsYearsSince(GregorianCalendar start, GregorianCalendar stop) { 264: GregorianCalendar startClone = new GregorianCalendar 265: (start.get(Calendar.YEAR), start.get(Calendar.MONTH), 266: start.get(Calendar.DAY_OF_MONTH)); 267: start = startClone; 268: int years=0, months=0, days=0; 269: while(start.get(Calendar.YEAR) != stop.get(Calendar.YEAR)) { 270: start.roll(Calendar.YEAR, true); 271: if(start.getTime().compareTo(stop.getTime())<0){ 272: ++years; 273: } 274: start.roll(Calendar.YEAR, true); 275: if(start.getTime().compareTo(stop.getTime())>0) { 276: start.roll(Calendar.YEAR, false); 277: break; 278: } 279: start.roll(Calendar.YEAR, false); 280: } 281: while(start.get(Calendar.MONTH) != stop.get(Calendar.MONTH)) { 282: start.roll(Calendar.MONTH, true); 283: if(start.getTime().compareTo(stop.getTime())<0){ 284: ++months; 285: } 286: start.roll(Calendar.MONTH, true); 287: if(start.getTime().compareTo(stop.getTime())>0) { 288: start.roll(Calendar.MONTH, false); 289: break; 290: } 291: start.roll(Calendar.MONTH, false); 292: } 293: while(start.get(Calendar.DAY_OF_MONTH) != stop.get(Calendar.DAY_OF_MONTH)) { 294: start.roll(Calendar.DAY_OF_MONTH, true); 295: if(start.getTime().compareTo(stop.getTime())<0){ 296: ++days; 297: } 298: start.roll(Calendar.DAY_OF_MONTH, true); 299: if(start.getTime().compareTo(stop.getTime())>0) { 300: start.roll(Calendar.DAY_OF_MONTH, false); 301: break; 302: } 303: start.roll(Calendar.DAY_OF_MONTH, false); 304: } 305: int[] array = new int[3]; 306: array[0] = years; 307: array[1] = months; 308: array[2] = days; 309: return array; 310: } 311: 312: /** 313: * Returns the timespan between 314: * 1. a specified past point of time and<br> 315: * 2. the moment this method is called (now) 316: * as an array containing 317: * <table> 318: * <tr><th>Index</th><th>Information</th></tr> 319: * <tr><td>0</td><td>full years</td></tr> 320: * <tr><td>1</td><td>full months</td></tr> 321: * <tr><td>2</td><td>full days</td></tr> 322: * @param start past point 323: * @return Time-span in full years, full months, full days as 324: * an array containing <code>int</code>s. 325: */ 326: public int[] getDaysMonthsYearsSince(GregorianCalendar start) { 327: return this.getDaysMonthsYearsSince(start, new GregorianCalendar()); 328: } 329: 330: /** 331: * Returns the timespan between two specified 332: * points in time as a String in the following form: 333: * <p><code>42 years, 5 months, 23 days</code></p> 334: * @param start Beginning of the measured time-span. 335: * @param stop End of the measured time-span. 336: * @return Time-span in full years, full months 337: * and full days as a String 338: */ 339: public String getDaysMonthsYearsSinceAsString(GregorianCalendar start, GregorianCalendar stop) { 340: int[] value = this.getDaysMonthsYearsSince(start, stop); 341: return value[0] + " years, " + value[1] + " months, " + value[2] + " days"; 342: } 343: 344: /** 345: * Returns the timespan between 346: * 1. a specified past point of time and<br> 347: * 2. the moment this method is called (now) 348: * as a String in the following form: 349: * <p><code>42 years, 5 months, 23 days</code></p> 350: * @param start past point 351: * @return Time-span in full years, full months 352: * and full days as a String 353: */ 354: public String getDaysMonthsYearsSinceAsString(GregorianCalendar start) { 355: return this.getDaysMonthsYearsSinceAsString(start, new GregorianCalendar()); 356: } 357: }
Webdings Tools (1.0.1) |
© 2005 by Stefan Thesing;
Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.