本文主要是介绍java实现本地时间和格林威治时间互换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
public static String getUTCTimeByLocalTime(String localDate, String format) {Calendar cal = Calendar.getInstance();// 获得时区和 GMT-0 的时间差,偏移量int offset = cal.get(Calendar.ZONE_OFFSET);// 获得夏令时 时差int dstoff = cal.get(Calendar.DST_OFFSET);Date date = new Date(getMillSecond(format, localDate) - (offset + dstoff));// 获得当前是UTC时区的时间毫秒值String formatDate = format(date, format);return formatDate;}public static String getLocalTimeByUTCTime(String utcDate, String format) {Calendar cal = Calendar.getInstance();// 获得时区和 GMT-0 的时间差,偏移量int offset = cal.get(Calendar.ZONE_OFFSET);// 获得夏令时 时差int dstoff = cal.get(Calendar.DST_OFFSET);Date date = new Date(getMillSecond(format, utcDate) + (offset + dstoff));// 获得当前是UTC时区的时间毫秒值String formatDate = format(date, format);return formatDate;}public static long getMillSecond(String format, String time) {Date parse = parse(time, format);return parse.getTime();}public static Date parse(String strDate, String pattern) {SimpleDateFormat df = new SimpleDateFormat(pattern);try {return df.parse(strDate);} catch (Exception e) {e.printStackTrace();return null;}}
这篇关于java实现本地时间和格林威治时间互换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!