Android时间转换错误,IllegalArgumentException: Cannot format given Object as a Date

后台返回来的时间一般是String格式的,手机上显示一般不会把时分秒显示出来,就需要格式化了,今天在转换的时候大意了...一开始没经过两次转换,直接把String往SimpleDateFormat里面丢,导致错误,正确写法参考下面的例子,需要转换两次.,先把string时间放到SimpleDateFormat的parse

转成data之后再放到SimpleDateFormat里面format转换就没问题了.

      String temp = "2021-7-9 18:27:00";
                Date date = null;
                try {
                    date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(temp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String time = new SimpleDateFormat("yyyy-MM-dd").format(date);
                LogUtils.e("m_tag_time", "time:" + time);

猜你喜欢

转载自blog.csdn.net/qq_19714505/article/details/118612645