Converting UTC dates to other timezones

后端 未结 5 554
礼貌的吻别
礼貌的吻别 2020-11-29 21:55

I\'m converting a UTC time to another timezone, using this method:

SimpleDateFormat format = new SimpleDateFormat(\"yyyy-MM-dd HH:mm:ss\");
Date parsed = for         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-29 22:21

    You can Parse your date format and time zone according to your requirements. Try this snippet of code i hope it helpful for you.

    private String getFormattedDate(String OurDate) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); // According to your Server TimeStamp
            formatter.setTimeZone(TimeZone.getTimeZone("UTC")); //your Server Time Zone
            Date value = formatter.parse(OurDate); // Parse your date
    
            SimpleDateFormat dateFormatter = new SimpleDateFormat("MM-dd-yyyy"); //this format changeable according to your choice
            dateFormatter.setTimeZone(TimeZone.getDefault());
            OurDate = dateFormatter.format(value);
    
        } catch (Exception e) {
            OurDate = "00-00-0000 00:00";
    
        }
        return OurDate;
    }
    

提交回复
热议问题