0

When I call a youtube API, it returns the following timestamp:

2012-11-13T18:38:30Z

I need to calculate 'time ago' based on current time and the above time stamp. So, using the timestamp above, and current time, I should get something like 6 days ago, etc.

I need to convert it in android, so any android or java answer can help.

There seems to be a function that calculates time ago, but you need to first convert the 2012-11-13T18:38:30Z to millisecond or seconds, or whatever

and my question is, in java, how do you convert 2012-11-13T18:38:30Z to something else that can give me seconds, days, or whatever.

user1697965
  • 1,255
  • 3
  • 14
  • 22

4 Answers4

0

Use SimpleDateFormat class to create Date object and then getTime() to get it in milliseconds.

Try Google, you will get lots of examples.

Pankaj
  • 5,132
  • 3
  • 28
  • 37
0

You mean Conversion from Date to Long?

This might help

http://www.roseindia.net/java/java-conversion/DateToLong.shtml

  • No, the example doesn't help, i have been seaching for a while, none of them work, i am converting this specific string 2012-11-13T18:38:30Z to whatever value as long as i can extract milli second value – user1697965 Nov 15 '12 at 02:39
0

You can't convert that to milliseconds it's only specified to the second.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
0

Use this pattern for parsing your timestamp.

yyyy-MM-dd'T'HH:mm:ssZ

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
java.util.Date parsedTimeStamp = format.parse("2012-11-13T18:38:30Z");
long videoTime = parsedTimeStamp.getTime();

I have told you the first part, and unfortunately, I am low on time, so you are on your own for the next steps: (I will add code later.) videoTime will give you time in milliseconds of the video. You can get current time using long systemTime = System.currentTimeMillis()/1000. Subtract these two, and convert the result of the subtraction to get the difference.

Calender c1 = Calender.getInstanec().setTime(new Date(videoTime));
Calender c2 = Calender.getInstanec().setTime(new Date(systemTime));

Create two calenders using this date, and using Calender methods, you find difference between dates in terms of days and months.

theTuxRacer
  • 13,709
  • 7
  • 42
  • 59
  • Thanks..but java.util.Date parsedTimeStamp = format.parse("2012-11-13T18:38:30Z"); throws an exception, saying it is unparseable date – user1697965 Nov 15 '12 at 18:56
  • I just searched SO, it has been answered before. Take a look: http://stackoverflow.com/questions/2201925/converting-iso8601-compliant-string-to-java-util-date – theTuxRacer Nov 16 '12 at 04:18