java设置类的字符格式_java类---与格式化相关的类

程序运行结果:

a4c26d1e5885305701be709a3d33442f.png显然月份字段在结果字符串中的位置是[5,7),注意是一个前闭后开区间。

String string)

将指定的日期字符串解析成Date对象

String string, ParsePosition

position)从position指定索引开始,将指定的日期字符串解析成Date对象,

备注:使用DateFormat解析日期字符串时,日期字符串的样式(style)必须和DateFormat的样式一种,否则会抛出ParseException异常。比如

DateFormat dateFmt =

DateFormat.getDateInstance();

String strDate = "2013年8月8日";

dateFmt.parse(strDate);

注意当上面例子中的日期字符串为类似String strDate =

"ww2013年8月8日"的不合法的字符串时,同样也会解析失败,通过String

string, ParsePosition position)方法则可以解析成功。比如。

ParsePositon parsePos = new

ParsePosition(2);

dateFmt.parse(strDate,paserPos);

跳过开头前两个非法的字符。

SimpleDateFormat

Class Overview

A concrete class for formatting and

parsing dates in a locale-sensitive manner. Formatting turns a

String into a Date.

类SimpleDateFormat是DateFormat的子类,是一个具体类。该类以对语言环境敏感的方法来格式化和解析Date对象。格式化将日期转化成字符串、解析将字符串转化成Date对象

SimpleDateFormat比DateFormat功能更加强大,可以非常灵活的格式化Date,除了可以调用通过继承获得的DateFormat的工厂方法来得到一个DateFormat实例外,还可以在构造SimpleDateFormat时传入一个Pattern字符串(日期模板字符串)来定制如何格式化和解析日期字符串。

Pattern 字符串的语法:

To specify the time format, use a time

pattern string. In this string, any character from

'A' to 'Z' or 'a' to

'z' is treated specially. All other characters are

passed through verbatim. The interpretation of each of the ASCII

letters is given in the table below. ASCII letters not appearing in

the table are reserved for future use, and it is an error to

attempt to use them.

使用pattern字符串来指定时间的格式,在模板字符串中,从大'A'到大'Z',从小'a'到小'z'的每一个字符都被当成特殊字符(具有特定含义的字符称为特殊字符),其它传进来的字符则被原样输出,每一个ASCII码字符的含义在下表中指定,没有出现在下表中的ASCII字符被保留为将来使用,如果试图使用它们将会产生错误。

Symbol

Meaning

Presentation

Example

D

day in year

(Number)

189

E

day of week

(Text)

Tuesday

F

day of week in month

(Number)

2 (2nd Wed in July)

G

era designator

(Text)

AD

H

hour in day (0-23)

(Number)

0

K

hour in am/pm (0-11)

(Number)

0

L

stand-alone month

(Text/Number)

July / 07

M

month in year

(Text/Number)

July / 07

S

fractional seconds

(Number)

978

W

week in month

(Number)

2

Z

time zone (RFC 822)

(Timezone)

-0800

a

am/pm marker

(Text)

PM

c

stand-alone day of week

(Text/Number)

Tuesday / 2

d

day in month

(Number)

10

h

hour in am/pm (1-12)

(Number)

12

k

hour in day (1-24)

(Number)

24

m

minute in hour

(Number)

30

s

second in minute

(Number)

55

w

week in year

(Number)

27

y

year

(Number)

2010

z

time zone

(Timezone)

Pacific Standard Time

'

escape for text

(Delimiter)

'Date='

''

single quote

(Literal)

'o''clock'

表中Number代表数字,Text代表字符串,Text/Number代表可以是字符串或数字。

pattern字符串中连续模式字符数目决定了格式字符串的样式:

Text:如果模式字母的数量大于或等于

4,则使用完全形式;否则,在尽可能的情况下(有缩写形式)使用短形式 或缩写形式。 比如zzzz

将会输出"Pacific Standard Time" 而 z

m则会输出"PST"

Number:模式字母的数量是最小的数位,如果数位不够,则用 0 填充以达到此数量。

Text/Number:如果模式字母的数量为 3 或大于

3,解释为成Text;否则解释为number.

用得最多的模板字符串"yyyy-MM-dd HH:mm:ss.SSSZ",该模板字符串符合ISO 8601

国际标准日期格式

其它常用模板:

"yyyy-MM-dd""yyyy-MM-dd HH:mm""yyyy-MM-dd HH:mmZ""yyyy-MM-dd HH:mm:ss.SSSZ"

常用函数:

构造器:

其格式化和解析方法和DateFormat差不多。

NumberFormat(数字格式器)

Class Overview

The abstract base class for all number

formats. This class provides the interface for formatting and

parsing numbers. NumberFormat also provides methods

for determining which locales have number formats, and what their

names are.

NumberFormat helps you to

format and parse numbers for any locale. Your code can be

completely independent of the locale conventions for decimal

points, thousands-separators, or even the particular decimal digits

used, or whether the number format is even decimal.

类NumberFormat是各种数字格式器的抽象基类,该类提供了一个接口用于格式化和解析数字,NumberFormat

还提供了一些方法,用来确定哪些语言环境具有数字格式,以及它们的名称是什么。

NumberFormat

将帮助你格式化和解析何语言环境的数字,代码能够完全独立于语言环境对于小数点、千位分隔符或特殊的十进制数字的约定,甚至与数字格式是否为十进制也是无关的。

NumberFormat是一个抽象类,我们虽然不能通过其构造器获取其实例,但NumberFormat提供了几个静态的工厂方法用来返回NumberFormat实例.

1

与获取NumberFormat实例相关的静态工厂方法

final static

NumberFormat getInstance() 根据默认的Locale返回通用的数字格式器

Locale locale) 根据指定的Locale返回整数格式器

2 与格式化和解析字符串相关的方法

ChoiceFormat

Class Overview

Returns a fixed string based on a numeric

value. The class can be used in conjunction with the

ChoiceFormat enables users to attach a

format to a range of numbers. The choice is specified with an

ascending list of doubles, where each item specifies a half-open

interval up to the next item as in the following: X matches j if

and only if limit[j] <= X <

limit[j+1].

f there is no match, then either the

first or last index is used. The first or last index is used

depending on whether the number is too low or too high. The length

of the format array must be the same as the length of the limits

array.

类Choice基于数字的值返回一个固定的字符串,该类可以和MessageFormat类联合使用用来处理信息,ChoiceFormat允许用户将一个格式字符串绑定到一个数值区间,choice由按升序排列的double列表指定

列表中每一项都指定一个到下一项的半开区间:当且仅当 limit[j] <= X

< limit[j+1] 时,X 匹配 j 。

如果不匹配,则根据数 (X) 的是太小还是太大,选择使用第一个或最后一个索引。如果 limit

数组不按升序排列,则格式化的结果将不正确

1 构造器:String[]

formats)

@param limits : double数值

@param formats:格式字符串数值.

根据这两个参数,ChoiceFormat对象就可以将指定double数值格式化成相应的字符串,也可以将固定的字符串解析成double数值.

Examples:

double[]limits={1,2,3,4,5,6,7};String[]fmts={"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};double[]limits2={0,1,ChoiceFormat.nextDouble(1)};String[]fmts2={"no files","one file","many files"};

String

template)

@param template:这是一个模板字符串。

该构造器接受一个模板字符串作为参数,比如上面例子中,也可以使用如下模板字符串来替代:

"1#Sun|2#Mon|3#Tue|4#Wed|5#Thur|6#Fri|7#Sat""0#are no files|1#is one file|1

#" #代表包括这个值;

"

常用方法:

与格式化相关的辅助类

(1)ParsePositon

Class Overview

Tracks the current position in a parsed string. In case of an error

the error index can be set to the position where the error occurred

without having to change the parse position

用来在解析字符串时跟踪当前位置,当解析字符串出错时,出错位置索引会被记录到position对象中去。

常用方法:

:解析字符串出错时,返回错误位置索引.

应用举例:

DateFormat dateFmt =

DateFormat.getDateInstance();

String strDate = "xx2013-8-8日";

dateFmt.parse(strDate);

这时可以通过一个ParsePositon对象跳过开头两个非法字符.

ParsePostion parsePos = new ParsePosition(2);

dateFmt.parse(strDate,parsePos);

(2)FiledPositon

Class Overview

Identifies fields in formatted strings.

If a FieldPosition is passed to the format method with

such a parameter, then the indices will be set to the start and end

indices of the field in the formatted string.

FiledPositon是Format及其子类用于在格式化输出字符串中标识指定字段位置的简单类,如果在格式化方法是传入了FiledPosition参数,指定字段在格式化结果字符串中开始和结束索引将会被填入该参数中去。

常用方法:

该类的应用举例见前面。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值