Java格式化字符串

67 篇文章 16 订阅

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

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

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

format:格式字符串。

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

(2)public static String format(Locale l, String format, Object... args)

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

format:格式字符串。

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

示例:使用String类的静态format()方法格式化的字符串。

/**
 * 使用String类的静态format()方法格式化的字符串
 * @author pan_junbiao
 */
public class StrFormat
{
    public static void main(String[] args) {
        String msg1 = String.format("您好,欢迎访问 %s","pan_junbiao的博客");
        String msg2 = String.format("博客地址:%s","https://blog.csdn.net/pan_junbiao");
        // 输出信息
        System.out.println(msg1);
        System.out.println(msg2);
    }
}

执行结果:

 

1、日期和时间字符串格式化

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

1.1 日期格式化

使用format()方法对日期进行格式化时,会用到日期格式化转换符,常用的日期格式化转换符如下所示。

示例:在项目中创建Eval类,实现日期格式化。

import java.util.*;

/**
 * 日期格式化
 * @author pan_junbiao
 */
public class Eval {
    public static void main(String[] args) {
        Date date = new Date(); // 创建Date对象date
        String year = String.format("%tY", date); // 将date进行格式化
        String month = String.format("%tB", date);
        String day = String.format("%td", date);
        String msg = String.format("您好,欢迎访问 %s","pan_junbiao的博客");
        // 输出信息
        System.out.println(msg);
        System.out.println("今年是:" + year + "年");
        System.out.println("现在是:" + month);
        System.out.println("今天是:" + day + "号");
    }
}

执行结果:

1.2 时间格式化

使用format()方法对时间进行格式化时,会用到时间格式化转换符,时间格式化转换符要比日期转换符更多、更精确,它可以将时间格式化为时、分、秒、毫秒。常用的时间格式化转换符如下表所示。

示例:在项目中创建GetDate类,实现将时间格式化。

import java.util.*;

/**
 * 时间格式化
 * @author pan_junbiao
 */
public class GetDate {
    public static void main(String[] args) {
        Date date = new Date(); // 创建Date对象date
        String hour = String.format("%tH", date); // 将date进行格式化
        String minute = String.format("%tM", date);
        String second = String.format("%tS", date);
        String msg = String.format("您好,欢迎访问 %s","pan_junbiao的博客");
        // 输出的信息
        System.out.println(msg);
        System.out.println("现在是:" + hour + "时" + minute + "分" + second + "秒");
    }
}

执行结果:

 

2、常规类型格式化

在程序设计过程中,经常需要对常规类型的数据进行格式化,例如格式化为整数,格式化为科学计数表示等,在Java中可以使用常规类型的格式化转换符来实现,下表列出了常规类型的格式化转换符。

示例:在项目中创建General类,实现常规类型格式化。

/**
 * 时间格式化
 * @author pan_junbiao
 */
public class General {
    public static void main(String[] args) {
        String str = String.format("%d", 400 / 2); // 将结果以十进制显示
        String str2 = String.format("%b", 3 > 5); // 将结果以布尔形式显示
        String str3 = String.format("%x", 200); // 将结果以十进制格式显示
        String msg = String.format("您好,欢迎访问 %s","pan_junbiao的博客");
        // 输出格式化字符串
        System.out.println(msg);
        System.out.println("400的一半是:" + str);
        System.out.println("3>5正确吗:" + str2);
        System.out.println("200的十六进制数是:" + str3);
    }
}

执行结果:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pan_junbiao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值