In-App purchase response, as purchase time, how to convert this timestamp to date format, the problem I am facing is the value of timestamp is exceeding long data type limit? kindly explain how to finad the date from timestamp in this case?
Asked
Active
Viewed 2,208 times
4
-
Please provide more information. What have you tried? How does the returned data look like? And please post some code showing how you are using the response. – ekholm Jun 21 '12 at 13:15
-
Hi Ekholm, the returned data look like {"nonce":1385087996049441605,"orders":[{"orderId":"839934789353249","packageName":"com.picsean.augustman","productId":"com.burda.august.01.06.2012","purchaseTime":1340109714000,"purchaseState":0}]} this is json response I am getting now in this one of the element is purchase time which has value 1340109714000, Now this value is exceeding "long" data type limit. one more example of purchase time is like 1340264211967, So the problem is how should I covert this value to data format? – Devavrata Jun 21 '12 at 13:18
-
The time stamp looks fine, it should not be any problem creating a Date object from that. Could you provide some code on how you try to do that and where it goes wrong? – ekholm Jun 21 '12 at 13:26
-
DateFormat format = new SimpleDateFormat("MMddyyHHmmss"); try { Date date = format.parse("1340264211967"); System.out.println("DATE VALUE IS:::: "+date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } this is giving me wrong date and time, the result of this code is DATE VALUE IS:::: Sun Feb 13 01:14:00 IST 2011, rather the actual result should be 19/06/2012 18:11:54 this value I got from online converter which is correct. – Devavrata Jun 21 '12 at 13:38
2 Answers
6
You are trying to parse a timestamp with SimpleDateFormat using an incorrect format string "MMddyyHHmmss". The timestamp value returned is expressed in "milliseconds since the epoch (Jan 1, 1970)". Parse the string to its long value and create a Date object directly from that:
Date date = new Date(Long.parseLong(timestampString));

ekholm
- 2,543
- 18
- 18
0
Kotlin version for subscriptions
- I have run into a similar problem but for subscriptions, so here is my solution.
- If you ever have your user purchase a subscription, it is common to tell them when their next billing period is. Here is a simple Kotlin conversion:
val timeStamp = purchase.purchaseTime
val date = Date(timeStamp)
val calendar = Calendar.getInstance()
calendar.time = date
calendar.add(Calendar.DATE,30)
Timber.tag("TIMES").d(calendar.time.toString())
- The
purchase
is a Purchase object. You should now be able to tell your user when their next billing period will be.
References

Tristan Elliott
- 594
- 10
- 8