How to set time zone of a java.util.Date?

后端 未结 10 1999
我寻月下人不归
我寻月下人不归 2020-11-22 01:45

I have parsed a java.util.Date from a String but it is setting the local time zone as the time zone of the date object.

The ti

10条回答
  •  感动是毒
    2020-11-22 02:09

    Here you be able to get date like "2020-03-11T20:16:17" and return "11/Mar/2020 - 20:16"

     private String transformLocalDateTimeBrazillianUTC(String dateJson) throws  ParseException {
        String localDateTimeFormat = "yyyy-MM-dd'T'HH:mm:ss";
        SimpleDateFormat formatInput = new SimpleDateFormat(localDateTimeFormat);
    
        //Here is will set the time zone
        formatInput.setTimeZone(TimeZone.getTimeZone("UTC-03"));
    
        String brazilianFormat = "dd/MMM/yyyy - HH:mm";
        SimpleDateFormat formatOutput = new SimpleDateFormat(brazilianFormat);
        Date date = formatInput.parse(dateJson);
        return formatOutput.format(date);
    }
    

提交回复
热议问题