将Android日期时间转换为MySQL日期时间
问题描述:
有什么办法可以从格式为MM-DD-YY'的android date和格式为HH:MM'的时间获取诸如'YYYY-MM-DD HH:MM:SS
'之类的日期时间格式存储为mysql中的日期时间格式
Is there any way I can get datetime format like 'YYYY-MM-DD HH:MM:SS
' to be stored as datetime format in mysql from android date of format 'MM-DD-YY
' and time of format 'HH:MM
'
输入: android日期"MM-DD-YY
"和android时间"HH:MM
"
输出: mysql datetime'YYYY-MM-DD HH:MM:SS
'
Input : android date 'MM-DD-YY
' and android time 'HH:MM
'
Output: mysql datetime 'YYYY-MM-DD HH:MM:SS
'
答
使用 SimpleDateFormat 解析和格式化日期:
Use SimpleDateFormat to parse and format the 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);