简体   繁体   English

Java Calendar对象,分别设置日期和时间

[英]Java Calendar object, setting date and time separately

When creating a calendar object and setting the date/time using SimpleDateFormat to parse a string, is it possible to set the date and time in two separate lines of code? 创建日历对象并使用SimpleDateFormat设置日期/时间以解析字符串时,是否可以在两行代码中设置日期和时间? For example, in my SQLite db the date (mm-dd-yyyy) is stored in a separate column from the time (hh:mm). 例如,在我的SQLite数据库中,日期(mm-dd-yyyy)与时间(hh:mm)存储在单独的列中。 Is it kosher to do something like the following: 做下面的事情是犹太洁食:

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdfDate = new SimpleDateFormat("MM-dd-yyyy");
SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm zzz");
cal.setTime(sdfDate.parse(DATE));
cal.setTime(sdfTime.parse(TIME));

Would the second cal.setTime line reset the date portion of the calendar object to now and just change the time? 第二个cal.setTime行是否会将日历对象的日期部分重置为现在并仅更改时间?

Yes it would. 是的,会的。

setTime() sets the the time regardless of the fact that a date contained no time value (00:00:00) or no date value (01.01.1970). setTime()设置时间,而与日期不包含任何时间值(00:00:00)或不包含日期值(01.01.1970)的事实无关。

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdfDate = new SimpleDateFormat("MM-dd-yyyy hh:mm zzz");
cal.setTime(sdfDate.parse(DATE+ " " + TIME));

Should work out for you. 应该为您解决。

tl;dr TL;博士

ZonedDateTime.of( 
    LocalDate.parse( "12-23-2015" , DateTimeFormatter.ofPattern( "MM-dd-yyyy") ) ,
    LocalTime.parse( "21:43" ) , 
    ZoneId.of( "Pacific/Auckland" )
)
.toString()

2015-12-23T21:43+13:00[Pacific/Auckland] 2015-12-23T21:43 + 13:00 [太平洋/奥克兰]

Details 细节

The Answer by Jan is correct. Jan的答案是正确的。

java.time java.time

Alternatively, you could use the new date-time framework, java.time. 另外,您可以使用新的日期时间框架java.time。

The java.time framework built into Java 8 and later supplants the troublesome old java.util.Date/.Calendar classes. Java 8和更高版本中内置的java.time框架取代了麻烦的旧java.util.Date/.Calendar类。 The new classes are inspired by the highly successful Joda-Time framework, intended as its successor, similar in concept but re-architected. 新课程的灵感取自于成功的Joda-Time框架,该框架旨在作为其继任者,其概念相似但经过重新架构。 Defined by JSR 310 . JSR 310定义。 Extended by the ThreeTen-Extra project. ThreeTen-Extra项目扩展。 See the Tutorial . 请参阅教程

If your inputs lacked an offset-from-UTC , then we could treat the date and the time-of-day separately. 如果您的输入缺少与UTC偏移量 ,那么我们可以分别处理日期和时间。 The new classes include LocalDate to represent a date-only value without a time-of-day, and LocalTime to represent a time-only value without a date. 新类包括LocalDate ,表示没有日期的仅日期值, LocalTime表示没有日期的仅时间值。 Then you can combine them and adjust into their intended time zone. 然后,您可以将它们组合并调整到其预期的时区。

DateTimeFormatter formatterDate = DateTimeFormatter.ofPattern( "MM-dd-yyyy");
LocalDate localDate = LocalDate.parse( "12-23-2015" , formatterDate );
LocalTime localTime = LocalTime.parse( "21:43" );
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.of( localDate , localTime , zoneId );

But your time string does contain an offset-from-UTC. 但是您的时间字符串中确实包含一个与UTC的偏移量。 So we should take the same approach as the Answer by Jan, concatenate the pair of strings and then parse. 因此,我们应该采用与Jan的Answer相同的方法,连接成对的字符串,然后进行解析。

String input = "12-23-2015" + " " + "21:43-05:00" ;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "MM-dd-yyyy HH:mmxxx");
ZonedDateTime zdt = ZonedDateTime.parse( input , formatter );

ISO 8601 ISO 8601

By the way, in the future when serializing a date, a time, or a date-time to a string such as you did in your SQLite database I strongly recommend using the standard ISO 8601 formats: YYYY-MM-DD , HH:MM , and YYYY-MM-DDTHH:MM:SS.S±00:00 . 顺便说一句,将来在将日期,时间或日期时间序列化为字符串时(例如您在SQLite数据库中所做的那样),我强烈建议使用标准ISO 8601格式: YYYY-MM-DDHH:MM ,以及YYYY-MM-DDTHH:MM:SS.S±00:00 For example, 2007-12-03T10:15:30+01:00 . 例如, 2007-12-03T10:15:30+01:00 These formats are standardized, easy for humans to read and discern, and easy for computers to parse without ambiguity. 这些格式是标准化的,易于人类阅读和识别,并且易于计算机解析而没有歧义。

The java.time framework parses and generates strings in these formats by default. 默认情况下,java.time框架解析并生成这些格式的字符串。 Also, java.time extends ISO 8601 by appending the name of the time zone in square brackets. 另外,java.time通过在方括号中附加时区名称来扩展ISO 8601。 For example, 2007-12-03T10:15:30+01:00[Europe/Paris] . 例如, 2007-12-03T10:15:30+01:00[Europe/Paris]


About java.time 关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧的旧式日期时间类,例如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参见Oracle教程 And search Stack Overflow for many examples and explanations. 并在Stack Overflow中搜索许多示例和说明。 Specification is JSR 310 . 规格为JSR 310

With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. 使用符合JDBC 4.2或更高版本的JDBC驱动程序 ,您可以直接与数据库交换java.time对象。 No need for strings or java.sql.* classes. 不需要字符串或java.sql。*类。

Where to obtain the java.time classes? 在哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目为将来可能在java.time中添加内容提供了一个试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM