赞
踩
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.TimeZone;
-
-
- /**
- * Created by william on 2020/5/11.
- */
- public class UTCTimeFormatUtil {
-
- public static Date localToUTC(String localTime) throws Exception{
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- Date localDate= null;
-
- localDate = sdf.parse(localTime);
-
- long localTimeInMillis=localDate.getTime();
- /** long时间转换成Calendar */
- Calendar calendar= Calendar.getInstance();
- calendar.setTimeInMillis(localTimeInMillis);
- /** 取得时间偏移量 */
- int zoneOffset = calendar.get(java.util.Calendar.ZONE_OFFSET);
- /** 取得夏令时差 */
- int dstOffset = calendar.get(java.util.Calendar.DST_OFFSET);
- /** 从本地时间里扣除这些差量,即可以取得UTC时间*/
- calendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset));
- /** 取得的时间就是UTC标准时间 */
- Date utcDate=new Date(calendar.getTimeInMillis());
- return utcDate;
- }
-
- /**
- * utc时间转成local时间
- * @param utcTime
- * @return
- */
- public static Date utcToLocal(String utcTime) throws Exception{
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
- Date utcDate = null;
-
- utcDate = sdf.parse(utcTime);
- sdf.setTimeZone(TimeZone.getDefault());
- Date locatlDate = null;
- String localTime = sdf.format(utcDate.getTime());
- try {
- locatlDate = sdf.parse(localTime);
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return locatlDate;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。