package com.mindprod.example;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import static java.lang.System.*;
/**
* Calculations in milliseconds.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2012-03-23 initial version.
* @since 2012-03-23
*/
public final class TestMillis
{
private static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000L;
private static final long MILLIS_PER_HOUR = 60 * 60 * 1000L;
private static final long MILLIS_PER_MINUTE = 60 * 1000L;
private static final long MILLIS_PER_MONTH = ( long ) ( 30.4375 * 24 * 60 * 60 * 1000L );
private static final long MILLIS_PER_SECOND = 1000L;
private static final long MILLIS_PER_YEAR = ( long ) ( 365.25d * 24 * 60 * 60 * 1000L );
private static final DecimalFormat DF = new DecimalFormat( "###,##0" );
/**
* Modified Zulu format mask 2008-06-22T07:57+46.438
*/
private static final SimpleDateFormat SDF = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm'+'ss'.'SSS" );
/**
* Calculations in milliseconds
*
* @param args not used
*/
public static void main( String[] args )
{
long now = System.currentTimeMillis();
out.println( SDF.format( new Date( now ) ) );
out.println( "----- using static final constants -----" );
out.println( "millis/second:"
+ DF.format( MILLIS_PER_SECOND ) );
out.println( "millis/minute:"
+ DF.format( MILLIS_PER_MINUTE ) );
out.println( "millis/hour:"
+ DF.format( MILLIS_PER_HOUR ) );
out.println( "millis/day:"
+ DF.format( MILLIS_PER_DAY ) );
out.println( "millis/month:"
+ DF.format( MILLIS_PER_MONTH ) );
out.println( "millis/year:"
+ DF.format( MILLIS_PER_YEAR ) );
out.println( "millis that can fit in a long:"
+ DF.format( Long.MAX_VALUE )
+ " i.e. "
+ DF.format( Long.MAX_VALUE / MILLIS_PER_YEAR )
+ " years worth" );
out.println( "millis that can fit in an int:"
+ DF.format( Integer.MAX_VALUE )
+ " i.e. "
+ DF.format( Integer.MAX_VALUE / MILLIS_PER_DAY )
+ " days worth" );
out.println( "----- using java.util.concurrent.TimeUnit -----" );
out.println( "millis/second:"
+ DF.format( TimeUnit.SECONDS.toMillis( 1 ) ) );
out.println( "millis/minute:"
+ DF.format( TimeUnit.MINUTES.toMillis( 1 ) ) );
out.println( "millis/hour:"
+ DF.format( TimeUnit.HOURS.toMillis( 1 ) ) );
out.println( "millis/day:"
+ DF.format( TimeUnit.DAYS.toMillis( 1 ) ) );
}
}