1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.util;
18
19 /***
20 * Utilities for converting times
21 * @author Greg Luck
22 */
23 public class TimeUtil {
24
25 /***
26 * Constant that contains the amount of milliseconds in a second
27 */
28 static final long ONE_SECOND = 1000L;
29
30 /***
31 * Converts milliseconds to seconds
32 * @param timeInMillis
33 * @return The equivalent time in seconds
34 */
35 public static int toSecs(long timeInMillis) {
36
37
38
39 return (int)Math.ceil((double)timeInMillis / ONE_SECOND);
40 }
41
42 /***
43 * Converts seconds to milliseconds, with a precision of 1 second
44 * @param timeInSecs the time in seconds
45 * @return The equivalent time in milliseconds
46 */
47 public static long toMillis(int timeInSecs) {
48 return timeInSecs * ONE_SECOND;
49 }
50
51 /***
52 * Converts a long seconds value to an int seconds value and takes into account overflow
53 * from the downcast by switching to Integer.MAX_VALUE.
54 * @param seconds Long value
55 * @return Same int value unless long > Integer.MAX_VALUE in which case MAX_VALUE is returned
56 */
57 public static int convertTimeToInt(long seconds) {
58 if (seconds > Integer.MAX_VALUE) {
59 return Integer.MAX_VALUE;
60 } else {
61 return (int) seconds;
62 }
63 }
64 }