Skip to content
Advertisement

How can I Convert Calendar.toString() into date using SimpleDateFormat.parse()?

I’m developing an Android app that uses a database, every time that the user insert a new register the current data and time is save in the db using

JavaScript

So, When I retrieve the data from the db, got a String like this:

java.util.GregorianCalendar[time=1496007575129,areFieldsSet=true,lenient=true,zone=America/Mexico_City,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=4,WEEK_OF_YEAR=22,WEEK_OF_MONTH=5,DAY_OF_MONTH=28,DAY_OF_YEAR=148,DAY_OF_WEEK=1,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=39,SECOND=35,MILLISECOND=129,ZONE_OFFSET=-21600000,DST_OFFSET=3600000]

The problem comes when I try convert that String using SimpleDateFormat.parse to display it in a RecyclerView, I get always the same date: 09/04/2017.

This is the code in my RecViewAdapter.java:

JavaScript

The other data is showed correctly in the RecView and the Calendar String are all diferent, so there is not the same date/hour in these strings. So, the question is:

How can I Convert Calendar.toString() into date using SimpleDateFormat.parse()?

This is the result running the app in a real device: two muppets

Advertisement

Answer

You need to modify the way you store the Calendar, call getTime() and format it as desired to begin with. For example,

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement