parsing date/time to localtimezone

前端 未结 2 1275
梦毁少年i
梦毁少年i 2020-12-22 12:01

I am trying to parse a date/time json from server side inside bind viewholder. The date string am trying to parse is this:

2018-06-25T08:06:52Z
2条回答
  •  遥遥无期
    2020-12-22 12:22

    The time set to the text view is the same as before parsing.

    This is because you're not passing in a new SimpleDateFormat.

    Try this:

    final String serverDate = "2018-06-25T08:06:52Z"
    final SimpleDateFormat serverDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    final SimpleDateFormat yourDateFormat = new SimpleDateFormat("yyyy-MM-dd"); // put whatever you want here!
    
    try {
        final Date date = serverDate.parse(serverDateFormat);
        final String formattedDate = yourDateFormat.format(date);
    } catch (ParseException e) {
        System.out.println(e.toString());
    }
    

    This way you're interpreting the String with the server format into a pure Date object. You're then free to do what you wish with that object and turn it into whichever format you want (by providing your new format).

    For more information on building your date format pattern, see this link.

提交回复
热议问题