Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 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 Answer

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...