How to convert UTC timestamp to device local time in android

后端 未结 9 1872
孤独总比滥情好
孤独总比滥情好 2020-11-29 21:24

I need to convert the UTC time stamp that i get from the server to local device time. currently i get 5 hrs difference in my time. for example when i post to server the post

9条回答
  •  情歌与酒
    2020-11-29 21:43

    Local to UTC

    DateTime dateTimeNew = new DateTime(date.getTime(),
    DateTimeZone.forID("Asia/Calcutta"));
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    String datetimeString = dateTimeNew.toString("yyyy-MM-dd HH:mm:ss");
    long milis = 0;
    try {
         milis = simpleDateFormat.parse(datetimeString).getTime();
    } catch (ParseException e) {
       e.printStackTrace();
    }
    

提交回复
热议问题