时区一致性问题?

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

ZonedDateTime timezone inconsistency issue?

问题

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

  1. public List<Date> getInstallmentDates(Invoice objectWithInvoiceDateField, int noOfInstallments, String instFreq) {
  2. // objectWithInvoiceDateField.getInvoiceDate将返回java.util.Date实例
  3. ZonedDateTime invoiceDate = objectWithInvoiceDateField.getInvoiceDate.toInstant().atZone(ZoneId.systemDefault());
  4. ZonedDateTime firstInstallment = ZonedDateTime.of(invoiceDate.getYear(), invoiceDate.getMonthValue(), invoiceDate.getDayOfMonth(), 0, 0, 0, 0, ZoneId.systemDefault());
  5. List<Date> installmentDates = new ArrayList();
  6. installmentDates.add(Date.from(firstInstallment.toInstant()));//第一笔分期付款
  7. /*后续分期付款的代码*/
  8. for (int i = 1; i < noOfInstallments; i++) {
  9. ZonedDateTime subsequentInstallments = null;
  10. if (instFreq.equalsIgnoreCase("Quarterly")) {
  11. subsequentInstallments = firstInstallment.plusMonths(3 * i);
  12. } else if (instFreq.equalsIgnoreCase("Semi-annual")) {
  13. subsequentInstallments = firstInstallment.plusMonths(6 * i);
  14. } else
  15. subsequentInstallments = firstInstallment.plusMonths(i);
  16. installmentDates.add(Date.from(subsequentInstallments.toInstant()));
  17. }
  18. return installmentDates;
  19. }

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

  1. getInstallmentDates(invoice, 5, "Monthly");

输出如下:

  1. Thu Jul 30 00:00:00 EDT 2020
  2. Sun Aug 30 00:00:00 EDT 2020
  3. Wed Sep 30 00:00:00 EDT 2020
  4. Fri Oct 30 00:00:00 EDT 2020
  5. 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 :-

  1. public List&lt;Date&gt; getInstallmentDates(Invoice objectWithInvoiceDateField, int noOfInstallments, String instFreq)
  2. {
  3. //objectWithInvoiceDateField.getInvoiceDate this will return java.util.Date instance
  4. ZonedDateTime invoiceDate = objectWithInvoiceDateField.getInvoiceDate.toInstant().atZone(ZoneId.systemDefault());
  5. ZonedDateTime firstInstallment = ZonedDateTime.of( invoiceDate.getYear(), invoiceDate.getMonthValue() , invoiceDate.getDayOfMonth() , 0 , 0 , 0, 0 , ZoneId.systemDefault());
  6. List&lt;Date&gt; installmentDates = new ArrayList();
  7. installmentDates.add(Date.from(firstInstallment.toInstant()));//First Installment
  8. /*Code for the subsequent installments*/
  9. for (int i = 1; i &lt; noOfInstallments; i++) {
  10. ZonedDateTime subsequentInstallments = null;
  11. if(instFreq.equalsIgnoreCase(&quot;Quarterly&quot;)) {
  12. subsequentInstallments = firstInstallment.plusMonths(3*i);
  13. }
  14. else if(instFreq.equalsIgnoreCase(&quot;Semi-annual&quot;)){
  15. subsequentInstallments = firstInstallment.plusMonths(6*i);
  16. }
  17. else
  18. subsequentInstallments = firstInstallment.plusMonths(i);
  19. installmentDates.add(Date.from(subsequentInstallments.toInstant()));
  20. }
  21. return installmentDates;
  22. }

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");

  1. Thu Jul 30 00:00:00 EDT 2020
  2. Sun Aug 30 00:00:00 EDT 2020
  3. Wed Sep 30 00:00:00 EDT 2020
  4. Fri Oct 30 00:00:00 EDT 2020
  5. 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:

确定