我想将字符串转换为Date,然后将该日期格式更改为保存在Date Object中。日期将自动更改其格式。
try {
String starttime = "10:00AM";
SimpleDateFormat format = new SimpleDateFormat("hh:mma", Locale.ENGLISH);
Date startdat = format.parse(starttime);
Calendar calendar = Calendar.getInstance();
Calendar startcalendar = Calendar.getInstance();
startcalendar.setTime(startdat);
startcalendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
java.text.DateFormat simpleformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSSZ", Locale.US);
String **time**=simpleformat.format(startcalendar.getTime());
Log.e("taggg",String.valueOf(time));
}catch (Exception e){
e.printStackTrace();
Log.e("error",e.getMessage());
}
我希望时间变量以相同的格式保存在日期对象中。
答案 0 :(得分:0)
检查我已更新
只需更改此
java.text.DateFormat simpleformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
所以更新
try {
String starttime = "10:00AM";
SimpleDateFormat format = new SimpleDateFormat("hh:mma", Locale.ENGLISH);
Date startdat = format.parse(starttime);
Calendar calendar = Calendar.getInstance();
Calendar startcalendar = Calendar.getInstance();
startcalendar.setTime(startdat);
startcalendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
java.text.DateFormat simpleformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
String time=simpleformat.format(startcalendar.getTime());
Log.e("taggg",String.valueOf(time));
}catch (Exception e){
e.printStackTrace();
Log.e("error",e.getMessage());
}