java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map

Viewed 1154

I'm working on CodeName One and I've got this error while retrieving data from the database.

The problem is with the element with type date the json response is working well and other attributes are displayed successfully.

Here's my code and I hope that someone could help me. Thank you in advance.

private Concour parseConcours(Map<String, Object> map) {
  Concour c = new Concour();

  c.setId(((Double) map.get("id")).intValue());
  c.setSujet(map.get("sujet").toString());
  c.setNom(map.get("nom").toString());
  c.setImageName(map.get("imageName").toString());
  c.setDescription(map.get("description").toString());
  c.setCategorie(map.get("categorie").toString());
  c.setCouleur(map.get("couleur").toString());
  c.setDateDebut(new Date((((Double) ((Map<String, Object>) map.get("dateDebut")).get("timestamp")).longValue() * 1000)));
  c.setDateFin(new Date((((Double) ((Map<String, Object>) map.get("dateFin")).get("timestamp")).longValue() * 1000)));

  return c;
}
2 Answers

Hyy what you can do is convert date string which you got from map to simpledateformat and then assign it in setter method as given below.

c.setDateDebut(new SimpleDateFormat("dd/MM/yyyy").parse(map.get("dateDebut")));
c.setDateFin(new SimpleDateFormat("dd/MM/yyyy").parse(map.get("dateFin")));

try this :

try {
   Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(sDate1); 
   c.setDateDebut(date1); 
    } 
catch (ParseException ex) { 
     System.out.println(ex.getMessage()); 
    }
Related