// conceptGO Open Source - www.conceptGO.com
// copyright (c) 2002, conceptGO
//
// This software is licensed under the
// conceptGO Open Source License, available
// at http://www.conceptgo.com/cgooslicense.html
//
/**
* The OS_MillisConverter class converts a
* millisecond value to a value expressed in
* weeks, days, hours, minutes, seconds and
* milliseconds. It also stores Total days and
* the original millisecond value. Each of these
* values can be retrieved.
*
* Formatted output can be requested in weeks and
* days or in days only. toString() returns the
* value in weeks and days, plus the total days
* and original milliseconds value.
*
* The class is mutable: after creation
* setMillis( millisValue ) can be invoked to
* store a new value.
*
* @author Joe Sam Shirah
*
*/
public class OS_MillisConverter
{
private final int MILLIS_IN_A_SECOND = 1000,
SECONDS_IN_A_MINUTE = 60,
MINUTES_IN_AN_HOUR = 60,
HOURS_IN_A_DAY = 24,
DAYS_IN_A_WEEK = 7;
private int iMillis,
iSeconds,
iMinutes,
iHours,
iDays,
iTotalDays,
iWeeks;
private long iOriginalMillis;
/**
* The constructor.
*
* @param millisValue The millisecond value
* to be converted.
*
* @see #setMillis( long millisValue )
*/
public OS_MillisConverter( long millisValue )
{
setMillis( millisValue );
} // end constructor
/**
* Returns a String formatted as days, hours,
* minutes, seconds, and milliseconds.
*
*
* @see #formatEndString( boolean b )
*/
public String formatToDaysOnly()
{
boolean b = ( iTotalDays != 0 );
StringBuffer sb = new StringBuffer( 70 );
if( b )
{
sb.append( "Days: ");
sb.append( iTotalDays );
sb.append( ", ");
}
sb.append( formatEndString( b ) );
return new String( sb );
} // end formatToDaysOnly
/**
* Returns a String formatted as weeks, days,
* hours, minutes, seconds, and milliseconds.
*
*
* @see #formatEndString( boolean b )
*/
public String formatToWeeksAndDays()
{
boolean b = ( iWeeks != 0 );
StringBuffer sb = new StringBuffer( 80 );
if( b )
{
sb.append( "Weeks: ");
sb.append( iWeeks );
sb.append( ", ");
}
b = b ? b : ( iDays != 0 );
if( b )
{
sb.append( "Days: ");
sb.append( iDays );
sb.append( ", ");
}
sb.append( formatEndString( b ) );
return new String( sb );
} // end formatToWeeksAndDays
/**
* Returns a StringBuffer formatted with the
* hours, minutes, seconds, and milliseconds
* value.
*
* @param bPrintRemaining If this value is true,
* all units ( hours, minutes, etc. ) will be
* returned, even if they are zero. If false,
* only units from the highest with a value
* will be returned. For example, false with a
* value of 45 seconds, and 212
* milliseconds will return exactly that. A
* value of true would also return 0 hours.amd
* zero minutes. This is done to accomodate
* formatToWeeksAndDays() and formatToDaysOnly(),
* but also means that the value excluding weeks
* and/or days can be requested.
*/
public StringBuffer formatEndString(
boolean bPrintRemaining )
{
StringBuffer sb = new StringBuffer( 50 );
bPrintRemaining = bPrintRemaining ?
bPrintRemaining : ( iHours != 0 );
if( bPrintRemaining )
{
sb.append( "Hours: ");
sb.append( iHours );
sb.append( ", ");
}
bPrintRemaining = bPrintRemaining ?
bPrintRemaining : ( iMinutes != 0 );
if( bPrintRemaining )
{
sb.append( "Minutes: ");
sb.append( iMinutes );
sb.append( ", ");
}
bPrintRemaining = bPrintRemaining ?
bPrintRemaining : ( iSeconds != 0 );
if( bPrintRemaining )
{
sb.append( "Seconds: ");
sb.append( iSeconds );
sb.append( ", ");
}
bPrintRemaining = bPrintRemaining ?
bPrintRemaining : ( iMillis != 0 );
if( bPrintRemaining )
{
sb.append( "Millis: ");
sb.append( iMillis );
}
return sb;
} // end formatEndString
/**
* Returns the milliseconds value remaining
* from second, minute, hour, etc calculation
* of the original value sent to the onject.
*
*
* @see #getOriginalMillis()
*/
public int getMillis()
{
return iMillis;
} // end getMillis
/**
* Returns the seconds value.
*/
public int getSeconds()
{
return iSeconds;
} // end getSeconds
/**
* Returns the minutes value.
*/
public int getMinutes()
{
return iMinutes;
} // end getMinutes
/**
* Returns the hours value.
*/
public int getHours()
{
return iHours;
} // end getHours
/**
* Returns the days value.remaining from
* the week calculation.
*
* @see #getTotalDays()
*/
public int getDays()
{
return iDays;
} // end getDays
/**
* Returns the total days value.without
* the week calculation.
*
* @see #getDays()
*/
public int getTotalDays()
{
return iTotalDays;
} // end getTotalDays
/**
* Returns the weeks value.
*/
public int getWeeks()
{
return iWeeks;
} // end getWeeks
/**
* Returns the original millisecond value
* sent to the object.
*/
public long getOriginalMillis()
{
return iOriginalMillis;
} // end getOriginalMillis
/**
* The workhorse method that accepts the
* original milliseconds and calculates
* all values.
*/
public void setMillis( long millisValue )
{
iSeconds = iMinutes = iHours =
iDays =iTotalDays = iWeeks = 0;
iOriginalMillis = millisValue;
iMillis = (int)(millisValue % MILLIS_IN_A_SECOND);
millisValue -= iMillis;
if( millisValue == 0 ) { return; }
millisValue /= MILLIS_IN_A_SECOND;
iSeconds = (int)(millisValue % SECONDS_IN_A_MINUTE);
millisValue -= iSeconds;
if( millisValue == 0 ) { return; }
millisValue /= SECONDS_IN_A_MINUTE;
iMinutes = (int)(millisValue % MINUTES_IN_AN_HOUR);
millisValue -= iMinutes;
if( millisValue == 0 ) { return; }
millisValue /= MINUTES_IN_AN_HOUR;
iHours = (int)(millisValue % HOURS_IN_A_DAY);
millisValue -= iHours;
if( millisValue == 0 ) { return; }
millisValue /= HOURS_IN_A_DAY;
iTotalDays = (int)millisValue;
iDays = (int)(millisValue % DAYS_IN_A_WEEK);
millisValue -= iDays;
if( millisValue == 0 ) { return; }
iWeeks = (int)(millisValue / DAYS_IN_A_WEEK);
} // end setMillis
/**
* Returns the values as formatted by
* formatToWeeksAndDays() plus the Total Days
* and Original Milliseconds values.
*/
public String toString()
{
return formatToWeeksAndDays() +
" - TotalDays: " + iTotalDays +
", OriginalMillis: " + iOriginalMillis;
} // end toString
/**
* Exercises the class by invoking
* formatToDaysOnly(), formatToWeeksAndDays(),
* and toStirng(). main expects a valid
* millisecond value. As a demo, it does no
* checking and will blow up if a non-numeric
* value is sent. To see the entire formatting
* capability, send a value larger than
* 604,800,000, which equals 1 week.
*/
public static void main( String[] args )
{
long lMillis = Long.parseLong( args[0] );
OS_MillisConverter mc =
new OS_MillisConverter( lMillis );
System.out.println( mc.formatToDaysOnly() );
System.out.println( mc.formatToWeeksAndDays() );
System.out.println( mc );
}// end main
} // end class OS_MillisConverter
Copyright © 2000-2002, conceptGO