`
ouyangfeng521
  • 浏览: 248769 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

android 格式化

阅读更多

在android 里经常要用到些提示  有时候要引用一些格式化  android 里有个

Formatter类里面有些格式化的像C的printf

 

Formats arguments according to a format string (like printf in C).

It's relatively rare to use a Formatter directly. A variety of classes offer convenience methods for accessing formatter functionality. Of these, format(String, Object...) is generally the most useful. PrintStream and PrintWriter both offer format and printf methods.

Format strings consist of plain text interspersed with format specifiers, such as "name: %s weight: %03dkg\n" . Being a Java string, the usual Java string literal backslash escapes are of course available.

Format specifiers (such as "%s" or "%03d" in the example) start with a % and describe how to format their corresponding argument. It includes an optional argument index, optional flags, an optional width, an optional precision, and a mandatory conversion type. In the example, "%s" has no flags, no width, and no precision, while "%03d" has the flag 0 , the width 3, and no precision.

Not all combinations of argument index, flags, width, precision, and conversion type are valid.

Argument index . Normally, each format specifier consumes the next argument to format . For convenient localization, it's possible to reorder arguments so that they appear in a different order in the output than the order in which they were supplied. For example, "%4$s" formats the fourth argument (4$ ) as a string (s ). It's also possible to reuse an argument with < . For example, format("%o %<d %<x", 64) results in "100 64 40" .

Flags . The available flags are:

 

Flags
, Use grouping separators for large numbers. (Decimal only.) format("%,d", 1024); 1,234
+ Always show sign. (Decimal only.) format("%+d, %+4d", 5, 5);
+
5
,
   
+
5
A space indicates that non-negative numbers should have a leading space. (Decimal only.) format("x% d% 5d", 4, 4);
x 
4
    
4
( Put parentheses around negative numbers. (Decimal only.) format("%(d, %(d, %(6d", 12, -12, -12);
12
,
 
(
12
),
   
(
12
)
- Left-justify. (Requires width.) format("%-6dx", 5);
format("%-3C, %3C", 'd', 0x65);
5
      x

D  
,
   E
0 Pad the number with leading zeros. (Requires width.) format("%07d, %03d", 4, 5555); 0000004, 5555
# Alternate form. (Octal and hex only.) format("%o %#o", 010, 010);
format("%x %#x", 0x12, 0x12);
10 010
12 0x12

Width . The width is a decimal integer specifying the minimum number of characters to be used to represent the argument. If the result would otherwise be shorter than the width, padding will be added (the exact details of which depend on the flags). Note that you can't use width to truncate a field, only to make it wider: see precision for control over the maximum width.

Precision . The precision is a . followed by a decimal integer, giving the minimum number of digits for d , o , x , or X ; the minimum number of digits after the decimal point for a , A , e , E , f , or F ; the maximum number of significant digits for g or G ; or the maximum number of characters for s or S .

Conversion type . One or two characters describing how to interpret the argument. Most conversions are a single character, but date/time conversions all start with t and have a single extra character describing the desired output.

Many conversion types have a corresponding uppercase variant that converts its result to uppercase using the rules of the relevant locale (either the default or the locale set for this formatter).

This table shows the available single-character (non-date/time) conversion types:

String conversions
All types are acceptable arguments. Values of type Formattable have their formatTo method invoked; all other types use toString .
s String. format("%s %s", "hello", "Hello"); hello Hello
S Uppercase string. format("%S %S", "hello", "Hello"); HELLO HELLO
Character conversions
Byte, Character, Short, and Integer (and primitives that box to those types) are all acceptable as character arguments. Any other type is an error.
c Character. format("%c %c", 'd', 'E'); d E
C Uppercase character. format("%C %C", 'd', 'E'); D E
Integer conversions
Byte, Short, Integer, Long, and BigInteger (and primitives that box to those types) are all acceptable as integer arguments. Any other type is an error.
d Decimal. format("%d", 26); 26
o Octal. format("%o", 032); 32
x , X Hexadecimal. format("%x %X", 0x1a, 0x1a); 1a 1A
Floating-point conversions
Float, Double, and BigDecimal (and primitives that box to those types) are all acceptable as floating-point arguments. Any other type is an error.
f Decimal floating point.
format
(
"%f"
,
 
123.456f
);

format
(
"%.1f"
,
 
123.456f
);

format
(
"%1.5f"
,
 
123.456f
);

format
(
"%10f"
,
 
123.456f
);

format
(
"%6.0f"
,
 
123.456f
);
123.456001


123.5


123.45600


123.456001

   
123
e , E Engineering/exponential floating point.
format
(
"%e"
,
 
123.456f
);

format
(
"%.1e"
,
 
123.456f
);

format
(
"%1.5E"
,
 
123.456f
);

format
(
"%10E"
,
 
123.456f
);

format
(
"%6.0E"
,
 
123.456f
);
1.234560e+02


1.2e+02


1.23456E+02


1.234560E+02

 
1E+02
g , G Decimal or engineering, depending on the magnitude of the value. format("%g %g", 0.123, 0.0000123); 0.123000 1.23000e-05
a , A Hexadecimal floating point. format("%a", 123.456f); 0x1.edd2f2p6
Boolean conversion
Accepts Boolean values. null is considered false, and instances of all other types are considered true.
b , B Boolean. format("%b %b", true, false);
format("%B %B", true, false);
format("%b", null);
format("%b", "hello");
true false
TRUE FALSE
false
true
Hash code conversion
Invokes hashCode on its argument, which may be of any type.
h , H Hexadecimal hash code. format("%h", this);
format("%H", this);
format("%h", null);
190d11
190D11
null
Zero-argument conversions
% A literal % character. format("%d%%", 50); 50%
n Newline. (The value of the system property "line.separator" .) format("first%nsecond"); first\nsecond

It's also possible to format dates and times with Formatter , though you should seriously consider using SimpleDateFormat via the factory methods in DateFormat instead. The facilities offered by Formatter are low-level and place the burden of localization on the developer. Using getDateInstance() , getTimeInstance() , and getDateTimeInstance() is preferable for dates and times that will be presented to a human. Those methods will select the best format strings for the user's locale.

The best non-localized form is ISO 8601 , which you can get with "%tF" (2010-01-22), "%tF %tR" (2010-01-22 13:39), "%tF %tT" (2010-01-22 13:39:15), or "%tF %tT%z" (2010-01-22 13:39:15-0800).

As with the other conversions, date/time conversion has an uppercase format. Replacing %t with %T will uppercase the field according to the rules of the formatter's locale.

This table shows the date/time conversions:

Date/time conversions
Calendar, Date, and Long (representing milliseconds past the epoch) are all acceptable as date/time arguments. Any other type is an error. The epoch is 1970-01-01 00:00:00 UTC.
ta Localized weekday name (abbreviated). format("%ta", cal, cal); Tue
tA Localized weekday name (full). format("%tA", cal, cal); Tuesday
tb Localized month name (abbreviated). format("%tb", cal); Apr
tB Localized month name (full). format("%tB", cal); April
tc Locale-preferred date and time representation. (See DateFormat for more variations.) format("%tc", cal); Tue Apr 01 16:19:17 CEST 2008
tC 2-digit century. format("%tC", cal); 20
td 2-digit day of month (01-31). format("%td", cal); 01
tD Ambiguous US date format (MM/DD/YY). Do not use. format("%tD", cal); 04/01/08
te Day of month (1-31). format("%te", cal); 1
tF Full date in ISO 8601 format (YYYY-MM-DD). format("%tF", cal); 2008-04-01
th Synonym for %tb .
tH 24-hour hour of day (00-23). format("%tH", cal); 16
tI 12-hour hour of day (01-12). format("%tH", cal); 04
tj 3-digit day of year (001-366). format("%tj", cal); 092
tk 24-hour hour of day (0-23). format("%tH", cal); 16
tl 12-hour hour of day (1-12). format("%tH", cal); 4
tL Milliseconds. format("%tL", cal); 359
tm 2-digit month of year (01-12). format("%tm", cal); 04
tM 2-digit minute. format("%tM", cal); 08
tN Nanoseconds. format("%tN", cal); 359000000
tp a.m. or p.m. format("%tp %Tp", cal, cal); pm PM
tQ Milliseconds since the epoch. format("%tQ", cal); 1207059412656
tr Full 12-hour time (%tI:%tM:%tS %Tp ). format("%tr", cal); 04:15:32 PM
tR Short 24-hour time (%tH:%tM ). format("%tR", cal); 16:15
ts Seconds since the epoch. format("%ts", cal); 1207059412
tS 2-digit seconds (00-60). format("%tS", cal); 17
tT Full 24-hour time (%tH:%tM:%tS ). format("%tT", cal); 16:15:32
ty 2-digit year (00-99). format("%ty", cal); 08
tY 4-digit year. format("%tY", cal); 2008
tz Time zone GMT offset. format("%tz", cal); +0100
tZ Localized time zone abbreviation. format("%tZ", cal); CEST

Number localization . Some conversions use localized decimal digits rather than the usual ASCII digits. So formatting 123 with %d will give 123 in English locales but ١٢٣ in appropriate Arabic locales, for example. This number localization occurs for the decimal integer conversion %d , the floating point conversions %e , %f , and %g , and all date/time %t or %T conversions, but no other conversions.

分享到:
评论

相关推荐

    android格式化U盘、SATA硬盘为exFAT格式,支持2T以上硬盘

    android默认格式化U盘、SATA硬盘为FAT32格式。FAT32无法使用2T以上硬盘。 而NTFS有版权保护,window又不识别ext4,所以使用exFAT比较适合。android,windows都能识别,都能读写。 拿一个新硬盘,没有分区的。设置---...

    android格式化文件

    android格式化文件,添加此插件,简单方便地整理 android 代码格式

    android恢复出厂设置及格式化SDCard

    在Android操作系统中,恢复出厂设置和格式化SDCard是两种常见的操作,用于处理设备问题或保护个人数据。本文将详细讲解这两个概念,以及如何在2.1及以上版本的Android系统中进行操作。 **一、恢复出厂设置** 恢复...

    android 日期格式化

    在Android开发中,日期格式化是一项常见的需求,用于将系统时间或特定时间转换为易于阅读和处理的格式。本文将深入探讨几个关键的日期格式化方法,这些方法可以帮助开发者更好地管理和展示日期信息。 ### 一、基本...

    Android SD剩余大小的求解及格式化显示

    可以作为开发中的工具函数,封装的很好的求解SD卡剩余大小的方法,及格式化显示的方法。简单好用。

    FuzzyDateFormatter, 用于模糊日期和时间的Android格式化程序.zip

    FuzzyDateFormatter, 用于模糊日期和时间的Android格式化程序 模糊日期格式化程序格式化日期以模糊形式显示的简单格式化程序库,用于在应用程序中显示。 E.g.Calendar -&gt;"6 minutes ago"捐赠和翻译欢迎。用法...

    Android-AndroidJSON格式化显示可配置样式展开与折叠

    本文将深入探讨如何在Android中实现一个JSON查看器,以友好的可读格式展示JSON数据,并支持用户进行展开和折叠操作,提升用户体验。 首先,我们需要理解JSON的基本结构。JSON由键值对组成,键用双引号包围,后跟...

    java & Android 格式化字符串详解

    以下是对Java和Android格式化字符串的详细讲解。 1. **基本格式化** - `%d` 用于表示整型数值,例如`%1$d`表示第一个整型参数,`%2$d`表示第二个整型参数。在`string.xml`文件中,可以使用`%1$d`来创建一个占位符...

    Android 带小数点的字符串格式化为固定位数

    在Android开发中,数据的格式化是一个非常重要的环节,特别是在显示数字时,为了保持数据的一致性和可读性,我们经常需要将带有小数点的字符串格式化为固定位数。这个工具类就是为了满足这样的需求而设计的。下面将...

    Android 时间格式化Demo

    在Android开发中,时间格式化是一项基础且重要的任务,它涉及到日期和时间的显示、解析以及比较等操作。本Demo是专为学习Android中的时间格式化而编写的,包含了一个实用的时间格式化工具类。通过这个Demo,我们可以...

    Android 将网络返回的Json数据格式化

    本教程将详细介绍如何在Android中接收、格式化Json数据,并结合Retrofit网络请求框架进行高效的数据操作。 一、理解Json格式 Json是一种基于文本的数据格式,它以键值对的形式存储数据,支持数组和对象。例如: ```...

    Android和XML代码格式化

    Eclipse 中: 配置方法: window-&gt;preferences-&gt;java-&gt;Code style-&gt;Formatter中导入android-formatting.xml ...XML格式化: http://www.androidpolice.com/2009/11/04/auto-formatting-android-xml-files-with-eclipse/

    Android-BankCardUtils自动格式化银行卡号手机号身份证号输入的工具类

    "Android-BankCardUtils"就是一个这样的工具类,它专注于自动格式化和验证银行卡号、手机号和身份证号。下面将详细讲解这个工具类的主要功能和实现原理。 首先,BankCardUtils的核心功能是根据银行卡号识别出对应的...

    Android代码格式化需要用到的配置文件

    Android代码格式化需要用到的配置文件,需要的可以到这里下载

    Android 对 strings.xml 的字符串进行格式化

    综上所述,通过合理利用`strings.xml`文件中的格式化功能,可以在Android应用开发中实现更加灵活和丰富的用户界面展示效果。同时,需要注意遵循一定的最佳实践原则,以确保代码的可维护性和可扩展性。

    Android日期时间格式国际化的实现代码

    在Android开发中,处理日期和时间的格式化时,为了适应不同国家和地区的用户习惯,通常需要进行国际化(i18n)处理。Android提供了`java.text.DateFormat`和`java.text.SimpleDateFormat`等类来帮助开发者实现这一...

    HP U盘格式化工具 V2.0.6

    【HP U盘格式化工具 V2.0.6】是一款专为HP品牌U盘设计的实用工具,旨在帮助用户对U盘进行高效、安全的格式化操作。该工具经过实际测试,确认能有效运行并解决U盘在使用过程中可能出现的问题,如容量显示异常或数据...

    Android 一些数字文字相关的格式化工具类

    验证手机,判断身份证,判定输入汉字等

Global site tag (gtag.js) - Google Analytics