java - 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 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 date : 2017-03-12 02:00

in first example expected sun mar 12 02:00:00 edt 2017 got sun mar 12 03:00:00 edt 2017.

please note daylight savings turned on on mar 12th. there should no 2:00 edt. when clock reach 2:00 becomes 3:00 am. regardless of code doing expectation seems incorrect.

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


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -