Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 414
Mohit Kanada Avatar asked May 25 '11 11:05

Mohit Kanada


People also ask

How do I display a date in a specific format?

The dates appear as, mm/dd/yyyy in the U.S. and as, dd/mm/yyyy outside the U.S. where mm is the month, dd is the day, and yyyy is the year. The time is displayed as, hh:mm:ss AM/PM, where hh is the hour, mm is minutes, and ss is seconds.

How do you read a timestamp with time zones?

To calculate UTC time one has to subtract the offset from the local time, e.g. for "15:00−03:30" do 15:00 − (−03:30) to get 18:30 UTC. Show activity on this post. That timestamp has a timezone offset that is telling you what time it was and the UTC offset.

What is T and Z in timestamp?

The T doesn't really stand for anything. It is just the separator that the ISO 8601 combined date-time format requires. You can read it as an abbreviation for Time. The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).


1 Answers

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)

like image 126
jmj Avatar answered Sep 21 '22 13:09

jmj