Convert UTC to current locale time

后端 未结 7 1109
时光取名叫无心
时光取名叫无心 2020-11-27 03:20

I am downloading some JSON data from a webservice. In this JSON I\'ve got some Date/Time values. Everything in UTC. How can I parse this date string so the result Date objec

7条回答
  •  萌比男神i
    2020-11-27 03:21

    So you want to inform SimpleDateFormat of UTC time zone:

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    TimeZone utcZone = TimeZone.getTimeZone("UTC");
    simpleDateFormat.setTimeZone(utcZone);
    Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));
    

    To display:

    simpleDateFormat.setTimeZone(TimeZone.getDefault());
    String formattedDate = simpleDateFormat.format(myDate);
    

提交回复
热议问题