有没有办法从格式为'MM-DD-YY
'的 Android 日期和格式为'HH:MM
'的时间得到像'YYYY-MM-DD HH:MM:SS
'这样的日期时间格式并将其存储为 MySQL 中的日期时间格式?
输入:Android 日期“MM-DD-YY
”和 Android 时间“HH:MM
”
输出:MySQL 日期时间“YYYY-MM-DD HH:MM:SS
”
使用SimpleDateFormat来解析和格式化日期:
// 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);
我发现在数据库中将日期存储为整数更容易,例如long millis - date.getTime()
。然后当您想要显示它时,可以将其转换回日期并添加适合所需输出的任何格式。