1. 程式人生 > >Illegal instant due to time zone offset transition (Asia/Shanghai)_夏令時問題

Illegal instant due to time zone offset transition (Asia/Shanghai)_夏令時問題

pad mat ati idcard formatter sta 報錯信息 目前 color

項目報錯信息:

Connot parse "1991-04-14",illegal instant due to time zone offset transition(Asia/Shanghai)

在網上查了一下說是由於夏令時問題引起了,那麽什麽是夏令時呢?

夏時制,夏時令(Daylight Saving Time:DST),又稱“日光節約時制”和“夏令時間”,是一種為節約能源而人為規定地方時間的制度,在這一制度實行期間所采用的統一時間稱為“夏令時間”。一般在天亮早的夏季人為將時間調快一小時,可以使人早起早睡,減少照明量,以充分利用光照資源,從而節約照明用電。各個采納夏時制的國家具體規定不同。目前全世界有近110個國家每年要實行夏令時。我國1986年4月中央頒布了相關政策, 1992年起,夏令時暫停實行。 夏令時開始:每年從四月中旬第一個星期日
的淩晨2時整(北京時間),將時鐘撥快一小時,即將表針由2時撥至3時; 夏令時結束:直到九月中旬第一個星期日的淩晨2時整(北京夏令時),再將時鐘撥回一小時,即將表針由2時撥至1時。 具體時段:【1986-05-04(第一年特殊化)至1986-09-14, 1987-04-12至1987-09-13, 1988-04-10至1988-09-11, 1989-04-16至1989-09-17, 1990-04-15至1990-09-16, 1991-04-14至1991-09-15】。 夏令時對編程的影響: 有一些日期轉換的類(org.joda.time包下的類)一碰到夏令時開始日期(1986-05-04,1987-04-12 ,1988-04-10,1989-04-16,1990-04-15,1991-04-14)就會報錯

import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public static Date getBirthdayByIdCardNo(String IDCardNo) {
        Date birthday = null;
        if (StringUtils.isNotBlank(IDCardNo)) {
            int length = IDCardNo.length();
            
if (length == 15 || length == 18) { String strBirthday = null; if (length == 18) { //7—14位出生年月日 strBirthday = IDCardNo.substring(6, 14); } else { //7-12位出生年月日,比如670401代表1967年4月1日 strBirthday = StringUtils.leftPad(IDCardNo.substring(6, 12), 8, "19"); }
//DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyyMMdd");會報錯
//birthday = LocalDate.parse(strBirthday, formatter).toDate();會報錯

          //SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//所有的日期轉換都要經過Calendar類,所以是線程不安全的。

//date = df.parse("2008-08-08");

          birthday = DateUtils.parse(strBirthday, "yyyyMMdd");

}

        }
        return birthday;
    }
import java.util.Date;
import java.util.regex.Pattern;

import org.joda.time.DateTime;
public class DateUtils {

    public static final Pattern DATE_PATTERN = Pattern.compile("^\\d{4}-\\d{1,2}-\\d{1,2}$");

    public static Date parse(String time, String format) {
        return DateTimeUtils.toDate(DateTimeUtils.parse(time, format));
    }
}


import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

public class DateTimeUtils {
    
    public static Date toDate(DateTime dateTime){
        return dateTime == null ? null : dateTime.toDate();
    }

     public static DateTime parse(String time, String format) {
        if(StringUtils.isBlank(time)){
            return null;
        }
        return DateTime.parse(time,       
                                         DateTimeFormat.forPattern(format));
    }
        
}

    



解決方案: 在啟動類*Applation.java 中加入 System.setProperty("user.timezone", "Etc/GMT-8"); 友情鏈接:https://my.oschina.net/u/3871554/blog/1859717(講述為何使用joda 包而非SimpleDateFormat)

Illegal instant due to time zone offset transition (Asia/Shanghai)_夏令時問題