14. Format string

Table of contents

1. Create a format string

(1)format(String format,Object…args)

(2)format(Local l,String format,Object…args)

Second, date and time string formatting

1. Date formatting

2. Time formatting

3. Conventional type formatting


1. Create a format string

The static format() method of the String class is used to create formatted strings. The format() method has two overloaded forms.

(1)format(String format,Object…args)

This method returns a formatted string using the specified format string and parameters, and the formatted new string uses the local default locale.

The syntax is as follows:

3e47c0a135ef4d0e81cd4c81b6dfe0bc.png

  • format: format string.
  • args: Arguments referenced by format specifiers in the format string. If there are arguments other than format specifiers, those extra arguments are ignored. The number of this parameter is variable and can be 0.

(2)format(Local l,String format,Object…args)

  • l: The locale to apply during formatting. If l is null, no localization takes place.
  • format: format string.
  • args: Arguments referenced by format specifiers in the format string. If there are arguments other than format specifiers, those extra arguments are ignored. The number of this parameter is variable and can be 0.

Second, date and time string formatting

In application design, it is often necessary to display time and date. If you want to output a satisfactory date and time format, you generally need to write a lot of code and go through various algorithms to achieve it. The format() method uses the given special conversion character as a parameter to format the date and time.

1. Date formatting

Commonly used date formatting conversion characters such as:

c523b1773e084022b34554dc1d85d4f2.png

Example 1 : Create class Eval_1 in the project to output the current date information in the form of 4-digit year, full name of month, and 2-digit date

import java.util.Date;  //导入java.util.Date类

public class Eval_1 {
       public static void main(String[]args) {  //主方法
              Date date=new Date();  //创建Date对象date
              String year=String.format("%tY", date);  //将date格式化
              String month=String.format("%tB", date);
              String day=String.format("%td", date);
              System.out.println("今年是:"+year+"年");  //输出结果
              System.out.println("现在是:"+month);
              System.out.println("今天是:"+day+"号");

       }
}

operation result:

8c31aaf054174de391e42a07f4172caf.png

2. Time formatting

Using the format() method can not only complete the formatting of the date, but also the formatting of the time. There are more and more precise time formatting conversion characters than date conversion characters. It can format time into hours, minutes, seconds, and milliseconds. The conversion characters for formatting time are as follows:

4b344a005cb14cb596b6422f3130b645.png

Example 2 : Create a class GetDate in the project to output the current time information in the form of 2-digit hours, 2-digit minutes, and 2-digit seconds

import java.util.Date;  //导入java.util.Date类

public class GetDate {  //创建类
       public static void main(String[]args) {  //主方法
              Date date=new Date();   //创建Date对象date
              String hour=String.format("%tH", date);  //将date格式化
              String minute=String.format("%tM", date);
              String second=String.format("%tS", date);
              //输出结果
              System.out.println("现在是:"+hour+"时"
                            +minute+"分"+second+"秒");

       }
}

operation result:

1f30e02237134d16a02659a40484664d.png

3. Conventional type formatting

Formatting of conventional types can be applied to any parameter type, and the common conversion operators implemented by it are:

e618a0acd238456d889d7b2a20d00b3e.png

Example 3 : Create a class General in the project, and realize the conversion of different data types to strings in the main method

public class General {  //创建类

       public static void main(String[]args) {  //主方法
              String s=String.format("%d", 400/2);  //将结果以十进制格式显示
              String s1=String.format("%b", 2>5);  //将结果以boolean型显示
              String s2=String.format("%x", 200);  //将结果以十六进制格式显示
              System.out.println("400的一半是:"+s);  //输出结果
              System.out.println("2>5正确?:"+s1);
              System.out.println("200的十六进制是:"+s2);

       }
}

operation result:

4bb00948327f4e6cae9795439b1ff40a.png

 

Guess you like

Origin blog.csdn.net/qq_25990967/article/details/128777234