import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.Locale;
public class TestFormat {
public static void main(String[] args) {
// The "0" symbol shows a digit or 0 if no digit present
NumberFormat formatter = new DecimalFormat("000000");
String s = formatter.format(-1234.567); // -001235
System.out.println(s);
// notice that the number was rounded up
// The "#" symbol shows a digit or nothing if no digit present
formatter = new DecimalFormat("##");
s = formatter.format(-1234.567); // -1235
System.out.println(s);
s = formatter.format(0); // 0
System.out.println(s);
formatter = new DecimalFormat("##00");
s = formatter.format(0); // 00
System.out.println(s);
// The "." symbol indicates the decimal point
formatter = new DecimalFormat(".00");
s = formatter.format(-.567); // -.57
System.out.println(s);
formatter = new DecimalFormat("0.00");
s = formatter.format(-.567); // -0.57
System.out.println(s);
formatter = new DecimalFormat("#.#");
s = formatter.format(-1234.567); // -1234.6
System.out.println(s);
formatter = new DecimalFormat("#.######");
s = formatter.format(-1234.567); // -1234.567
System.out.println(s);
formatter = new DecimalFormat(".######");
s = formatter.format(-1234.567); // -1234.567
System.out.println(s);
formatter = new DecimalFormat("#.000000");
s = formatter.format(-1234.567); // -1234.567000
System.out.println(s);
// The "," symbol is used to group numbers
formatter = new DecimalFormat("#,###,###");
s = formatter.format(-1234.567); // -1,235
System.out.println(s);
s = formatter.format(-1234567.890); // -1,234,568
System.out.println(s);
// The ";" symbol is used to specify an alternate pattern for negative values
formatter = new DecimalFormat("#;(#)");
s = formatter.format(-1234.567); // (1235)
System.out.println(s);
// The ' symbol is used to quote literal symbols
formatter = new DecimalFormat("'#'#");
s = formatter.format(-1234.567); // -#1235
System.out.println(s);
formatter = new DecimalFormat("'abc'#");
s = formatter.format(-1234.567); // -abc1235
System.out.println(s);
System.out.println("-------using LOCALE-------");
//with locale
{
Locale ptBR = new Locale("pt", "BR");
DateFormat dateFormat =
DateFormat.getDateInstance(DateFormat.FULL, ptBR);
System.out.println(dateFormat.format(new Date()));
DateFormat dateFormat2 =
DateFormat.getDateInstance(DateFormat.MEDIUM, ptBR);
System.out.println(dateFormat2.format(new Date()));
DateFormat dateFormat3 =
DateFormat.getDateInstance(DateFormat.SHORT, ptBR);
System.out.println(dateFormat3.format(new Date()));
DateFormat timeFormat =
DateFormat.getTimeInstance(DateFormat.SHORT, ptBR);
System.out.println(timeFormat.format(new Date()));
DateFormat timeFormat2 =
DateFormat.getTimeInstance(DateFormat.MEDIUM, ptBR);
System.out.println(timeFormat2.format(new Date()));
DateFormat timeFormat3 =
DateFormat.getTimeInstance(DateFormat.FULL, ptBR);
System.out.println(timeFormat3.format(new Date()));
NumberFormat numberFormat =
NumberFormat.getNumberInstance(ptBR); //for numbers
System.out.println(numberFormat.format(13.23));
NumberFormat moedaFormat =
NumberFormat.getCurrencyInstance(ptBR); //for currencies
System.out.println(moedaFormat.format(13.23));
}
{
Locale ptBR = new Locale("es", "AR");
DateFormat dateFormat =
DateFormat.getDateInstance(DateFormat.FULL, ptBR);
System.out.println(dateFormat.format(new Date()));
DateFormat dateFormat2 =
DateFormat.getDateInstance(DateFormat.MEDIUM, ptBR);
System.out.println(dateFormat2.format(new Date()));
DateFormat dateFormat3 =
DateFormat.getDateInstance(DateFormat.SHORT, ptBR);
System.out.println(dateFormat3.format(new Date()));
DateFormat timeFormat =
DateFormat.getTimeInstance(DateFormat.SHORT, ptBR);
System.out.println(timeFormat.format(new Date()));
DateFormat timeFormat2 =
DateFormat.getTimeInstance(DateFormat.MEDIUM, ptBR);
System.out.println(timeFormat2.format(new Date()));
DateFormat timeFormat3 =
DateFormat.getTimeInstance(DateFormat.FULL, ptBR);
System.out.println(timeFormat3.format(new Date()));
NumberFormat numberFormat =
NumberFormat.getNumberInstance(ptBR); //for numbers
System.out.println(numberFormat.format(13.23));
NumberFormat moedaFormat =
NumberFormat.getCurrencyInstance(ptBR); //for currencies
System.out.println(moedaFormat.format(13.23));
}
{
Locale ptBR = new Locale("en", "US");
DateFormat dateFormat =
DateFormat.getDateInstance(DateFormat.FULL, ptBR);
System.out.println(dateFormat.format(new Date()));
DateFormat dateFormat2 =
DateFormat.getDateInstance(DateFormat.MEDIUM, ptBR);
System.out.println(dateFormat2.format(new Date()));
DateFormat dateFormat3 =
DateFormat.getDateInstance(DateFormat.SHORT, ptBR);
System.out.println(dateFormat3.format(new Date()));
DateFormat timeFormat =
DateFormat.getTimeInstance(DateFormat.SHORT, ptBR);
System.out.println(timeFormat.format(new Date()));
DateFormat timeFormat2 =
DateFormat.getTimeInstance(DateFormat.MEDIUM, ptBR);
System.out.println(timeFormat2.format(new Date()));
DateFormat timeFormat3 =
DateFormat.getTimeInstance(DateFormat.FULL, ptBR);
System.out.println(timeFormat3.format(new Date()));
NumberFormat numberFormat =
NumberFormat.getNumberInstance(ptBR); //for numbers
System.out.println(numberFormat.format(13.23));
NumberFormat moedaFormat =
NumberFormat.getCurrencyInstance(ptBR); //for currencies
System.out.println(moedaFormat.format(13.23));
}
}
}
enjoy it! (many thanks Marcelo Torres)
Nenhum comentário:
Postar um comentário