Introduction to Java (Java Strings - Format Strings)

1. Format string

    The static format() method of type String is used to create a formatted string.

    ( 1 )format ( String fromat , Object ... args )

    format : format string

    args : Arguments in the format string referenced by format specifiers. If there are arguments other than format specifiers, these 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 is done.

    format : format string

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

    1.1 Date and time string formatting

    In applications, it is often necessary to display the date and time. If you want to output a satisfactory date and time, you generally want to write a lot of code to achieve it. The format() method implements the formatting of date and time by giving a special conversion character as an argument.

    1.1.1 Date formatting

    Returns the number of days in a month

Date date = new Date(); //Create Date object date
String s = String.format("%te",date); //Format date by foramt() method

    The value of s is the number of days in the current date. If today is the 1st, the value of s is 1.

Common date formatting converters
converter illustrate Example
%at day of the month ( 1 ~ 31 ) 2
%tb the short name of the month for the specified locale Feb (English), February (Chinese)
%tB the full name of the month for the specified locale February (English), February (Chinese)
% tA the full name of the day of the week for the specified locale Monday (English), Monday (Chinese)
% ta Abbreviated day of the week for the specified locale Mon (English), Monday (Chinese)
%tc Include full date and time information Sunday April 01 21:33:22 CST 2018
%tY 4 digit year 2018
% i.e. Day of the year ( 001 ~ 366 ) 085
%tm month 03
%td Day of the month ( 01 ~ 31 ) 02
%ty 2-digit year 08
import java.util.Date; //import package
public class Eval{ //Create class
    public static void main(String[] args){ //Main method
        Date date = new Date(); //Create a Date object
        // format date
        String year = String.format("%ty", date);
        String month = String.format("%tB", date);
        String day = String.format("%td", date);
        // output information
        System.out.println("This year is:" + year + "year");
        System.out.println("Now is: " + month + "month");
        System.out.println("Today is:" + day + "Number");
    }
}

    The running result is:

This year is: 2018
It is now: April
Today is: 01

    1.1.2 Time Formatting

    The time can also be formatted using the format() method.

time format converter
converter illustrate Example
%tH 2-digit 24-hour hour (00~23) 14
% tI 2-digit 12-hour hour (01~12) 05
%tk 2-digit 24-hour hour (0~23) 5
%tl 2-digit 1 2-hour hour (1~12) 10
%tM 2-digit minute (00~59) 05
%tS 2-digit seconds (00~60) 12
%tL 3-digit milliseconds (000~999) 920
%tN 9-digit microseconds (000000000~999999999) 062000000
%tp AM or PM marker in the specified locale afternoon (Chinese), pm (English)
%tz Numeric timezone offset relative to GMT RFC 82 format +0800
%tZ a string of time zone abbreviations CST
%ts Number of seconds since 1970-01-01 00:00:00 1206426646
% tQ 1970-01-01 00:00:00 milliseconds since now 1206426737453
import java.util.Date; //import package
public class GetDate{ //Create class
    public static void mian(String[] args){ //main method
        Date date = new Date(); //Create a Date object
        //Format the date
        String hour = new String.format("%tH",date);
        String minute= new String.format("%tM",date);
        String second = new String.format("%tS",date);
        // output information
        System,out,println("Now is:"+huor+"hour"+minute+"minute"+second+"second")
    }
}
    The result of the operation is: It is now 21:57:50

    1.3 Formatting common date-time combinations

    Formatted date-to-time converters define the format of various date-time combinations.

Formats for common date and time combinations
converter illustrate Example
%tF "Year-Month-Day" format (4-digit year) 2018-04-01
%tD "month/day/year" format (2-digit year) 04/01/18
%tc Full date and time information Sunday April 01 22::04:00 CST 2018 
%tr "Hour:Minute:Second PM ( AM )" format (12-hour clock) 03:22:06 PM
%tT "hours:minutes:seconds" format (24-hour clock) 15:23:50
%tR "hour:minute" format (24-hour clock) 15:25
import java.util.Date;
public class DateAndTime{
    public static void main(String[] args){
        Date date = new Date(); //Create a Date object
        String time = String.format("%tc",date); //Format
        String form = String.format("%tF",date);
        System.out.println("All time information is: "+time); //Output information
        System.out.println("年-月-日 格式:"+form);
    }
}

    运行结果为:

    全部的时间信息是:星期日 四月 01 22:10:00 CST 2018

    年-月-日 格式:2018-04-01 

二、常规类型格式化

    常规类型格式化可应用于任何参数类型。

常规转换符
转换符 说明 示例
%b 、%B 结果被格式化为布尔类型 true
%h 、%H 结果被格式化为散列码 A05A5198
%s 、%S 结果被格式化为字符串类型 " abcd "
%c 、%C 结果被格式化为字符类型 ' a '
%d 结果被格式化为十进制整数 40
%o 结果被格式化为八进制整数 11
%x 、%X 结果被格式化为十六进制整数 4b1
%e 结果被格式化为用计算机科学记数法表示的十进制数 1.700000e+01
%a 结果被格式化为带有效位数和指数的十六进制浮点值 0X1.c00000000001P4
%n 结果为特定于平台的行分隔符  
%% 结果为面值 ‘ % ’ %
public class General{
    public static void main(String[] args){
        String str = String.format("%d",400/2);    //将结果以十进制格式显示
        String str2 = String.format("%b",3>5);     //将结果以 boolean 型显示
        String str3 = String.format("%x",200);     //将结果以十六进制格式显示
        System.out.println("400 的一半是:"+str);
        System.out.println("3 > 5 正确吗:"+str2);
        System.out.println("200 的十六进制数是:"+str3);
    }
}

运行结果为:

400 的一半是: 200
3 > 5 正确吗: false
200 的十六进制数是: c8

    对于学习Java,看书,看视频,看官方文档,看API,多练,多想。

    要时刻学习,更新很快,要学习新技术,好好加油,祝终成大神。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325727552&siteId=291194637