The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.text  [26 examples] > Messsages  [3 examples]

e325. Formatting a Message Containing a Time

    Object[] params = new Object[]{new Date(), new Date(0)};
    String msg = MessageFormat.format("The time is {0} and UTC of 0 is {1}", params);
    // The time is 2/27/02 2:08 PM and UTC of 0 is 12/31/69 4:00 PM
    
    msg = MessageFormat.format("The time is {0,time} and UTC of 0 is {1,time}", params);
    // The time is 2:08:48 PM and UTC of 0 is 4:00:00 PM
    
    msg = MessageFormat.format("The time is {0,time,short} and UTC of 0 is {1,time,short}", params);
    // The time is 2:08 PM and UTC of 0 is 4:00 PM
    
    msg = MessageFormat.format("The time is {0,time,medium} and UTC of 0 is {1,time,medium}", params);
    // The time is 2:08:48 PM and UTC of 0 is 4:00:00 PM
    
    msg = MessageFormat.format("The time is {0,time,long} and UTC of 0 is {1,time,long}", params);
    // The time is 2:08:48 PM PST and UTC of 0 is 4:00:00 PM PST
    
    msg = MessageFormat.format("The time is {0,time,full} and UTC of 0 is {1,time,full}", params);
    // The time is 2:08:48 PM PST and UTC of 0 is 4:00:00 PM PST
    
    // Use a custom format; see e316 Formatting the Time Using a Custom Format
    msg = MessageFormat.format("The time is {0,time,HH-mm-ss} and UTC of 0 is {1,time,HH-mm-ss}", params);
    // The time is 14-11-41 and UTC of 0 is 16-00-00

 Related Examples
e324. Formatting a Message Containing a Number
e326. Formatting a Message Containing a Date

See also: Dates    Numbers    Times    Words and Sentences   


© 2002 Addison-Wesley.