Java 将天数添加到JodaTime Instant
我正在尝试编写一个简单实用的方法,将整数天数添加到Joda时间。这是我的第一次尝试Java 将天数添加到JodaTime Instant,java,datetime,jodatime,Java,Datetime,Jodatime,我正在尝试编写一个简单实用的方法,将整数天数添加到Joda时间。这是我的第一次尝试 /** * Adds a number of days specified to the instant in time specified. * * @param instant - the date to be added to * @param numberOfDaysToAdd - the number of days to be added to the instant specified *
/**
* Adds a number of days specified to the instant in time specified.
*
* @param instant - the date to be added to
* @param numberOfDaysToAdd - the number of days to be added to the instant specified
* @return an instant that has been incremented by the number of days specified
*/
public static Instant addNumberOfDaysToInstant(final Instant instant, final int numberOfDaysToAdd) {
Days days = Days.days(numberOfDaysToAdd);
Interval interval = new Interval(instant, days);
return interval.getEnd().toInstant();
}
这在大多数情况下都很好,除了当你考虑这个例子的时候,增加的天数使你跨越BST/GMT边界。这里有一个小例子
public class DateAddTest {
/**
*用于输入和输出的区域
*/
私有静态最终日期时区=DateTimeZone.forId(“欧洲/伦敦”)
}
我认为这个问题是因为间隔没有考虑到它已经跨越bst边界的事实。如果您有任何更好的方法来实现此功能,我们将不胜感激。如果您想处理日期,请不要使用瞬间。我怀疑这是正确的增加48小时的瞬间
改为使用LocalDate
,然后使用plusDays
方法
如果你想知道在一天中的同一时间,在指定的瞬间之后n天内发生的瞬间,我们无疑可以找到一种方法(将瞬间分为LocalDate
和LocalTime
,将LocalDate
提前,然后重新组合,或者检查LocalDateTime
是否符合您的要求)但是,如果原始时间在新的一天出现两次,或者根本没有出现,您需要确定希望发生什么
编辑:好的,所以你需要处理一个瞬间。它必须在原始时区吗?你能使用UTC吗?这将消除DST问题。如果不能,你希望它在不明确或不存在的情况下做什么(例如,在每次转换前的12:30)。假设你的其余代码:
public static void main(String[] args) {
DateTime dateTime = FORMATTER.parseDateTime("24/10/2009");
Instant pInstant = dateTime.withFieldAdded(DurationFieldType.days(),2).toInstant();
System.out.println("24/10/2009 + 2 Days = " + pInstant.toString(FORMATTER));
}
这就是所选择的解决方案
/**
* Zone to use for input and output
*/
private static final DateTimeZone ZONE = DateTimeZone.forId("Europe/London");
/**
* Adds a number of days specified to the instant in time specified.
*
* @param instant - the date to be added to
* @param numberOfDaysToAdd - the number of days to be added to the instant specified
* @return an instant that has been incremented by the number of days specified
*/
public static Instant addNumberOfDaysToInstant(final Instant instant, final int numberOfDaysToAdd) {
return instant.toDateTime(ZONE).withFieldAdded(DurationFieldType.days(), numberOfDaysToAdd).toInstant();
}
我知道在这种情况下,LocalDate比instant更合适,但由于其他限制,无法更改它。您希望从哪个时区创建instant参数?它是否一致?如果不以某种方式考虑该值,计算将毫无意义,因为您无法使用原始的修复建议这更简单:返回instant.toDateTime(ZONE).plusDays(numberOfDaysToAdd.toInstant();
/**
* Zone to use for input and output
*/
private static final DateTimeZone ZONE = DateTimeZone.forId("Europe/London");
/**
* Adds a number of days specified to the instant in time specified.
*
* @param instant - the date to be added to
* @param numberOfDaysToAdd - the number of days to be added to the instant specified
* @return an instant that has been incremented by the number of days specified
*/
public static Instant addNumberOfDaysToInstant(final Instant instant, final int numberOfDaysToAdd) {
return instant.toDateTime(ZONE).withFieldAdded(DurationFieldType.days(), numberOfDaysToAdd).toInstant();
}