java普通时间与UTC之间的相互转换

//普通时间转为UTC
public static String localToUTC(String localTimeStr) {
    try {
        Date localDate = getLocalSDF().parse(localTimeStr);
        return getUTCSDF().format(localDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}
//UTC转为普通时间
public static String utcToLocal(String utcTimeStr) {
    try {
        Date date = getUTCSDF().parse(utcTimeStr);
        return getLocalSDF().format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return null;
}

private static SimpleDateFormat getLocalSDF() {
    return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}

private static SimpleDateFormat getUTCSDF() {
    SimpleDateFormat utcSDF = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    utcSDF.setTimeZone(TimeZone.getTimeZone("UTC"));
    return utcSDF;
}

版权声明:本文为weixin_42683274原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。