How to handle dates when the input dates are in the transition period from PDT to PST?

前端 未结 2 1332
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 00:49

I am calling an API which takes two dates as input.The API checks if the difference between the two date is greater than 60 min, then it throws an exception.My input dates a

2条回答
  •  误落风尘
    2020-12-12 01:41

    What you can do here get the difference between the 2 dates using timezone offset. something like below

    private int getDSTdifferenceDateAdjustment(Date startDate, Date endDate, TimeZone timeZone)
    {
        if (startDate == null || endDate == null) return 0;
        int baseOffset = timeZone.getOffset(startDate.getTime());
        int newOffSet = timeZone.getOffset(endDate.getTime());
    
        return (newOffSet - baseOffset);
    }
    

    Have something like this in your method

    int dstDifference = getDSTdifferenceDateAdjustment(startdate, enddate, TimeZone.getDefault());
    // The dstDifference will get in the negative, so we are adding to the dateRange variable
    dateRange += dstDifference;
    

    Try this one and even check when the DST starts next year. Mostly this will work in all these cases

提交回复
热议问题