import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class TestMain {
public static String utc2Local(String utcTime, String format) throws Exception {
TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
SimpleDateFormat utcFormat = new SimpleDateFormat(format);
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));// 时区定义并进行时间获取
SimpleDateFormat localFormat = new SimpleDateFormat(format);
localFormat.setTimeZone(TimeZone.getDefault());
return localFormat.format(utcFormat.parse(utcTime).getTime());
}
public static void main(String[] args) throws Exception {
System.out.println(TestMain.utc2Local("20211029100407", "yyyyMMddHHmmss"));
}
}
输出结果:
当前时间:20211029120407
该博客内容展示了如何使用Java的SimpleDateFormat和TimeZone类将UTC时间转换为欧洲柏林的本地时间。主要方法`utc2Local`接收UTC时间和指定格式,然后进行时间转换。
1万+

被折叠的 条评论
为什么被折叠?



