java 将天数添加到 JodaTime Instant
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1058743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a number of days to a JodaTime Instant
提问by mR_fr0g
I'm trying to write a simple utility method for adding aninteger number of days to a Joda time instant. Here is my first stab.
我正在尝试编写一个简单的实用方法,用于向 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
* @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();
}
This works fine for the most part except when you consider the example when the number of days added takes you across the BST / GMT boundary. Here is a small example.
这在大多数情况下都可以正常工作,除非您考虑添加的天数跨越 BST/GMT 边界的示例。这是一个小例子。
public class DateAddTest {
/** * Zone to use for input and output */ private static final DateTimeZone ZONE = DateTimeZone.forId("Europe/London");
/** * 用于输入和输出的区域 */ private static final DateTimeZone ZONE = DateTimeZone.forId("Europe/London");
/**
* Formatter used to translate Instant objects to & from strings.
*/
private static final DateTimeFormatter FORMATTER = DateTimeFormat.forPattern(DATE_FORMAT).withZone(ZONE);
/**
* Date format to be used
*/
private static final String DATE_FORMAT = "dd/MM/yyyy";
public static void main(String[] args) {
DateTime dateTime = FORMATTER.parseDateTime("24/10/2009");
Instant toAdd = dateTime.toInstant();
Instant answer = JodaTimeUtils.addNumberOfDaysToInstant(toAdd, 2);
System.out.println(answer.toString(FORMATTER)); //25/10/2009
}
}
}
I think this problem is because the interval does not take into acount the fact that it has crossing the bst boundary. Any ideas of a better way to implement this would be appreciated.
我认为这个问题是因为间隔没有考虑它已经跨越了 bst 边界的事实。任何实现这一点的更好方法的想法都将不胜感激。
采纳答案by mR_fr0g
This is the solution that was chosen.
这是选择的解决方案。
/**
* 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();
}
回答by Jon Skeet
If you want to deal with dates, don't use instants. I suspect it's correctly adding 48 hours to the instant.
如果您想处理日期,请不要使用瞬间。我怀疑它正确地将 48 小时添加到了瞬间。
Use a LocalDateinstead, and then the plusDaysmethod.
使用 aLocalDate代替,然后使用plusDays方法。
If you want to know the instant that occurs n days after the specified instant, at the same time of day, we could no doubt work out a way of doing that (split the instant into a LocalDateand a LocalTime, advance the LocalDateand then reassemble, or check whether LocalDateTimedoes what you want) but you need to work out what you want to happen if the original time occurs twice on the new day, or doesn't occur at all.
如果您想知道在指定时刻后 n 天发生的时刻,在同一时间,我们无疑可以想出一种方法(将时刻拆分为 aLocalDate和 a LocalTime,推进LocalDate然后重新组合,或检查是否满足LocalDateTime您的要求)但是您需要弄清楚如果原始时间在新的一天出现两次,或者根本没有出现,您希望发生什么。
EDIT: Okay, so you need to work with an instant. Does that have to be in an original time zone? Could you use UTC? That would take away the DST issues. If not, what do you want it to do in cases of ambiguity or non-existence (e.g. at 12.30am before each of the transitions).
编辑:好的,所以你需要立即工作。必须在原始时区吗?你能用UTC吗?这将消除 DST 问题。如果没有,您希望它在模棱两可或不存在的情况下做什么(例如在每次转换之前的 12.30am)。
回答by Clint
Assuming the rest of your code:
假设您的代码的其余部分:
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));
}

