1

I have a Asp.net c# webservice that returns a dateTime object. I am consuming this webservice in my Android app. The value i get when i check from degugger is like '/Date(1400220842000)/'

How can i convert this value to valid Java Date object or date string or timeinmillis

user545424
  • 15,713
  • 11
  • 56
  • 70
isumit
  • 2,313
  • 4
  • 23
  • 28
  • Is there a possibility of converting the date to a long value (Date in milliseconds in the WS) and construct the date object from the long value? – Ashish May 16 '14 at 17:32

3 Answers3

1

What type of webservice do you have? You could try splitting the string on '(', this would return an array with the string splitted in half, take the last half then splitting it again on ')' This way you can extract the milliseconds

Then follow this article to convert it to datetime:

how to convert milliseconds to date format in android?

or

how to get date from milliseconds in android

Milliseconds to Date in GMT in Java

http://www.java2s.com/Code/Android/Date-Type/getDateStringfrommilliseconds.htm

http://javarevisited.blogspot.ca/2012/12/how-to-convert-millisecond-to-date-in-java-example.html

Use this to check quickly if the number you have is actually a datetime converted in milliseconds : http://currentmillis.com/

Community
  • 1
  • 1
Tascalator
  • 462
  • 5
  • 13
  • Are you sure that the value between ( ) is the milliseconds value ? I can get the date from milliseconds and actually i need the milliseconds only. – isumit May 16 '14 at 17:40
  • nope im not sure, but I believe it is, check out this website http://currentmillis.com/ The number you have returns Fri May 16 2014 6:14:02 AM.. seems legit? :) – Tascalator May 16 '14 at 17:41
1

The value that is returned by your webservice seems to be of timestamp type, which is defined as milliseconds past 1 jan 1970 (UNIX epoch). So in order to consume it you can use this code :

private String getDate(long time) {
Calendar cal = Calendar.getInstance(Locale.ENGLISH);
cal.setTimeInMillis(time);
String date = DateFormat.format("dd-MM-yyyy", cal).toString();
return date;

}

Farhad
  • 12,178
  • 5
  • 32
  • 60
0

The value inside ( ) is the value of milliseconds. So I first got the milliseconds extracted using :

Scanner in = new Scanner(obj.getString("CreationTime")).useDelimiter("[^0-9]+");
Long createdate = in.nextLong();

And then getting Date or Calendar object from milliseconds was easy :)

isumit
  • 2,313
  • 4
  • 23
  • 28