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
409 views
in Technique[技术] by (71.8m points)

java - How can I get Date in MM/DD/YY format from Timestamp

I want to get the Date in MM/DD/YY format from a timestamp.

I have used the below method but it does not gives proper output

final Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Long.parseLong(1306249409));    
Log.d("Date--",""+cal.DAY_OF_MONTH);    
Log.d("Month--",""+cal.MONTH);    
Log.d("Year--",""+cal.YEAR);

But its gives the output like below

Date--5 Month--2 Year--1

The correct date is 24 May 2010 for Timestamp - 1306249409

Note - Timestamp is received by a webservice which is used in my application.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Better Approach

Simply Use SimpleDateFormat

new SimpleDateFormat("MM/dd/yyyy").format(new Date(timeStampMillisInLong));

Mistake in your Approach

DAY_OF_MONTH ,MONTH, .. etc are just constant int value used by Calendar class internally

You can get the date represented by cal by cal.get(Calendar.DATE)


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

...