一、格式化字符串

    String 类型的静态 format() 方法用于创建格式化的字符串。

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

    format :格式字符串

    args :格式字符串中由格式说明符引用的参数。如果还有格式说明符以外的参数,则忽略这些额外的参数。此参数的数目是可变的,可以为 0 。

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

    l :格式化过程中要应用的语言环境。如果 l 为 null ,则不进行本地化。

    format :格式字符串

    args :格式字符串中由格式说明符引用的参数。如果患有格式说明符以外的参数,则忽略这些额外的参数。此参数的数目是可变的,可以为 0 。

    1.1 日期与时间字符串格式化

    在应用程序中,经常需要显示日期和时间。如果想要输出满意的日期和时间,一般想要编写大量的代码来实现。 format() 方法通过给定的特殊转换符作为参数来实现对日期和时间的格式化。

    1.1.1 日期格式化

    返回一个月中的天数

Date date = new Date();                //创建 Date 对象 date
String s = String.format("%te",date);  //通过 foramt() 方法对 date 进行格式化

    s 的值是当前日期中的天数,如今天是1号,则 s 的值为 1 。

常用的日期格式化转化符

转换符

说明

示例

%te

一个月中的某一天( 1 ~ 31 )

2

%tb

指定语言环境的月份简称

Feb (英文) 、二月(中文)

%tB

指定语言环境的月份全称

February(英文)、二月(中文)

%tA

指定语言环境的星期几全称

Monday(英文)、星期一(中文)

%ta

指定语言环境的星期几简称

Mon(英文)、星期一(中文)

%tc

包括全部日期和时间信息

星期日  四月  01 21:33:22 CST 2018

%tY

4 位年份

2018

%tj

一年中的第几天 ( 001 ~ 366 )

085

%tm

月份

03

%td

一个月中的第几天 ( 01 ~ 31 )

02

%ty

2 位年份

08

import java.util.Date;                        //导入包
public class Eval{                            //创建类
    public static void main(String[] args){   //主方法
        Date date = new Date();               //创建 Date 对象
        //格式化 date 
        String year = String.format("%ty", 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 + "号");
    }
}

    运行结果为:

今年是: 2018 年
现在是: 四 月
今天是: 01 号

    1.1.2 时间格式化

    使用 format() 方法也可以格式化时间。

时间格式化转化符

转换符

说明

示例

%tH

2 位数字的 24 时制的小时(00~23)

14

%tI

2 位数字的 12 时制的小时(01~12)

05

%tk

2 位数字的 24 时制的小时(0~23)

5

%tl

2 位数字的 1 2时制的小时(1~12)

10

%tM

2 位数字的分钟(00~59)

05

%tS

2 位数字的秒数(00~60)

12

%tL

3 位数字的毫秒数(000~999)

920

%tN

9 位数字的微秒数(000000000~999999999)

062000000

%tp

指定语言环境下上午或下午标记

下午(中文)、pm(英文)

%tz

相对于 GMT RFC 82 格式的数字时区偏移量

+0800

%tZ

时区缩写形式的字符串

CST

%ts

1970-01-01 00:00:00 至现在经过的秒数

1206426646

%tQ

1970-01-01 00:00:00 至现在经过的毫秒数

1206426737453

import java.util.Date;                         //导包
public class GetDate{                          //创建类
    public static void mian(String[] args){    //主方法
        Date date = new Date();                //创建 Date 对象
        //对 date 进行格式化
        String hour = new String.format("%tH",date);
        String minute= new String.format("%tM",date);
        String second = new String.format("%tS",date);
        //输出信息
        System,out,println("现在是:"+huor+"时"+minute+"分"+second+"秒")
    }
}

    运行结果为: 现在是 21 时 57 分 50 秒


    1.3 格式化常见的日期时间组合

    格式化日期与时间的转换符定义了各种日期时间组合的格式。

常见的日期和时间组合的格式

转换符

说明

示例

%tF

" 年-月-日 " 格式(4 位年份)

2018-04-01

%tD

" 月/日/年 " 格式(2 位年份)

04/01/18

%tc

全部日期和时间信息

星期日 四月 01 22::04:00 CST 2018 

%tr

" 时:分:秒  PM ( AM )  "格式(12 时制)

03:22:06 下午

%tT

" 时:分:秒 " 格式(24 时制)

15:23:50

%tR

" 时:分 " 格式(24 时制)

15:25

import java.util.Date;
public class DateAndTime{
    public static void main(String[] args){
        Date date = new Date();                        //创建 Date 对象
        String time = String.format("%tc",date);       //格式化
        String form = String.format("%tF",date);
        System.out.println("全部的时间信息是:"+time);   //输出信息
        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,多练,多想。

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