日期对象到日历 [Java]
我有一堂课电影 其中我有一个开始日期、持续时间和停止日期。 开始和停止日期是日期对象(private Date startDate ...) (这是一项任务,所以我无法更改) 现在我想通过将持续时间(以分钟为单位)添加到开始日期来自动计算停止日期。
据我所知,使用 Date 的时间操作函数已被弃用,因此是不好的做法,但另一方面,我认为没有办法将 Date 对象转换为日历对象来操作时间并将其重新转换为 Date 对象。 有办法吗?如果有什么是最佳实践
I have a class Movie
in it i have a start Date, a duration and a stop Date.
Start and stop Date are Date Objects (private Date startDate ...)
(It's an assignment so i cant change that)
now I want to automatically calculate the stopDate by adding the duration (in min) to the startDate.
By my knowledge working with the time manipulating functions of Date is deprecated hence bad practice but on the other side i see no way to convert the Date object to a calendar object in order to manipulate the time and reconvert it to a Date object.
Is there a way? And if there is what would be best practice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以做的是创建
GregorianCalendar
的实例,然后将Date
设置为开始时间:但是,另一种方法是不使用
Date
> 完全没有。您可以使用这样的方法:通过这种方式,您可以访问开始时间并获取从开始到停止的持续时间。当然,精度取决于你。
What you could do is creating an instance of a
GregorianCalendar
and then set theDate
as a start time:However, another approach is to not use
Date
at all. You could use an approach like this:In this way you can access both the start time and get the duration from start to stop. The precision is up to you of course.
date 是一个 java.util.Date 对象。您也可以使用 Calendar.getInstance() 来获取 Calendar 实例(效率更高)。
date is a java.util.Date object. You may use Calendar.getInstance() as well to obtain the Calendar instance(much more efficient).
日历.setTime()
查看 API 方法的签名和描述,而不仅仅是它们的名称,通常很有用:) - 即使在 Java 标准 API 中,名称有时也可能会产生误导。
Calendar.setTime()
It's often useful to look at the signature and description of API methods, not just their name :) - Even in the Java standard API, names can sometimes be misleading.
您不需要为此转换为
Calendar
,只需使用getTime()
/setTime()
即可。一秒有 1000 毫秒,一分钟有 60 秒。只要算一下就可以了。
1000
中的L
后缀表示它是一个long
文字;这些计算通常很容易溢出int
。You don't need to convert to
Calendar
for this, you can just usegetTime()
/setTime()
instead.There are 1000 milliseconds in a second, and 60 seconds in a minute. Just do the math.
The
L
suffix in1000
signifies that it's along
literal; these calculations usually overflowsint
easily.tl;dr
java.time
其他答案是正确的,尤其是 Borgwardt 的答案。但这些答案使用过时的遗留类。
与 Java 捆绑在一起的原始日期时间类已被 java.time 类取代。在 java.time 类型中执行业务逻辑。仅在需要使用尚未更新以处理 java.time 类型的旧代码时转换为旧类型。
如果您的
日历
实际上是一个GregorianCalendar
您可以转换为ZonedDateTime
。查找添加到旧类中的新方法,以方便与 java.time 类型之间的转换。如果您的
日历
不是公历
,请调用toInstant
获取Instant
对象。即时
类代表 UTC 中时间轴上的时刻,分辨率为 纳秒。同样,如果从
java.util.Date
对象开始,请转换为Instant
。即时
类代表 UTC 中时间轴上的时刻,分辨率为 纳秒(最多九 (9) 位小数)。应用时区来获取
ZonedDateTime< /代码>
。
要获取
java.util.Date
对象,请通过Instant
。有关旧日期时间类型和 java.time 之间转换的更多讨论以及漂亮的图表,请参阅我的回答另一个问题。
Duration
将时间跨度表示为
Duration
对象。您输入的持续时间是问题中提到的分钟数。您可以将其添加到开始位置以确定停止位置。
关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
java.time
Other Answers are correct, especially the Answer by Borgwardt. But those Answers use outmoded legacy classes.
The original date-time classes bundled with Java have been supplanted with java.time classes. Perform your business logic in java.time types. Convert to the old types only where needed to work with old code not yet updated to handle java.time types.
If your
Calendar
is actually aGregorianCalendar
you can convert to aZonedDateTime
. Find new methods added to the old classes to facilitate conversion to/from java.time types.If your
Calendar
is not aGregorian
, calltoInstant
to get anInstant
object. TheInstant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.Similarly, if starting with a
java.util.Date
object, convert to anInstant
. TheInstant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).Apply a time zone to get a
ZonedDateTime
.To get a
java.util.Date
object, go through theInstant
.For more discussion of converting between the legacy date-time types and java.time, and a nifty diagram, see my Answer to another Question.
Duration
Represent the span of time as a
Duration
object. Your input for the duration is a number of minutes as mentioned in the Question.You can add that to the start to determine the stop.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.在 Kotlin 中将日期转换为日历的扩展。
Extension for converting date to calendar in Kotlin.
类似的东西
something like
以下是有关如何将日期转换为不同类型的完整示例:
Here is a full example on how to transform your date in different types: