The Java Developers Almanac 1.4


Order this book from Amazon.

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

e326. Formatting a Message Containing a Date

    Object[] params = new Object[]{new Date(), new Date(0)};
    String msg = MessageFormat.format("Today is {0} and UTC of 0 is {1}", params);
    // Today is 2/27/02 2:00 PM and UTC of 0 is 12/31/69 4:00 PM
    
    msg = MessageFormat.format("Today is {0,date} and UTC of 0 is {1,date}", params);
    // Today is Feb 27, 2002 and UTC of 0 is Dec 31, 1969
    
    msg = MessageFormat.format("Today is {0,date,short} and UTC of 0 is {1,date,short}", params);
    // Today is 2/27/02 and UTC of 0 is 12/31/69
    
    msg = MessageFormat.format("Today is {0,date,medium} and UTC of 0 is {1,date,medium}", params);
    // Today is Feb 27, 2002 and UTC of 0 is Dec 31, 1969
    
    msg = MessageFormat.format("Today is {0,date,long} and UTC of 0 is {1,date,long}", params);
    // Today is February 27, 2002 and UTC of 0 is December 31, 1969
    
    msg = MessageFormat.format("Today is {0,date,full} and UTC of 0 is {1,date,full}", params);
    // Today is Wednesday, February 27, 2002 and UTC of 0 is Wednesday, December 31, 1969
    
    // Use a custom format; see e320 Formatting a Date Using a Custom Format
    msg = MessageFormat.format("Today is {0,date,MMM d} and UTC of 0 is {1,date,MMM d}", params);
    // Today is Feb 27 and UTC of 0 is Dec 31

 Related Examples
e324. Formatting a Message Containing a Number
e325. Formatting a Message Containing a Time

See also: Dates    Numbers    Times    Words and Sentences   


© 2002 Addison-Wesley.