;-) I had just finished an example for you of pretty much the same thing but
included formatting to illustrate the funky (and ugly) things you can do with
formatting patterns. For posterity, here it is:

import java.text.*;

public class TimeTest {

    public static final long SECOND_MS = 1000;
    public static final long MINUTE_MS = (60 * SECOND_MS);
    public static final long HOUR_MS = (60 * MINUTE_MS);
    public static final long DAY_MS = (24 * HOUR_MS);

    public static final String FORMAT_PATTERN =
        "{0,choice,0#0days|1#1day|1<{0}days}" +
        "{1,choice,0#0hours|1#1hour|1<{1}hours}" +
        "{2,choice,0#0minutes|1#1minute|1<{2}minutes}" +
        "{3,choice,0#0seconds|1#1second|1<{3}seconds}" +
        "{4,choice,0#0milliseconds|1#1millisecond|1<{4}milliseconds}";

    public static String formatTime(long time) {
        time = Math.abs(time);

        long days = 0;
        long hours = 0;
        long minutes = 0;
        long seconds = 0;
        long ms = 0;

        days = time / DAY_MS;
        time %= DAY_MS;
        if (time > 0) {
            hours = time / HOUR_MS;
            time %= HOUR_MS;
            if (time > 0) {
                minutes = time / MINUTE_MS;
                time %= MINUTE_MS;
                if (time > 0) {
                    seconds = time / SECOND_MS;
                    ms = time % SECOND_MS;
                }
            }
        }

        Object[] formatArgs = { new Long(days),
                                new Long(hours),
                                new Long(minutes),
                                new Long(seconds),
                                new Long(ms) };
        return MessageFormat.format(FORMAT_PATTERN, formatArgs);
    }

    public static void main(String[] args) {
        long time = Long.parseLong(args[0]);
        System.out.println(formatTime(time));
    }
}


Quoting "Bailey, Shane C." <[EMAIL PROTECTED]>:

> 
> In case anyone is interested, here is a little utility I just wrote (and
> quickly tested so any feedback is fine):
> 
> 
> /**
>  * Get the number of days, hours, minutes, seconds and left over
> miliseconds
>  * from a milisecond based time format. This is good for getting the time
>  * difference between d2.getTime() - d1.getTime() for display purposes.
>  *
>  */
> public class TimeConverter
> {
>               private long time = 0l;
>               private static long MS_DAY = 86400000;
>               private static long MS_HR =   3600000;
>               private static long MS_MIN =    60000;
>               private static long MS_SEC =     1000;
> 
>               private long days = 0;
>               private long hours= 0;
>               private long mins = 0;
>               private long secs = 0;
>               private long loMilisecs = 0;
> 
>               public TimeConverter(long time)
>               {
>                       this.time = time;
> 
>                       long remainder = 0;
>                       if(time<MS_DAY)
>                       { 
>                               days = 0; 
>                               remainder = time;
>                       }
>                       else
>                       {
>                               days = time / MS_DAY;
>                               remainder = time % MS_DAY;
>                       }
> 
> 
>                       if(remainder<MS_HR)
>                       {
>                               hours = 0;
>                       }
>                       else
>                       {
>                               hours = remainder / MS_HR;
>                               remainder = remainder % MS_HR;
>                       }
> 
>                       if(remainder<MS_MIN)
>                       {
>                               mins = 0;
>                       }
>                       else
>                       {
>                               mins = remainder / MS_MIN;
>                               remainder = remainder % MS_MIN;
>                       }
> 
>                       if(remainder<MS_SEC)
>                       {
>                               secs = 0;
>                       }
>                       else
>                       {
>                               secs = remainder / MS_SEC;
>                               remainder = remainder % MS_SEC;
>                       }
> 
>                       loMilisecs = remainder;
> 
> 
> 
>               }
> 
> 
>               public int getDays()
>               {
>                       return (int)days;
>               }
> 
>               public int getHours()
>               {
>                       return (int)hours;
>               }
> 
>               public int getMinutes()
>               {
>                       return (int)mins;
>               }
> 
>               public int getSeconds()
>               {
>                       return (int)secs;
>               }
> 
>               public int getLeftOverMiliseconds()
>               {
>                       return (int)loMilisecs;
>               }
> }
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> -----Original Message-----
> From: Bailey, Shane C. [mailto:[EMAIL PROTECTED] 
> Sent: Wednesday, July 16, 2003 10:46 AM
> To: 'Struts Users Mailing List'
> Subject: [OT] Display Time was RE: How to use <bean:write format/formatKet>
> 
>           I need to display the time difference between two dates.  I may
> be
> making this harder than it seems.
> 
>           My first instinct is to do long timeDiff = date2.getTime() -
> date1.getTime()  but then how do I get this
> 
>           millisecond format to display as: "dddaysHHhrsmmminsssecSSms"
> meaning "2days12hrs13min23sec15ms",
> 
>           very confusing I think (how do I set the format string, I mean do
> I backslash "dd\daysHH\hrs" etc? how will
> 
>           the formatter know that the 's' in "days" doesn't mean to display
> seconds?
> 
>  
> 
>           Any suggestions would be great!
> 
>  
> 
>           TIA
> 
>  
> 
> -----Original Message-----
> From: Nagendra Kumar O V S [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, July 15, 2003 6:14 AM
> To: [EMAIL PROTECTED]
> Subject: Re: How to use <bean:write format/formatKet>
> 
>  
> 
> 
> hi,
> 
> u can use <bean:write  to format dates and numbers.
> 
> for ex. u have action form 
> 
> private Date date1;
> 
>  
> 
> if u want to format that using bean:write
> 
> <bean:write name="form" property="date1" format="MM/dd/yyyy"/>
> 
> this renders the date using that format(simpledate format)
> 
> if u want this format from application resouces , use formatkey which will
> get the value from the resources.
> 
>  
> 
>  
> 
> --nagi
> 
>  
> 
> -------Original Message-------
> 
>  
> 
> From: Struts Users Mailing List <mailto:[EMAIL PROTECTED]> 
> 
> Date: Tuesday, July 15, 2003 02:57:30 PM
> 
> To: [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> 
> 
> Subject: How to use 
> 
>  
> 
> Hi ,
> Can anybody let me know how to use "bean:write" for formatting the text.
> 
> What is the difference between format / formaKey in Bean:write attributes ?
> 
> What are all the valid message format texts that I can use in
> ApplicationResourse. 
> 
> Kishore Kumar K
> Covansys India Pvt Ltd
> *:- [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> 
> *:- 91-44-22628080-6877

-- 
Kris Schneider <mailto:[EMAIL PROTECTED]>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to