日历对象导哪个包_日期对象到日历[Java]
我有一部课堂电影其中我有一个开始日期、持续时间和结束日期。开始日期和停止日期是日期对象(私有日期开始日期…)(这是一项任务,所以我不能改变)现在,我想通过将持续时间(以分钟为单位)添加到开始日期来自动计算停止日期。
据我所知,不赞成使用日期的时间操作函数,因此这是一种糟糕的做法,但在另一方面,我看不到将日期对象转换为日历对象的方法,以便操作时间并将其重新转换为日期对象。有办法吗?如果有什么最佳实践
如果你经常使用日期,我觉得JoDA时间远比Java的日期和日历类好得多:JoDaTime.SooCurfGe.NET
分发日期/日历任务的人应该受到惩罚。-如果我处在你的位置,我会使用Java 7中添加的理智API。
类似问题:将日期对象转换为日历对象
您可以创建一个GregorianCalendar的实例,然后将Date设置为开始时间:
Date date;
Calendar myCal = new GregorianCalendar();
myCal.setTime(date);
然而,另一种方法是根本不使用Date。您可以使用这样的方法:
private Calendar startTime;
private long duration;
private long startNanos; //Nano-second precision, could be less precise
...
this.startTime = Calendar.getInstance();
this.duration = 0;
this.startNanos = System.nanoTime();
public void setEndTime() {
this.duration = System.nanoTime() - this.startNanos;
}
public Calendar getStartTime() {
return this.startTime;
}
public long getDuration() {
return this.duration;
}
。
通过这种方式,您可以访问开始时间和从开始到停止的持续时间。当然,精度由你决定。
Java's date/time implementation i s so shitty that I'm not sure what's worse-keeping jodatime-even though time computation i s a very small part of my AP's overall functionality-or switching to date/calendar and spending hours wading through the half-assed messed that should have been a well-implemented,solid component.
@Melllvar so true.现在你有第三个选择:爪哇.时间阶级建成8号和以后。For Java 6&Amp 7,much of the Java.Time functionality has been back-ported in the threeten-backport project.For earlier Android,see the threetenabp project.作为一项工作,该项目现在处于维护模式,设计Java.Time as its officially successor,its creator Stephen Coleborne has led both projects.
Calendar tCalendar = Calendar.getInstance();
tCalendar.setTime(date);
日期是java.util.date对象。您也可以使用calendar.getInstance()获取日历实例(效率更高)。
回到本地——具体的Calendar实施,因此通常更可取。
您不需要转换为Calendar,只需使用getTime()/setTime()。
getTime():
Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.
setTime(long time) : Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT. )
号
一秒钟有1000毫秒,一分钟有60秒。算算就行了。
Date now = new Date();
Date oneMinuteInFuture = new Date(now.getTime() + 1000L * 60);
System.out.println(now);
System.out.println(oneMinuteInFuture);
号
1000中的L后缀表示它是long字;这些计算通常很容易溢出int。
Calendar.setTime()。
查看API方法的签名和描述通常是有用的,而不仅仅是它们的名称:-即使在Java标准API中,名称有时也会误导。
DR
Instant stop =
myUtilDateStart.toInstant()
.plus( Duration.ofMinutes( x ) )
;
。java.time时间
其他答案都是正确的,尤其是鲍格华特的答案。但这些答案使用了过时的传统课程。
与JAVA绑定的原始日期时间类已经被Java.Times类取代。在java.time类型中执行业务逻辑。仅在需要使用尚未更新以处理java.time类型的旧代码时转换为旧类型。
如果你的Calendar实际上是GregorianCalendar,你可以转换成ZonedDateTime。查找添加到旧类中的新方法,以方便与java.time类型的转换。
if( myUtilCalendar instanceof GregorianCalendar ) {
GregorianCalendar gregCal = (GregorianCalendar) myUtilCalendar; // Downcasting from the interface to the concrete class.
ZonedDateTime zdt = gregCal.toZonedDateTime(); // Create `ZonedDateTime` with same time zone info found in the `GregorianCalendar`
end if
。
如果你的Calendar不是Gregorian的话,打电话给toInstant得到Instant的对象。Instant类表示UTC时间轴上的一个时刻,分辨率为纳秒。
Instant instant = myCal.toInstant();
同样,如果从java.util.Date对象开始,则转换为Instant。Instant类表示UTC时间轴上的一个时刻,分辨率为纳秒(小数点后最多九(9)位)。
Instant instant = myUtilDate.toInstant();
。
应用时区获取ZonedDateTime。
ZoneId z = ZoneId.of("America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );
要得到一个java.util.Date物体,请穿过Instant。
java.util.Date utilDate = java.util.Date.from( zdt.toInstant() );
。
有关在传统日期时间类型和java.time之间转换的更多讨论,以及一个漂亮的图表,请参阅我对另一个问题的回答。埃多克斯1〔25〕
将时间跨度表示为Duration对象。您输入的持续时间是问题中提到的分钟数。
Duration d = Duration.ofMinutes( yourMinutesGoHere );
号
您可以将其添加到开始以确定停止。
Instant stop = startInstant.plus( d );
号关于java.time
JavaTimeFr框架是在Java 8和之后构建的。这些类取代了麻烦的旧遗留日期时间类,如java.util.Date、Calendar和SimpleDateFormat。
现在处于维护模式的joda time项目建议迁移到java.time。
要了解更多信息,请参阅Oracle教程。以及搜索堆栈溢出以获得许多示例和解释。规格为JSR 310。
在哪里获取java.time类?
Java SE 8和SE 9及以后
内置。
标准JAVA API的一部分与捆绑实现。
Java 9增加了一些次要的特性和修复。
Java SE 6和SE 7
大部分JavaTimeActudio都被移植到TealEnter后端的Java 6和7中。
安卓
threetenabp项目专门为Android调整threeten backport(如上所述)。
看看如何使用……
threeten额外项目使用额外的类扩展java.time。这个项目是将来可能添加到java.time的一个试验场。您可以在这里找到一些有用的类,如Interval、YearWeek、YearQuarter等等。
以下是如何转换不同类型日期的完整示例:
Date date = Calendar.getInstance().getTime();
// Display a date in day, month, year format
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String today = formatter.format(date);
System.out.println("Today :" + today);
// Display date with day name in a short format
formatter = new SimpleDateFormat("EEE, dd/MM/yyyy");
today = formatter.format(date);
System.out.println("Today :" + today);
// Display date with a short day and month name
formatter = new SimpleDateFormat("EEE, dd MMM yyyy");
today = formatter.format(date);
System.out.println("Today :" + today);
// Formatting date with full day and month name and show time up to
// milliseconds with AM/PM
formatter = new SimpleDateFormat("EEEE, dd MMMM yyyy, hh:mm:ss.SSS a");
today = formatter.format(date);
System.out.println("Today :" + today);
号
此处使用的混乱日期-时间类(EDOCX1&2,EDOCX1&1)现在是遗产,被现代爪哇的几年前所取代。
有点像
movie.setStopDate(movie.getStartDate() + movie.getDurationInMinutes()* 60000);