Date and Time on Android

1

Hi, I'm trying to send a datetime format to the Mysql database. I'm using this snippet of code:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    Date date = new Date();
    String fecha = dateFormat.format(date);

But when I send, only the day arrives and the time arrives in 00:00:00

I would like to know what I am doing wrong. Thanks.

    
asked by Juampi 13.12.2018 в 19:31
source

1 answer

1

The pattern or format you pass to SimpleDateFormat is missing to add:
hh:mm:ss 12 hour format, or HH:mm:ss 24 hour format.

Here are two examples:

Example 1: 12 hour format:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault());
Date date = new Date();
String fecha = dateFormat.format(date);
System.out.println(fecha);

Example 2: format in 24 hours:

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
String fecha = dateFormat.format(date);
System.out.println(fecha);
    
answered by 13.12.2018 / 19:45
source