Convert date and time in Android to mysql datetime

Is there a way to get the datetime format, for example ' YYYY-MM-DD HH:MM:SS ', which will be stored in datetime format in mysql from the android date of the format ' MM-DD-YY ' and the formatting time ' HH:MM '

Input: android ' MM-DD-YY ' date and android time HH:MM

Output: mysql datetime ' YYYY-MM-DD HH:MM:SS '

+4
source share
2 answers

Use SimpleDateFormat to parse and format dates:

 // Parse the input date SimpleDateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm"); Date inputDate = fmt.parse("10-22-2011 01:00"); // Create the MySQL datetime string fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateString = fmt.format(inputDate); 
+7
source

It is much easier for me to store dates as int in a database, for example. long millis - date.getTime() . Then, when you want to display it, you can convert it back to date and add any formatting according to your desired output.

-1
source

Source: https://habr.com/ru/post/1382037/


All Articles