初识Java(Java字符串-格式化字符串)

一、格式化字符串

    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
%tY4 位年份2018
%tj一年中的第几天 ( 001 ~ 366 )085
%tm月份03
%td一个月中的第几天 ( 01 ~ 31 )02
%ty2 位年份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() 方法也可以格式化时间。

时间格式化转化符
转换符说明示例
%tH2 位数字的 24 时制的小时(00~23)14
%tI2 位数字的 12 时制的小时(01~12)05
%tk2 位数字的 24 时制的小时(0~23)5
%tl2 位数字的 1 2时制的小时(1~12)10
%tM2 位数字的分钟(00~59)05
%tS2 位数字的秒数(00~60)12
%tL3 位数字的毫秒数(000~999)920
%tN9 位数字的微秒数(000000000~999999999)062000000
%tp指定语言环境下上午或下午标记下午(中文)、pm(英文)
%tz相对于 GMT RFC 82 格式的数字时区偏移量+0800
%tZ时区缩写形式的字符串CST
%ts1970-01-01 00:00:00 至现在经过的秒数1206426646
%tQ1970-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,多练,多想。

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值