Java 从 LocalDateTime Bar 中减去 LocalDateTime foo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40490323/
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
Java Subtracting LocalDateTime foo from LocalDateTime Bar
提问by Jonnyr612
I know this is probably a really stupid question, however I was wondering whether there is a way to minus a local date time from another, or if not, to cast localDateTime to just Date time and then subtract from there?
我知道这可能是一个非常愚蠢的问题,但是我想知道是否有办法从另一个地方减去本地日期时间,或者如果没有,将 localDateTime 强制转换为日期时间,然后从那里减去?
I need to be able to work out the date time difference, between the start of record foo and the start of record bar (which is also the end of Foo)
我需要能够计算出记录 foo 的开始和记录栏的开始(也是 Foo 的结束)之间的日期时间差
I am rather new to java, and for simplicity's sake, I want to be able to keep it in roughly the same layout, rather than having to convert from seconds etc.
我对java比较陌生,为了简单起见,我希望能够将它保持在大致相同的布局中,而不必从秒等进行转换。
If adding and subtracting LocalDateTime is not an option, I know that once I am in dateTime I am able to subtract them using:
如果添加和减去 LocalDateTime 不是一个选项,我知道一旦我进入 dateTime 我就可以使用以下方法减去它们:
Period diff = new Period(start, end);
Due to the purpose of the data, I need to keep both the date and the time in order for this to work, and as mentioned before, I wish to keep it fairly simple so theres no need to convert between seconds back to the date.
由于数据的目的,我需要保留日期和时间才能使其正常工作,并且如前所述,我希望保持相当简单,因此无需在秒之间转换回日期。
My issue is really just getting it in an acceptable format, as I say, I am rather new to java and object orientation as a whole, so please don't ridicule me for this, seemingly simple, question.
我的问题实际上只是以一种可接受的格式获取它,正如我所说,我对 Java 和面向对象的整体而言还很陌生,所以请不要因为这个看似简单的问题而嘲笑我。
Thanks,
谢谢,
Jonny
强尼
采纳答案by Meno Hochschild
Why do you not simply use directly LocalDateTime
as parameter type this way:
为什么你不直接用LocalDateTime
这种方式作为参数类型:
LocalDateTime start = new LocalDateTime(2016, 10, 4, 17, 45);
LocalDateTime end = LocalDateTime.now();
PeriodType ptype = PeriodType.yearMonthDayTime();
Period diff = new Period(start, end, ptype);
See also the API of Joda-Time. You don't need to cast to DateTime
.
回答by Olivier Grégtheitroade
The question is about Joda-Time, not about Java 8. The question wasn't really clear about it. Anyways, this answer is about Java 8 and therefore doesn't directly answers the question. Though I think it's an appropriate answer if anyone wants to use Java 8 instead.
问题是关于 Joda-Time,而不是关于 Java 8。这个问题并不是很清楚。无论如何,这个答案是关于 Java 8 的,因此没有直接回答这个问题。虽然我认为如果有人想改用 Java 8,这是一个合适的答案。
You should probably check the java.time.Duration
class.
您可能应该检查一下java.time.Duration
课程。
LocalDateTime from = ... ;
LocalDateTime to = ... ;
Duration duration = Duration.between(from, to);
If you really want to obtain a Period
, it's still rather easy:
如果你真的想获得一个Period
,它仍然很容易:
LocalDateTime from = ... ;
LocalDateTime to = ... ;
Period period = Period.between(from.toLocalDate(), to.toLocalDate());