Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

java - SimpleDateFormat with TimeZone

I'm trying to format date from java.util.Date. I need this format:

2016-06-10T13:38:13.687+02:00.

How correctly convert this from standard Date format

May 04 09:51:52 CDT 2009 ?

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z", Locale.getDefault());
sdf.format(new Date());

This code unfortunately return value without +02:00.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As per the standard Java docs: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

for getting date and time formatting of

2001-07-04T12:08:56.235-07:00


You Need to use below String pattern:

"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"

So with below code, you can get what you want:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.getDefault());
    simpleDateFormat .format(new Date());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...