Time.java
/*******************************************************************************
* Copyright (c) 2004, 2013 Steve Flasby
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* <ul>
* <li>Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.</li>
* <li>Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.</li>
* </ul>
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package org.flasby.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public final class Time {
public static final Time TIME = new Time();
private Time() {
}
public static String estimate(long tInMillis) {
SimpleDateFormat sdf1 = null;
Date d = new Date(tInMillis);
if (tInMillis >= 3600000) {
sdf1 = new SimpleDateFormat("h 'hour(s)' m 'minute(s)' ss 'seconds'");
} else if (tInMillis < 60000) {
sdf1 = new SimpleDateFormat("ss 'seconds'");
} else if (tInMillis < 120000) {
if ((tInMillis / 1000) % 60 == 0) {
sdf1 = new SimpleDateFormat("m 'minute'");
} else {
sdf1 = new SimpleDateFormat("m 'minute' ss 'seconds'");
}
} else {
if ((tInMillis / 1000) % 60 == 0) {
sdf1 = new SimpleDateFormat("m 'minutes'");
} else {
sdf1 = new SimpleDateFormat("m 'minutes' ss 'seconds'");
}
}
// Need to set 0 offset in the time zone...
sdf1.setTimeZone(TimeZone.getTimeZone("GMT"));
return sdf1.format(d);
}
public static String toSimpleTimeUTC(long time) {
SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormatGmt.format(new Date(time));
}
}