简体   繁体   中英

Day light saving issue

Using following :

public static void main(String[] args) throws ParseException {
        System.out.println(convertDate("2017-03-12 02:00", true));
}

public static Date convertDate(final String dateStr, final Boolean isDateTime) throws ParseException{
          Date tmpDate = null;
          if ( dateStr == null || dateStr.isEmpty() ) {
                 return tmpDate;
          }
          String dateFormat = "yyyy-MM-dd";
          // LOGGER.debug("Input date string: " + dateStr);


          if (isDateTime) {
                 dateFormat = "yyyy-MM-dd HH:mm";
                 // TimeZone frTimeZone = TimeZone.getTimeZone("UTC");
                 // sdf.setTimeZone(frTimeZone);
          }
          SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);


          try {
                 tmpDate = sdf.parse(dateStr);
          } catch (ParseException e) {

          }
          return tmpDate;
}

output getting : Sun Mar 12 03:00:00 EDT 2017 expected output : Sun Mar 12 02:00:00 EDT 2017

second try :

public static Date convertDate(final String dateStr, final Boolean isDateTime) throws ParseException{
          Date tmpDate = null;
         String dateFormat = "dd-M-yyyy hh:mm:ss a";
          if (isDateTime) {
                 dateFormat = "yyyy-MM-dd HH:mm";
          }
        SimpleDateFormat sdfAmerica = new SimpleDateFormat(dateFormat);
        Date date = sdfAmerica.parse(dateStr);
       // TimeZone tz = TimeZone.getDefault();
        DateTime dt = new DateTime(date);
        DateTimeZone dtZone = DateTimeZone.forID("America/New_York");
        DateTime dtus = dt.withZone(dtZone);
        TimeZone tzInAmerica = dtZone.toTimeZone();
        Date dateInAmerica = dtus.toLocalDateTime().toDate(); //Convert to LocalDateTime first
        sdfAmerica.setTimeZone(tzInAmerica);
        System.out.println("dateInAmerica"+dateInAmerica);
        tmpDate=sdfAmerica.parse(dateStr);
        boolean inDaylightTime=tzInAmerica.inDaylightTime(tmpDate);
        if(inDaylightTime){
         Calendar cal = Calendar.getInstance(); 
         int hour=(tzInAmerica.getDSTSavings() / (1000 * 60 * 60));
          System.out.println("hour::::"+hour);
          cal.setTime(sdfAmerica.parse(dateStr));
          System.out.println("date:::"+cal.getTime());
          cal.add(Calendar.HOUR, hour);
          tmpDate=cal.getTime();
        }
        return tmpDate;
    }

output : Sun Mar 12 04:00:00 EDT 2017

However facing issue in getting correct output for date : 2017-03-12 02:00

In your first example you say you expected Sun Mar 12 02:00:00 EDT 2017 but got Sun Mar 12 03:00:00 EDT 2017 .

Please note that Daylight Savings were turned on on Mar 12th. So there should be no 2:00 AM EDT. When the clock is about to reach 2:00 AM it becomes 3:00 AM. So regardless of what your code is doing your expectation seems to be incorrect.

https://www.timeanddate.com/time/change/usa?year=2017

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM