Parse DATE with data from a JSON

2

How about today I had a problem with a parseo Date I'm bringing data from a service so far all good, well I have a field in which brings me a Date with the following format

2017-01-05 11:11:00

Now this value takes me error in my codigo , having event as a model and jEvento as a JSONARRAY

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS", new Locale("es", "pe"));
        try {
         evento.setFecha_inicio(sdf.parse(jEventos.getJSONObject(i).getString("EVE_FEC_INICIO")+".000000"));
        } catch (ParseException e) {
            e.printStackTrace();
        }

As you will see I have added +".000000" I do not think it is a good practice, now my question is whether to continue with this code or modify the type of variable in my database. Maybe someone has a better way to work this. Thanks.

    
asked by marlonpya 30.01.2017 в 20:26
source

1 answer

2

Definitely ideally you have a uniform data format in the .json, for example:

"yyyy-MM-dd HH:mm:ss"

I also do not see any case defining a format that contains a millisecond:

"yyyy-MM-dd HH:mm:ss.SSSSSS"

when you are always defining the milli seconds as:

".000000"

The reason for the problem is that the date in the json, originally came without milliseconds and the format was defined as:   "yyyy-MM-dd HH:mm:ss.SSSSSS"

a format defined as:

"yyyy-MM-dd HH:mm:ss" 

would be enough.

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", new Locale("es", "pe"));
    
answered by 30.01.2017 / 23:12
source