简体   繁体   中英

ZonedDateTime timezone inconsistency issue?

I need to write one function which will create the future installments for the invoice. Below is the function which creates the list of future installment dates:-

public List<Date> getInstallmentDates(Invoice objectWithInvoiceDateField, int noOfInstallments, String instFreq)
{
    //objectWithInvoiceDateField.getInvoiceDate this will return java.util.Date instance
    ZonedDateTime invoiceDate = objectWithInvoiceDateField.getInvoiceDate.toInstant().atZone(ZoneId.systemDefault());
    ZonedDateTime firstInstallment = ZonedDateTime.of( invoiceDate.getYear(), invoiceDate.getMonthValue() , invoiceDate.getDayOfMonth() , 0 , 0 , 0, 0 , ZoneId.systemDefault());
    List<Date> installmentDates = new ArrayList();
    installmentDates.add(Date.from(firstInstallment.toInstant()));//First Installment
    /*Code for the subsequent installments*/
    for (int i = 1; i < noOfInstallments; i++) {
        ZonedDateTime subsequentInstallments = null;
        if(instFreq.equalsIgnoreCase("Quarterly")) {
            subsequentInstallments = firstInstallment.plusMonths(3*i);
        }
        else if(instFreq.equalsIgnoreCase("Semi-annual")){
            subsequentInstallments = firstInstallment.plusMonths(6*i);
        }
        else
            subsequentInstallments = firstInstallment.plusMonths(i);
        installmentDates.add(Date.from(subsequentInstallments.toInstant()));
    }
    return installmentDates;
}

This works as expected except for the last iteration. Below is the output if I run this method from main method for

getInstallmentDates(invoice, 5, "Monthly");

Thu Jul 30 00:00:00 EDT 2020
Sun Aug 30 00:00:00 EDT 2020
Wed Sep 30 00:00:00 EDT 2020
Fri Oct 30 00:00:00 EDT 2020
Mon Nov 30 00:00:00 ***EST*** 2020

Can some one please help me understand why the timezone for last instance is changed to EST? Thanks in advance!

Presumably because you have used the timezone to be ZoneId.systemDefault(), and your system defaults to a timezone that honours daylight saving time. Assuming EDT is Eastern Daylight Time and EST is Eastern Standard Time, in 2020 the end of daylight saving happens on 1 November and therefore the timezone name changes.

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