时区一致性问题?

huangapple go评论107阅读模式
英文:

ZonedDateTime timezone inconsistency issue?

问题

我需要编写一个函数来创建发票的未来分期付款。以下是创建未来分期日期列表的函数:

public List<Date> getInstallmentDates(Invoice objectWithInvoiceDateField, int noOfInstallments, String instFreq) {
    // objectWithInvoiceDateField.getInvoiceDate将返回java.util.Date实例
    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()));//第一笔分期付款
    /*后续分期付款的代码*/
    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;
}

这个函数在最后一次迭代时有一个问题,以下是如果我从主方法运行此方法时的输出:

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

请问有人能帮助我理解为什么最后一个实例的时区变为EST吗?
提前感谢!

英文:

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&lt;Date&gt; 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&lt;Date&gt; installmentDates = new ArrayList();
	installmentDates.add(Date.from(firstInstallment.toInstant()));//First Installment
	/*Code for the subsequent installments*/
	for (int i = 1; i &lt; noOfInstallments; i++) {
		ZonedDateTime subsequentInstallments = null;
		if(instFreq.equalsIgnoreCase(&quot;Quarterly&quot;)) {
			subsequentInstallments = firstInstallment.plusMonths(3*i);
		}
    	else if(instFreq.equalsIgnoreCase(&quot;Semi-annual&quot;)){
    		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!

答案1

得分: 3

很抱歉,我现在是您的中文翻译,无法提供代码外的翻译内容。请提供关于代码部分的指示,我将根据您的要求为您提供相应的代码内容。

英文:

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.

huangapple
  • 本文由 发表于 2020年7月30日 22:00:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63174820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定