1

I'm new developing for Android, i want to deserialize a String of a Date value, and i was looking for deserialize the date converted into json like this /Date(1446063654000)/ to String like this formart YYYY/MM/DD.

But i dont find a solution.

Could you check it and tell me how to do this?

2 Answers2

3

/Date(1446063654000)/ seems to be a unix timestamp. Assuming you have it as a String...

String str = "/Date(1446063654000)/";

...converting this into a date is quite simple, as you could, for example, simply do..

long time = Long.parseLong( str.substring(6, str.length() - 2 );

In other words, take the string part after '/Date( (which is 6 characters long) until the last ), in other words, just the number part, and parse it into a long.

A long can be made into a date...

Date date = new Date( time );

And a Date can be formatted into a String...

String formatted = new SimpleDateFormat("yyyy/MM/dd").format( date );
Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
0

You need to use SimpleDateFormat

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.US);
Date date = sdf.parse(string, parsePosition)

If you are using Gson library to parse Json string you can use:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat(dateFormat);
Gson gson = gsonBuilder.create();
gson.fromJson(string, YourClass.class);
m0rphine
  • 126
  • 4