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
%tY4位年份2020
%tj一年中的第几天(001~366)200
%tm月份(01~12)03
%td一个月中的第几天(01~31)25
%ty2位年份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 + "天");

    }

}

时间格式化

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

时间格式转换符
转换符说明示例
%tH2位数字的24小时制的小时(00~23)05
%tI(小写t,大写i)2位数字的12小时制的小时(01~12)05
%tk2位数字的24小时制的小时(0~23)5
%tl(小写t,小写L)2位数字的12小时制的小时(1~12)5
%tM2位数字的分钟(00~59)05
%tS2位数字的秒(00~60)15
%tL3位数字的毫秒(000~999)123
%tN9位数字的微秒(000000000~999999999)123456789
%tp指定语言环境下的上下午标记pm、下午
%tz相对于 GMT RFC 82 格式的数字时区偏移量+0800
%tZ时区缩写形式的字符串CST
%ts1970-01-01 00:00:00 至现在的秒数1234567890
%tQ1970-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 + "秒");

    }

}

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

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

常见的日期时间组合的格式
转换符说明示例
%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));

    }

}

常规类型格式化

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

常规转换符
转换符说明示例
%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));

    }

}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值