本文整理汇总了Java中java.util.concurrent.TimeUnit.equals方法的典型用法代码示例。如果您正苦于以下问题:Java TimeUnit.equals方法的具体用法?Java TimeUnit.equals怎么用?Java TimeUnit.equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.TimeUnit
的用法示例。
在下文中一共展示了TimeUnit.equals方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: toStringAsLargestTimeUnit
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
/**
* Returns a string representation of this duration with the largest time unit this duration can be converted to where <code>length > 0</code>.
* Returns {@link #toString()} if no such time unit exists excluding this duration's time unit, or <code>length = 0</code>.
*
* @param length the length
* @param unit the unit
* @return a string representation of this duration with largest time unit this duration can be converted to where <code>length > 0</code>
*/
public static String toStringAsLargestTimeUnit(final long length, final TimeUnit unit) {
if (length > 0 && !unit.equals(TimeUnit.DAYS)) {
final TimeUnit[] units = TimeUnit.values();
int i = units.length - 1;
TimeUnit new_unit = units[i];
// Find the largest TimeUnit that can present this duration in a non-zero length. The unit is always bigger than or equal to the current unit.
while (!unit.equals(new_unit)) {
final long length_in_new_unit = new_unit.convert(length, unit);
if (length_in_new_unit != 0) {
return toString(length_in_new_unit, new_unit);
}
new_unit = units[i--];
}
}
return toString(length, unit);
}
示例2: validateToken
import java.util.concurrent.TimeUnit; //导入方法依赖的package包/类
public boolean validateToken(String token, Integer loginTime, TimeUnit timeUnit) {
try {
String key = jasypt.decrypt(token);
String [] keyParts = key.split("\\|");
String strDate = keyParts[keyParts.length - 1];
Date date = format.parse(strDate);
Date currentTime = new Date();
long duration = currentTime.getTime() - date.getTime();
long diff;
if (timeUnit.equals(TimeUnit.MINUTES))
diff = TimeUnit.MILLISECONDS.toMinutes(duration);
else if (timeUnit.equals(TimeUnit.SECONDS))
diff = TimeUnit.MILLISECONDS.toSeconds(duration);
else
throw new UnsupportedOperationException(timeUnit.toString() + " not supported");
if (diff > loginTime)
return false;
return true;
}
catch (Exception exc) {
exc.printStackTrace();
return false;
}
}