Java format() method: format string

Grammar 1

format(String format,Object……args)

Example

String str = String.format("%d",400/2);
String str2 = String.format("%b",3>5);

Grammar 2

format(Locale l,String format,Object……args)

Typical application

public static void main(String[] args){
    Date date=new Date();//定义Date类对象
    Locale form=Locale.US;
    String year=String.format(form,"%tY",date);//将当前年份进行格式化
    String month=String.format(form,"%tB",date);//将当前月份进行格式化
    String day=String.format(form,"%td",date);//将当前日期进行格式化
    System.out.println("今年是:"+year+"年");//将格式化后的日期输出
    System.out.println("现在是:"+month);
    System.out.println("今天是:"+day+"号");
}

The operation results are as follows:

This year is: 2011 now is: March today is: the 22nd

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>