The easiest way to display a double in the local currency is to get an instance of
NumberFormat currency formatter and use its
format()
method. The following example ...
import java.text.NumberFormat;
public class CurrencyFormatExample {
public static void main(String[] argv) {
NumberFormat cf = NumberFormat.getCurrencyInstance();
double cost = 19.51;
System.out.println("Cost = " + cf.format(cost));
}
}
... produces ...
Cost = $19.51
Similarly, the easiest way to display a date is to get an instance of a
DateFormat date formatter and use its
format()
method. Choose different types of date formatters to produce shorter or longer output. The following example creates four different data formatters ...
import java.text.DateFormat;
import java.util.Date;
public class DateFormatExample {
public static void main(String[] argv) {
DateFormat dfShort = DateFormat.getDateInstance(DateFormat.SHORT);
DateFormat dfMedium = DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat dfLong = DateFormat.getDateInstance(DateFormat.LONG);
DateFormat dfFull = DateFormat.getDateInstance(DateFormat.FULL);
Date today = new Date();
System.out.println("Today is ...");
System.out.println(dfShort.format(today));
System.out.println(dfMedium.format(today));
System.out.println(dfLong.format(today));
System.out.println(dfFull.format(today));
}
}
... and produces ...
Today is ...
26/04/05
26/04/2005
26 April 2005
Tuesday, 26 April 2005