虽然自己也是老司机 。不过最近翻车。在系统RTC时间设置的时候。发现定时开关机不能重启。在RTC时间转化的时候差12小时
如下所示:
2017-01-01 20:44:30.211 1776-1776/com.DeviceTest E/StrToDate str:: 2017/1/1 12:44:30
2017-01-01 20:44:30.212 1776-1776/com.DeviceTest E/StrToDate date:: Sun Jan 01 00:44:30 GMT+08:00 2017
后来发现解决问题的是 :
/**
* 字符串转换成日期
* @param str
* @return date
*/
public static Date StrToDate(String str) {
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US);
format.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
Date date = null;
try {
format.setLenient(false);//禁止宽松格式
Log.e("StrToDate str:",str);
date = format.parse(str);
Log.e("StrToDate date:",date.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
这里面最要注意的就是格式: 应该为yyyy-MM-dd HH:mm:ss
中间的HH
如果写成hh
就会出现。
谨记谨记!!!
网友评论