Java tutorial
/******************************************************************************* * Copyright 2012 Apigee Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package org.usergrid.utils; import com.google.common.base.CharMatcher; import com.google.common.base.Splitter; public class TimeUtils { public static long millisToMinutes(long millis) { return millis / 1000 / 60; } public static long minutesToMillis(long minutes) { return minutes * 60 * 100; } public static long millisToHours(long millis) { return millis / 1000 / 60 / 60; } public static long hoursToMillis(long hours) { return hours * 60 * 60 * 100; } public static long millisToDays(long millis) { return millis / 1000 / 60 / 60 / 24; } public static long daysToMillis(long days) { return days * 24 * 60 * 60 * 100; } public static long millisToWeeks(long millis) { return millis / 1000 / 60 / 60 / 24 / 7; } public static long weeksToMillis(long weeks) { return weeks * 7 * 24 * 60 * 60 * 100; } /** * Jira-style duration parser. Supported duration strings are: * <ul> * <li>'S': milliseconds</li> * <li>'s': seconds</li> * <li>'m': minutes</li> * <li>'h': hours</li> * <li>'d': days</li> * </ul> * * Durations can be compound statements in any order as long as they are * separated by a ',' (comma). Eg. "1d,14h,3s" to get the millisecond * equivalent of one day, fourteen hours and 3 seconds. * * Numbers with no durations will be treated as raw millisecond values * * @param durationStr * @return the number of milliseconds representing the duration */ public static long millisFromDuration(String durationStr) { long total = 0; MultiplierToken mt; long dur; for (String val : Splitter.on(',').trimResults().omitEmptyStrings().split(durationStr)) { dur = Long.parseLong(CharMatcher.DIGIT.retainFrom(val)); mt = MultiplierToken.from(val.charAt(val.length() - 1)); total += (mt.multiplier * dur); } return total; } private enum MultiplierToken { MILSEC_TOKEN('S', 1L), SEC_TOKEN('s', 1000L), MIN_TOKEN('m', 60000L), HOUR_TOKEN('h', 3600000L), DAY_TOKEN('d', 86400000L); final char token; final long multiplier; MultiplierToken(char token, long multiplier) { this.token = token; this.multiplier = multiplier; } static MultiplierToken from(char c) { switch (c) { case 's': return SEC_TOKEN; case 'm': return MIN_TOKEN; case 'h': return HOUR_TOKEN; case 'd': return DAY_TOKEN; case 'S': return MILSEC_TOKEN; } if (CharMatcher.DIGIT.matches(c)) { return MILSEC_TOKEN; } throw new IllegalArgumentException("Duration token was not on of [S,s,m,h,d] but was " + c); } } }