0

I have a problem with a data type "Date", which I get from a JSON API.

I have an object that contains these two types of Dates.

@SerializedName("DateDebut")
public Date dateDebut;

@SerializedName("DateFin")
public Date dateFin;

When I post I don't get any errors but on the response.body it goes into catch and I have this error.

Failed to parse date ["2021-05-26T00:00:00"]: No time zone indicator

The type of data I receive is this "DateDebut": "2021-04-21T13: 56: 55.552Z".

I believe it is a DateTime type, while I in my application Date.

    @SerializedName("DateDebut")
    public String dateDebutReq;

    @SerializedName("DateFin")
    public String dateFinReq;
    
    public Date dateDebut;
   
    public Date dateFin;

If I create two more String types, everything works fine, but I have to leave the Dates for the post.

I would like to avoid having two types 4 elements instead of two.

I have found articles that recommend adding the time zone to the parse in this way

dateFormat.timeZone = TimeZone.getTimeZone ("GMT");

but I have the problem with the reception where I do not do any part Do you have solutions? Thanks

Kevin
  • 415
  • 3
  • 8
  • 21
  • Date is an object while you must be getting date as String from the server. You will have to parse that date string to the Date object manually using SImpleDateFormat. – Kunu Apr 22 '21 at 08:53
  • 1
    This might solve your issue https://stackoverflow.com/questions/18473011/retrofit-gson-serialize-date-from-json-string-into-java-util-date#:~:text=Look%20at%20Ondreju's%20response%20to,implement%20this%20in%20retrofit%202.&text=Quite%20literally%20if%20you%20already,ss'Z'%22). – NRUSINGHA MOHARANA Apr 22 '21 at 09:17
  • @NRUSINGHAMOHARANA thanks, I solved the problem with your answer/link – Kevin Apr 22 '21 at 11:52

0 Answers0