Java SimpleDateFormat parse result off by one hour (and yes, I set the time zone)

后端 未结 2 735
北恋
北恋 2020-12-17 21:08

Riddle me this: why does this simple JUnit assertion fail?

public void testParseDate() throws ParseException {
    final SimpleDateFormat formatter = new Sim         


        
2条回答
  •  渐次进展
    2020-12-17 21:31

    You also need to set up the timezone/daylight savings time for the calendar. Have a look at this snippet taken from the documentation:

     // create a Pacific Standard Time time zone
     SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
    
     // set up rules for daylight savings time
     pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
     pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
    
     // create a GregorianCalendar with the Pacific Daylight time zone
     // and the current date and time
     Calendar calendar = new GregorianCalendar(pdt);
    

提交回复
热议问题