请教一个android问题,在保存数据到SQlite数据库中时,怎么将mm/dd/yyyy
格式的日期转换为yyyy-mm-dd
这种格式?
谢谢。
SimpleDateFormat new_format= new SimpleDateFormat("yyyy-MM-dd);
SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yyyy");
Date date;
String local_date = null;
try{
date = sdf.parse(value);
local_date = new_format.format(date);
}catch(ParseException e)
{
e.printStackTrace();
}
试试下面的代码可以实现格式转换:
public static String convertDateStringFormat(String currentFormat,
String newFormat, String strDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat(currentFormat);
Date date = null;
try {
date = dateFormat.parse(strDate);
} catch (ParseException e) {
CommonFunctions.DoCatchOperation(e);
}
String newFormatString = convertDateToString(newFormat, date);
return newFormatString;
}
public static Date convertStringToDate(String dateFormatter, String strDate) {
SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatter);
Date date = null;
try {
date = dateFormat.parse(strDate);
} catch (ParseException e) {
CommonFunctions.DoCatchOperation(e);
}
return date;
}
public static String convertDateToString(String dateFormatter, Date date) {
if (date == null)
return "";
else {
SimpleDateFormat dFormat = new SimpleDateFormat(dateFormatter);
return dFormat.format(date);
}
}
额。。。有这个必要吗?
管他数据库里存的是什么格式 用户也不看它 显示的时候没问题不就好了
话说数据库里字段类型是DATETIME的话 难道不应该是任何格式的日期时间存进去都是一样的值么