1
2
3
4
5
6
7
8
9 package ca.uhn.cache.internal.util;
10
11 import java.sql.Timestamp;
12 import java.util.Calendar;
13 import java.util.Date;
14
15
16 /***
17 * Utility class to handle time and Univertal Time Coordinates (UTC).
18 *
19 * @author <a href="mailto:alexei.guevara@uhn.on.ca">Alexei Guevara</a>
20 * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:53:25 $ by $Author: bryan_tripp $
21 */
22 public final class UTC {
23
24 /***
25 */
26 private UTC() {
27 super();
28 }
29
30 /***
31 * @return The current <b>UTC</b> time.
32 */
33 public static final Date currentTime() {
34 Timestamp nowTimeStamp = new Timestamp( System.currentTimeMillis() );
35 nowTimeStamp.setNanos( 0 );
36 return new Date( nowTimeStamp.getTime() );
37 }
38
39 /***
40 * Time Arithmetic function.
41 *
42 * Adds the specified (signed) amount of years, months and days to the <code>currentTime</code> and returns it.
43 *
44 * @param theYearsDelta The years to add.
45 * @param theMonthsDelta The months to add.
46 * @param theDaysDelta The days to add.
47 * @return The new date/time.
48 */
49 public static final Date fromCurrentTime( int theYearsDelta, int theMonthsDelta, int theDaysDelta ) {
50 Calendar c = Calendar.getInstance();
51 c.setTime( UTC.currentTime() );
52 c.add( Calendar.YEAR, theYearsDelta );
53 c.add( Calendar.MONTH, theMonthsDelta );
54 c.add( Calendar.DATE, theDaysDelta );
55 return c.getTime();
56 }
57
58 /***
59 * Time Arithmetic function.
60 *
61 * Adds the specified (signed) amount of milliseconds to <code>currentTime</code> and returns it.
62 *
63 * @param theMillis The milliseconds to add.
64 * @return The new date/time.
65 */
66 public static final Date fromCurrentTime( long theMillis ) {
67 Timestamp nowTimeStamp = new Timestamp( System.currentTimeMillis() + theMillis );
68 nowTimeStamp.setNanos( 0 );
69 return new Date( nowTimeStamp.getTime() );
70 }
71
72 }