【问题标题】:How to calculate a new date? - Using one date variable and a int variable [duplicate]如何计算新日期? - 使用一个日期变量和一个 int 变量 [重复]
【发布时间】:2019-06-24 14:20:44
【问题描述】:

场景

用户可以使用应用程序记录他们的许可证,他们输入许可证被兑换的日期,他们还输入许可证的长度(天)。

我想知道您是否能够从这两个变量中计算出新的日期。例如

days = INPUT FROM USER;          days = 7;      
dateRedeemed = new Date();       date = 24/06/2019;      
newDate = dateRedeemed + days;   newDate = 01/07/2019;    
//Getting the values
            String name = txtName.getText();
            String contact = txtContact.getText();
            int years = Integer.parseInt(cboyears.getSelectedItem().toString());
            int months = Integer.parseInt(cboMonths.getSelectedItem().toString());
            int days = Integer.parseInt(cboDays.getSelectedItem().toString());


//Calculation            
            days = (years * 365) + (months * 12) + days;
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY");
            Date dRedeemed = cboDate.getDate();
            String strRedeemed = format.format(dRedeemed);

如果有人能帮忙那就太好了

编辑

在这个帖子中有一些很好的建议,你们中的很多人一直指出 Date 类非常糟糕和过时,我现在要开始使用 LocalDateTime 类,因为它看起来更强大,我想问的另一件事是,是否有更有效的日期选择器。我一直在用一个swingx日期选择器有没有更有效的选择?

【问题讨论】:

  • 你使用的是 JDK 8 吗?
  • 从不使用 DateSimpleDateFormatCalendar 类。很久以前,它们被 java.time 包中的类所取代。
  • 我不认为LocalDateTime 是正确的类型。这看起来像是ZonedDateTime 的应用程序。

标签: java date


【解决方案1】:

Java(任何)解决方案:

Date currentDate = new Date(); // or any date you set
Calendar c = Calendar.getInstance();
c.setTime(currentDate);
c.add(Calendar.DAY_OF_MONTH, 7);
Date currentDatePlusSevenDays = c.getTime();

Java 8+ 解决方案:

Date currentDate = new Date(); // or any date you set
LocalDateTime localDateTime = currentDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
localDateTime = localDateTime.plusDays(7);
Date currentDatePlusSevenDays = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

带有外部库Joda-Time的Java(任何)解决方案:

DateTime currentDate = DateTime.now(); // or any date you set
DateTime currentDatePlusSevenDays = currentDate.plusDays(7);   

【讨论】:

  • LocalDateTime 是此处使用的错误的类,因为它不能代表片刻。简单的解决方案是:LocalDate.now().plusDays( 7 )
  • @BasilBourque 我不太确定您的评论是否正确,也许您会考虑根据您的逻辑编辑我的答案?
  • 至于我的评论,思考一下(A)一般说“23 日中午”和(B)说“东京 23 日中午”或“蒙特利尔 23 日中午”之间的区别”。至于解决方案,作者显然只对日期感兴趣,而不是一天中的时间,而不是片刻。所以正确的解决方案就是LocalDate.now( ZoneId.of( "Africa/Casablanca" ) ).plusDays( 7 )
【解决方案2】:

如果您使用 Java 8,则应使用 LocalDateTime 类。

这让事情变得如此简单。

LocalDateTime dateRedeemed = LocalDateTime.now(); // 2019-06-24T16:31:16.993
// custom date also possible (year, month, day (of month), hours, minutes, seconds and nanoseconds)
LocalDateTime dateRedeemed = LocalDateTime.of(2019, 06, 24, 13, 55, 36, 123);

// Add 7 days
LocalDateTime newDate = dateRedeemed.plusDays(7);
System.out.println(newDate.toString()); // 2019-07-01T16:31:16.993

【讨论】:

  • 我正在使用 jxDatePicker,因此用户可以选择它被兑换的日期,我将如何通过 LocalTimeDate 尝试解析它 LocalDateTime dateRedeemed = cboDate.getDate();但给出了 errorDate '不能转换为 LocalDateTime。'
  • LocalDateTime 正是当前要使用的错误的类。由于缺乏任何时区或与 UTC 偏移的概念,此类 不能代表时刻,如其 Javadoc 中所述。我想不出在任何情况下调用LocalDateTime.now() 是正确的做法。暂时使用InstantOffsetDateTimeZonedDateTime
【解决方案3】:

如果你不使用 Java 8,你可以使用下面的代码

int days = 8;
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DATE, days);

SimpleDateFormat format = new SimpleDateFormat("dd/MM/YYYY");
Date newDate = c.getTime();
System.out.println(dateFormat.format(newDate));

【讨论】:

  • 对于 Java 6 和 Java 7,最好使用 ThreeTen-Backport 项目中的 java.time 的后端端口。 DateCalendar 类真的很糟糕,应该避免。
  • @BasilBourque 我完全同意。我会从我身边推荐joda.org/joda-time。不幸的是,许多人/项目仍然使用旧的和糟糕的 DateCalendar
  • Joda-Time 项目现在处于维护模式,建议迁移到 java.time 类。这两个项目都由同一个人 Stephen Colebourne 领导。
  • 很高兴知道!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多