How to Format ISO-8601 in Java

前端 未结 3 890
北海茫月
北海茫月 2020-12-04 00:00

I am trying to change from standard ISO 8601 format 2014-09-11T21:28:29.429209Z into a nice MMM d yyyy hh:mm z format, however my current code is failing.

3条回答
  •  误落风尘
    2020-12-04 00:21

    public void setCreatedAt(String dateTime) {
        SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault());
        SimpleDateFormat sdfTarget = new SimpleDateFormat("MMM d yyyy hh:mm a z", Locale.getDefault());
        try {
            Date date = sdfSource.parse(dateTime);
            String createdAt = sdfTarget.format(date);
            Log.e(TAG, "setCreatedAt: createdAt " + createdAt);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    

提交回复
热议问题