天天看点

Java学习之旅(十八):字符串之格式化字符串

String 类的静态 format() 方法用于创建格式化的字符串。format() 方法有两种重载形式:

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

该方法使用指定的格式字符串和参数返回一个格式化字符串,格式化后的新字符串使用本地默认的语言环境。语法为:

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

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

  • local:格式化过程中要应用的语言环境,如果 local 为 null,则不进行本地化
  • format:格式字符串
  • args:格式字符串中由格式说明符引用的参数。如果还有格式说明符以外的参数,则忽略这些额外的参数。此参数的数目是可变的,可以为0

日期和时间字符串格式化

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

日期格式化

常用的日期格式化转换符

转换符 说明 示例
%te 一个月中的某一天 15
%tb 指定语言环境的月份简称 Feb(英文)、二月(中文)
%tB 指定语言环境的月份全称 February(英文)、二月(中文)
%tA 指定语言环境的星期全称 Monday(英文)、星期一(中文)
%ta 指定语言环境的星期简称 Mon(英文)、星期一(中文)
%tc 包括全部日期和时间信息 星期二 三月 25 14:22:26:33 CST 2020
%tY 4位年份 2020
%tj 一年中的第几天(001~366) 200
%tm 月份(01~12) 03
%td 一个月中的第几天(01~31) 25
%ty 2位年份 20
import java.util.Date;

public class test {

    public static void main(String[] args) {

        Date date = new Date();
        String year = String.format("%tY",date);
        String month = String.format("%tm",date);
        String day = String.format("%td",date);
        System.out.println("今天是" + year + "年" + month + "月" + day + "日");
        String dayOfYear = String.format("%tj",date);
        System.out.println("今天是" + year + "年的第" + dayOfYear + "天");

    }

}
           
Java学习之旅(十八):字符串之格式化字符串

时间格式化

使用 format() 方法不仅可以完成日期的格式化操作,也可以实现时间的格式化。时间格式化转换符要比日期转换符更多、更精确,它可以将时间格式化为时、分、秒、毫秒。

时间格式转换符

转换符 说明 示例
%tH 2位数字的24小时制的小时(00~23) 05
%tI(小写t,大写i) 2位数字的12小时制的小时(01~12) 05
%tk 2位数字的24小时制的小时(0~23) 5
%tl(小写t,小写L) 2位数字的12小时制的小时(1~12) 5
%tM 2位数字的分钟(00~59) 05
%tS 2位数字的秒(00~60) 15
%tL 3位数字的毫秒(000~999) 123
%tN 9位数字的微秒(000000000~999999999) 123456789
%tp 指定语言环境下的上下午标记 pm、下午
%tz 相对于 GMT RFC 82 格式的数字时区偏移量 +0800
%tZ 时区缩写形式的字符串 CST
%ts 1970-01-01 00:00:00 至现在的秒数 1234567890
%tQ 1970-01-01 00:00:00 至现在的毫秒数 1234567891011
import java.util.Date;

public class test {

    public static void main(String[] args) {

        Date date = new Date();
        System.out.println("当前时间:" + date);
        String hour = String.format("%tH",date);
        String minute = String.format("%tM",date);
        String second = String.format("%tS",date);
        System.out.println("现在是北京时间:" + hour + "时" + minute + "分" + second + "秒");
        String secondsCount = String.format("%ts",date);
        System.out.println("距1970-01-01已有" + secondsCount + "秒");

    }

}
           
Java学习之旅(十八):字符串之格式化字符串

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

格式化日期与时间的转换符定义了各种时间组合的格式,其中最常见的有以下几种:

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

转换符 说明 示例
%tF “年-月-日”格式(4位数字年份) 2020-10-10
%tD “月/日/年”格式(2位数字年份) 10/10/20
%tc 全部日期和时间信息 星期六 十一月 07 10:20:23 CST 2020
%tr “时:分:秒 PM”格式(12小时制) 02:22:36 下午
%tT “时:分:秒”格式(24小时制) 14:22:36
%tR “时:分”格式(24小时制) 14:22
import java.util.Date;

public class test {

    public static void main(String[] args) {

        Date date = new Date();
        System.out.println("当前时间:" + String.format("%tc", date));
        System.out.println("当前时间(年月日):" + String.format("%tF", date));

    }

}
           
Java学习之旅(十八):字符串之格式化字符串

常规类型格式化

常规类型的格式化可以应用于任何参数类型:

常规转换符

转换符 说明 示例
%b、%B 格式化为布尔类型 true
%h、%H 格式化为散列码 A05A5198
%s、%S 格式化为字符串类型 "ABCD"
%c、%C 格式化为字符类型 'a'
%d 格式化为十进制整数 40
%o 格式化为八进制整数 47
%x、%X 格式化为十六进制整数 2B4
%e 格式化为用计算机科学计数法表示的十进制数 1.700000e+01
%a 格式化为带有效位数和指数的十六进制浮点值 0X1.C000000000001P4
%n 格式化为特定于平台的行分隔符
%% 格式化为字面值'%' %
public class test {

    public static void main(String[] args) {

        int number = 1024;
        System.out.println("number = " + number);
        System.out.println("转换为十进制为:" + String.format("%d", number));
        System.out.println("转换为八进制为:" + String.format("%o", number));
        System.out.println("转换为十六进制为:" + String.format("%x", number));

    }

}
           
Java学习之旅(十八):字符串之格式化字符串