`

Java 字符串格式化

    博客分类:
  • j2se
 
阅读更多
JDK: java.util.Formatter.Conversion 类:
private static class Conversion {
        // Byte, Short, Integer, Long, BigInteger
        // (and associated primitives due to autoboxing)
	static final char DECIMAL_INTEGER     = 'd';
	static final char OCTAL_INTEGER       = 'o';
	static final char HEXADECIMAL_INTEGER = 'x';
	static final char HEXADECIMAL_INTEGER_UPPER = 'X';

        // Float, Double, BigDecimal
        // (and associated primitives due to autoboxing)
	static final char SCIENTIFIC          = 'e';
	static final char SCIENTIFIC_UPPER    = 'E';
	static final char GENERAL             = 'g';
	static final char GENERAL_UPPER       = 'G';
	static final char DECIMAL_FLOAT       = 'f';
	static final char HEXADECIMAL_FLOAT   = 'a';
	static final char HEXADECIMAL_FLOAT_UPPER = 'A';

        // Character, Byte, Short, Integer
        // (and associated primitives due to autoboxing)
	static final char CHARACTER           = 'c';
	static final char CHARACTER_UPPER     = 'C';

        // java.util.Date, java.util.Calendar, long
	static final char DATE_TIME           = 't';
	static final char DATE_TIME_UPPER     = 'T';

        // if (arg.TYPE != boolean) return boolean
        // if (arg != null) return true; else return false;
	static final char BOOLEAN             = 'b';
	static final char BOOLEAN_UPPER       = 'B';
        // if (arg instanceof Formattable) arg.formatTo()
        // else arg.toString();
	static final char STRING              = 's';
	static final char STRING_UPPER        = 'S';
        // arg.hashCode()
	static final char HASHCODE            = 'h';
	static final char HASHCODE_UPPER      = 'H';

	static final char LINE_SEPARATOR      = 'n';
	static final char PERCENT_SIGN        = '%';

	static boolean isValid(char c) {
	    return (isGeneral(c) || isInteger(c) || isFloat(c) || isText(c)
		    || c == 't' || isCharacter(c));
	}

格式化代码:
public class TestString {
    
    public static void main(String[] args) {
        
        String repStr = "hello";
        
        int repInt = 33;
        
        double repNo = 21.22;
        
        String configStr = "%s world %d %f";
        
        String ret = String.format(configStr, repStr,repInt,repNo);
        
        System.out.println(ret);        
    }

}


JDK HELP:

Conversion  Argument Category  Description 
'b', 'B'  general  If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(). Otherwise, the result is "true". 
'h', 'H'  general  If the argument arg is null, then the result is "null". Otherwise, the result is obtained by invoking Integer.toHexString(arg.hashCode()). 
's', 'S'  general  If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invoking arg.toString(). 
'c', 'C'  character  The result is a Unicode character 
'd'  integral  The result is formatted as a decimal integer 
'o'  integral  The result is formatted as an octal integer 
'x', 'X'  integral  The result is formatted as a hexadecimal integer 
'e', 'E'  floating point  The result is formatted as a decimal number in computerized scientific notation 
'f'  floating point  The result is formatted as a decimal number 
'g', 'G'  floating point  The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding. 
'a', 'A'  floating point  The result is formatted as a hexadecimal floating-point number with a significand and an exponent 
't', 'T'  date/time  Prefix for date and time conversion characters. See Date/Time Conversions. 
'%'  percent  The result is a literal '%' ('\u0025') 
'n'  line separator  The result is the platform-specific line separator 

Any characters not explicitly defined as conversions are illegal and are reserved for future extensions.

Date/Time Conversions
The following date and time conversion suffix characters are defined for the 't' and 'T' conversions. The types are similar to but not completely identical to those defined by GNU date and POSIX strftime(3c). Additional conversion types are provided to access Java-specific functionality (e.g. 'L' for milliseconds within the second).

The following conversion characters are used for formatting times: 'H'  Hour of the day for the 24-hour clock, formatted as two digits with a leading zero as necessary i.e. 00 - 23. 
'I'  Hour for the 12-hour clock, formatted as two digits with a leading zero as necessary, i.e. 01 - 12. 
'k'  Hour of the day for the 24-hour clock, i.e. 0 - 23. 
'l'  Hour for the 12-hour clock, i.e. 1 - 12. 
'M'  Minute within the hour formatted as two digits with a leading zero as necessary, i.e. 00 - 59. 
'S'  Seconds within the minute, formatted as two digits with a leading zero as necessary, i.e. 00 - 60 ("60" is a special value required to support leap seconds). 
'L'  Millisecond within the second formatted as three digits with leading zeros as necessary, i.e. 000 - 999. 
'N'  Nanosecond within the second, formatted as nine digits with leading zeros as necessary, i.e. 000000000 - 999999999. 
'p'  Locale-specific morning or afternoon marker in lower case, e.g."am" or "pm". Use of the conversion prefix 'T' forces this output to upper case. 
'z'  RFC 822 style numeric time zone offset from GMT, e.g. -0800. 
'Z'  A string representing the abbreviation for the time zone. The Formatter's locale will supersede the locale of the argument (if any). 
's'  Seconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE/1000 to Long.MAX_VALUE/1000. 
'Q'  Milliseconds since the beginning of the epoch starting at 1 January 1970 00:00:00 UTC, i.e. Long.MIN_VALUE to Long.MAX_VALUE. 


The following conversion characters are used for formatting dates: 'B'  Locale-specific full month name, e.g. "January", "February". 
'b'  Locale-specific abbreviated month name, e.g. "Jan", "Feb". 
'h'  Same as 'b'. 
'A'  Locale-specific full name of the day of the week, e.g. "Sunday", "Monday" 
'a'  Locale-specific short name of the day of the week, e.g. "Sun", "Mon" 
'C'  Four-digit year divided by 100, formatted as two digits with leading zero as necessary, i.e. 00 - 99 
'Y'  Year, formatted as at least four digits with leading zeros as necessary, e.g. 0092 equals 92 CE for the Gregorian calendar. 
'y'  Last two digits of the year, formatted with leading zeros as necessary, i.e. 00 - 99. 
'j'  Day of year, formatted as three digits with leading zeros as necessary, e.g. 001 - 366 for the Gregorian calendar. 
'm'  Month, formatted as two digits with leading zeros as necessary, i.e. 01 - 13. 
'd'  Day of month, formatted as two digits with leading zeros as necessary, i.e. 01 - 31 
'e'  Day of month, formatted as two digits, i.e. 1 - 31. 


The following conversion characters are used for formatting common date/time compositions. 'R'  Time formatted for the 24-hour clock as "%tH:%tM" 
'T'  Time formatted for the 24-hour clock as "%tH:%tM:%tS". 
'r'  Time formatted for the 12-hour clock as "%tI:%tM:%tS %Tp". The location of the morning or afternoon marker ('%Tp') may be locale-dependent. 
'D'  Date formatted as "%tm/%td/%ty". 
'F'  ISO 8601 complete date formatted as "%tY-%tm-%td". 
'c'  Date and time formatted as "%ta %tb %td %tT %tZ %tY", e.g. "Sun Jul 20 16:17:00 EDT 1969". 


Any characters not explicitly defined as date/time conversion suffixes are illegal and are reserved for future extensions
分享到:
评论

相关推荐

    Java字符串格式化

    总结来说,Java字符串格式化是构建和展示文本信息的关键技术,包括使用`String.format()`和`printf()`方法,以及`StringBuilder`和`StringBuffer`类。同时,理解并处理GBK这样的特定字符编码对于处理中文字符至关...

    java字符串格式化String.format()

    ### Java字符串格式化String.format()详解 #### 一、引言 `String.format()` 方法是JDK 1.5引入的新特性,它提供了一种灵活且强大的方式来格式化字符串。这种方法类似于C语言中的`printf`函数,允许开发者通过指定...

    字符串格式化需求规格说明书

    1. **格式化字符串的基本语法**:在各种编程语言中,如Python的f-string、C++的printf、Java的String.format等,都有各自的字符串格式化方法。理解这些语法对于正确地构造和输出格式化的字符串至关重要。 2. **变量...

    json字符串格式化工具

    "json字符串格式化工具"是针对JSON数据进行美化和整理的工具,主要功能包括将紧凑的JSON字符串转换为易于阅读的格式,添加缩进、换行,以及高亮显示键值对等。这对于开发者调试API接口、查看JSON响应或者理解复杂...

    json字符串格式化工具JsonView

    json字符串格式化工具,不用安装,直接运行,美化json字符串,方便,快捷。json字符串格式化工具,不用安装,直接运行,美化json字符串,方便,快捷。json字符串格式化工具,不用安装,直接运行,美化json字符串,...

    Java_格式化字符串 汇总

    Java中的字符串格式化功能是处理和展示数据时不可或缺的一部分,尤其在需要将各种数据类型转换为特定格式的字符串时显得尤为重要。本文将深入探讨Java中`String`类的`format()`方法,及其如何用于创建格式化的字符串...

    java 字符串 正则表达式 详细实例代码

    4. **日期和时间字符串格式化** - Java中的`java.util.Date`类和`java.text.SimpleDateFormat`类用于处理日期和时间。`SimpleDateFormat`允许我们自定义日期时间格式,如`"yyyy-MM-dd HH:mm:ss"`。 - 示例代码: ...

    Java字符串_日期_数字格式化输出

    #### 二、Java字符串格式化输出 在Java中,`System.out.println()` 是最常用的打印输出语句,但当需要进行复杂的格式化输出时,它就显得力不从心了。此时可以考虑使用 `java.text` 包中的类来实现更为灵活的格式化...

    Java字符串 日期 数字格式化输出-.doc

    Java 字符串日期数字格式化输出 Java 中的字符串日期数字格式化输出是一种常见的操作,特别是在数据输出和显示时。 Java 提供了多种方式来实现格式化输出,包括使用 NumberFormat、DecimalFormat 和 ...

    java字符串的各种编码转换

    ### Java字符串的编码转换 在Java中,处理不同字符集之间的字符串转换是一项常见任务。尤其是在处理国际化应用时,理解并掌握各种字符编码格式变得尤为重要。下面将介绍几种常见的字符编码格式以及如何在Java中实现...

    java验证字符串是否符合json格式

    在Java编程中,验证字符串是否符合JSON(JavaScript Object Notation)格式是一项常见的任务,尤其是在进行前后端数据交互时。JSON是一种轻量级的数据交换格式,它允许数据以键值对的形式存储,易于人阅读和编写,...

    java String 字符串格式化

    JDK1.5中,String类新增了一个很有用的静态方法,这些方法有助于开发人员对字符串的各类操作

    JAVA 字符串 操作

    在Java编程语言中,字符串...以上只是Java字符串操作的一小部分,实际开发中还有许多其他方法和特性,如国际化(I18N)、正则表达式等,都需要程序员灵活掌握。理解并熟练运用这些操作,能有效提高代码质量和效率。

    Java字符串长度不够前面或后面补0.txt

    ### Java字符串长度不够前面或后面补0 在Java编程中,有时我们需要确保字符串达到特定的长度,如果实际长度不足,则需要在字符串的前部或后部添加特定字符(如0)来填充,使得最终字符串满足预设长度的要求。本文将...

    Json字符串格式化工具

    HiJson工具免去了我们需要联网格式化json字符串的烦恼,无需百度搜索在线格式化,只要安装了JDK,打开就可以用。

    JAVA 字符串应用笔记

    在Java编程语言中,字符串是极其重要且常用的数据类型,尤其在Android开发中更是不可或缺。...在实际开发中,还会涉及到更多高级特性和实践技巧,如字符串格式化、正则表达式的复杂使用、字符串的安全问题等。

    JAVA日期与字符串的转换

    `SimpleDateFormat`是`java.text`包中的一个子类,它可以用来格式化和解析日期。下面是一个具体的例子: ```java import java.text.SimpleDateFormat; import java.util.Date; public class DateToStringConverter...

    Java字符串转换为日期和时间比较大小[归类].pdf

    在 Java 中,SimpleDateFormat 是一个格式化和解析日期的类。我们可以使用 SimpleDateFormat 将字符串转换为日期,然后比较日期的大小。 首先,我们需要创建一个 SimpleDateFormat 对象,并设置日期格式为 "yyyy-MM...

    java完美按格式化字符串String转sql.date

    ### Java完美按格式化字符串String转sql.Date 在Java编程中,经常需要处理日期与时间相关的数据。特别是在数据库操作时,经常会遇到需要将字符串类型的日期转换为`java.sql.Date`类型的情况。本文将详细介绍如何...

    java代码-使用java解决json字符串格式化的源代码

    java代码-使用java解决json字符串格式化的源代码 ——学习参考资料:仅用于个人学习使用!

Global site tag (gtag.js) - Google Analytics