Convert UTC date to current timezone

前端 未结 5 1143
广开言路
广开言路 2021-01-27 02:51

I have to convert a UTC date in this format \"2016-09-25 17:26:12\" to the current time zone of Android. I did this:

SimpleDateFormat simpleDateFormat = new Simp         


        
5条回答
  •  时光说笑
    2021-01-27 03:28

    it print GMT+02 because this is your "local" timezone. if you want to print the date without timezone information, use SimpleDateFormat to format the date to you liking.

    edit : adding the code example (with your variable 'myDate')

    SimpleDateFormat inputSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    inputSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date myDate = inputSDF.parse("2016-09-25 17:26:12");
    //
    SimpleDateFormat outputSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println(outputSDF.format(myDate));
    System.out.println(TimeZone.getDefault().getID());
    

    yield on the (my) console (with my local timezone).

    2016-09-25 19:26:12
    Europe/Paris
    

提交回复
热议问题