【问题标题】:How to update the date value alone in a DateTime variable如何在 DateTime 变量中单独更新日期值
【发布时间】:2014-03-04 15:03:27
【问题描述】:

我有一个数据类型为 DateTime (org.joda.time.DateTime) 的变量。我需要在不修改时间的情况下更新变量的日期值吗?

是否可以在不修改时间的情况下向 DateTime 变量添加天数? [就像我们使用 DateUtils.addDays() 方法对 Date (org.apache.commons.lang.time.DateUtils) 变量所做的那样]

谢谢, 萨桑克

【问题讨论】:

  • 你检查过javadoc吗?

标签: java datetime


【解决方案1】:

你考虑过使用

http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html#withDate%28int,%20int,%20int%29

来自 JavaDocs

withDate

public DateTime withDate(int year, int monthOfYear, int dayOfMonth)

Returns a copy of this datetime with the specified date, retaining the time fields.

If the date is already the date passed in, then this is returned. 

【讨论】:

    【解决方案2】:
    DateTime d = new DateTime();
    DateTime fiveDaysLater = d.plusDays(5);
    

    这会使您的时间增加 5 天。我希望这就是您正在寻找的。​​p>

    同样适用于 plusMonths 和更多请参阅:http://www.joda.org/joda-time/apidocs/org/joda/time/DateTime.html

    【讨论】:

      猜你喜欢
      • 2020-09-16
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多