Convert Java UTC time to normal Beijing time (+8 hours)

public class TimeUtils {


    /**
     * @param utcTime UTC时间字符串
     * @param utcFormat UTC时间格式
     * @return String
     */
    public static String transferUtcTime(String utcTime,String utcFormat) throws ParseException {
        SimpleDateFormat sdf1 = new SimpleDateFormat(utcFormat);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf1.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date date = sdf1.parse(utcTime);
        return sdf2.format(date );
    }

    public static void main(String[] args) throws ParseException {
        //utcTime 的时间格式一定要 和 utcFormat的时间格式保持一致
        System.out.println(transferUtcTime("2023-07-11T06:23:58Z","yyyy-MM-dd'T'HH:mm:ss'Z'"));
    }

}

The above is an example of converting UTC time to Beijing time for your reference!

Guess you like

Origin blog.csdn.net/ddwangbin520/article/details/131662917