网页制作试题及答案,福州百度推广排名优化,wordpress显示所有tag,肇庆建设银行招聘网站本来想偷懒百度一个时间字符串转UTC的代码#xff0c;但发现没有一个能用#xff0c;写得还复杂得要死#xff0c;没办法还是自己撸一个。/*** UTC时间字符串转本地时间字符串* 我的本地getDateTimeInstance()是格式#xff1a;yyyy-MM-dd HH:mm:ss* param str UTC时间字符…本来想偷懒百度一个时间字符串转UTC的代码但发现没有一个能用写得还复杂得要死没办法还是自己撸一个。/*** UTC时间字符串转本地时间字符串* 我的本地getDateTimeInstance()是格式yyyy-MM-dd HH:mm:ss* param str UTC时间字符串* return*/public static String dateUTC2LocalString(String str) {SimpleDateFormat utcDateFormat new SimpleDateFormat(yyyyMMddTHHmmssZ);//输入UTC时间格式utcDateFormat.setTimeZone(TimeZone.getTimeZone(UTC));SimpleDateFormat localFormater (SimpleDateFormat) DateFormat.getDateTimeInstance();//解决Date.toLocaleString()过时Date datenull;try {dateutcDateFormat.parse(str);} catch (Exception e) {e.printStackTrace();}return localFormater.format(date);}/*** 本地时间字符串转UTC时间字符串* 我的本地getDateTimeInstance()是格式yyyy-MM-dd HH:mm:ss* param str 本地时间字符串* return*/public static String localString2StringUTC(String str) {SimpleDateFormat utcFormat new SimpleDateFormat(yyyyMMddTHHmmssZ);//转化后UTC时间格式utcFormat.setTimeZone(TimeZone.getTimeZone(UTC));SimpleDateFormat localFormater (SimpleDateFormat) DateFormat.getDateTimeInstance();//解决Date.toLocaleString()过时Date datenull;try {datelocalFormater.parse(str);} catch (Exception e) {e.printStackTrace();}return utcFormat.format(date);}我的JDK版本是1.8所以Date.toLocaleString()方法过期了用了java.text.DateFormat的getDateTimeInstance()方法这个方法我自己的本地格式是yyyy-MM-dd HH:mm:ss。使用localString2StringUTC方法时如果本地时间字符串输入格式不匹配getDateTimeInstance方法的格式的话会报错可以用SimpleDateFormat写死时间格式来解决。注未做错误校验和优化。