美文网首页
2021-04-17 RTC 获取RTC时间 字符串转日期 系统

2021-04-17 RTC 获取RTC时间 字符串转日期 系统

作者: fjasmin | 来源:发表于2021-04-17 19:58 被阅读0次

虽然自己也是老司机 。不过最近翻车。在系统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就会出现。
谨记谨记!!!

相关文章

网友评论

      本文标题:2021-04-17 RTC 获取RTC时间 字符串转日期 系统

      本文链接:https://www.haomeiwen.com/subject/ghdplltx.html