Java Utililty Methods Milliseconds

List of utility methods to do Milliseconds

Description

The list of methods to do Milliseconds are organized into topic(s).

Method

longtruncateMillisecond(Date date)
truncate Millisecond
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = sdf.format(date);
Date finalDate = null;
try {
    finalDate = sdf.parse(dateStr);
} catch (ParseException e) {
    e.printStackTrace();
return finalDate.getTime();
booleantrySleepMillis(int millis)
try Sleep Millis
try {
    Thread.sleep(millis);
    return true;
} catch (Exception ex) {
return false;
intunixToDosTime(long timeMillis)
Converts a unix timestamp into a 32-bit DOS timestamp.
Calendar time = Calendar.getInstance();
time.setTimeInMillis(timeMillis);
if (!isValidInDos(timeMillis)) {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    throw new IllegalArgumentException(String.format(
            "%s is not representable in the DOS time" + " format. It must be in the range %s to %s",
            df.format(time.getTime()), df.format(new Date(DOS_EPOCH)), df.format(new Date(MAX_DOS_DATE))));
int dos = time.get(Calendar.SECOND) / 2;
dos |= time.get(Calendar.MINUTE) << DOS_MINUTE_OFFSET;
dos |= time.get(Calendar.HOUR_OF_DAY) << DOS_HOUR_OFFSET;
dos |= time.get(Calendar.DAY_OF_MONTH) << DOS_DAY_OFFSET;
dos |= (time.get(Calendar.MONTH) + 1) << DOS_MONTH_OFFSET;
dos |= (time.get(Calendar.YEAR) - 1980) << DOS_YEAR_OFFSET;
return dos;
voidwaitMillis(long millis)
wait Millis
try {
    Thread.sleep(millis);
} catch (Exception e) {
voidwaitMillis(long time)
Waits for the specified amount of time in milliseconds.
Object obj = new Object();
synchronized (obj) {
    try {
        obj.wait(time);
    } catch (Throwable t) {
voidwaitMilliSeconds(final long waitTimeInMilliseconds)
Wait milli seconds.
try {
    Thread.sleep(waitTimeInMilliseconds);
} catch (final InterruptedException e) {
voidwaitMilliSeconds(int ms)
Wraps Thread.sleep() and just try/catches the InterruptedException
try {
    Thread.sleep(ms);
} catch (InterruptedException e) {
    throw new RuntimeException(e);
longweeksToMillis(long weeks)
weeks To Millis
return weeks * 7 * 24 * 60 * 60 * 100;