mic*_*cha 313 java datetime timestamp java-8 java-time
Java 8添加了一个新的java.time API来处理日期和时间(JSR 310).
我有日期和时间作为字符串(例如"2014-04-08 12:30"
).如何LocalDateTime
从给定的字符串中获取实例?
在我完成LocalDateTime
对象的使用之后:如何将LocalDateTime
实例转换回具有与上面所示格式相同的字符串?
mic*_*cha 498
解析日期和时间
要从LocalDateTime
字符串创建对象,可以使用静态LocalDateTime.parse()
方法.它需要一个字符串和一个DateTimeFormatter
参数.所述DateTimeFormatter
用于指定的日期/时间模式.
String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Run Code Online (Sandbox Code Playgroud)
格式化日期和时间
要从LocalDateTime
对象创建格式化字符串,您可以使用该format()
方法.
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.of(1986, Month.APRIL, 8, 12, 30);
String formattedDateTime = dateTime.format(formatter); // "1986-04-08 12:30"
Run Code Online (Sandbox Code Playgroud)
请注意,有一些常用的日期/时间格式预定义为常量DateTimeFormatter
.例如:使用从上面DateTimeFormatter.ISO_DATE_TIME
格式化LocalDateTime
实例将导致字符串"1986-04-08T12:30:00"
.
该parse()
和format()
方法可用于所有日期/时间相关的对象(例如LocalDate
或ZonedDateTime
)
Suf*_*ori 142
如果是ISO-8601格式,您也可以使用LocalDate.parse()
或LocalDateTime.parse()
在String
不提供图案的情况下使用.String
例如,
String strDate = "2015-08-04";
LocalDate aLD = LocalDate.parse(strDate);
System.out.println("Date: " + aLD);
String strDatewithTime = "2015-08-04T10:11:30";
LocalDateTime aLDT = LocalDateTime.parse(strDatewithTime);
System.out.println("Date with Time: " + aLDT);
Run Code Online (Sandbox Code Playgroud)
输出,
Date: 2015-08-04
Date with Time: 2015-08-04T10:11:30
Run Code Online (Sandbox Code Playgroud)
并且DateTimeFormatter
仅在必须处理其他日期模式时使用,例如,dd MMM uuuu表示月份的日期(两位数),月份名称的三个字母(Jan,Feb,Mar,...),和一个四位数的年份:
DateTimeFormatter dTF = DateTimeFormatter.ofPattern("dd MMM uuuu");
String anotherDate = "04 Aug 2015";
LocalDate lds = LocalDate.parse(anotherDate, dTF);
System.out.println(anotherDate + " parses to " + lds);
Run Code Online (Sandbox Code Playgroud)
产量
04 Aug 2015 parses to 2015-08-04
Run Code Online (Sandbox Code Playgroud)
还记得DateTimeFormatter
对象是双向的; 它既可以解析输入也可以格式化输出.
String strDate = "2015-08-04";
LocalDate aLD = LocalDate.parse(strDate);
DateTimeFormatter dTF = DateTimeFormatter.ofPattern("dd MMM uuuu");
System.out.println(aLD + " formats as " + dTF.format(aLD));
Run Code Online (Sandbox Code Playgroud)
产量
2015-08-04 formats as 04 Aug 2015
Run Code Online (Sandbox Code Playgroud)
(请参阅格式化和解析DateFormatter的模式的完整列表)
Symbol Meaning Presentation Examples
------ ------- ------------ -------
G era text AD; Anno Domini; A
u year year 2004; 04
y year-of-era year 2004; 04
D day-of-year number 189
M/L month-of-year number/text 7; 07; Jul; July; J
d day-of-month number 10
Q/q quarter-of-year number/text 3; 03; Q3; 3rd quarter
Y week-based-year year 1996; 96
w week-of-week-based-year number 27
W week-of-month number 4
E day-of-week text Tue; Tuesday; T
e/c localized day-of-week number/text 2; 02; Tue; Tuesday; T
F week-of-month number 3
a am-pm-of-day text PM
h clock-hour-of-am-pm (1-12) number 12
K hour-of-am-pm (0-11) number 0
k clock-hour-of-am-pm (1-24) number 0
H hour-of-day (0-23) number 0
m minute-of-hour number 30
s second-of-minute number 55
S fraction-of-second fraction 978
A milli-of-day number 1234
n nano-of-second number 987654321
N nano-of-day number 1234000000
V time-zone ID zone-id America/Los_Angeles; Z; -08:30
z time-zone name zone-name Pacific Standard Time; PST
O localized zone-offset offset-O GMT+8; GMT+08:00; UTC-08:00;
X zone-offset 'Z' for zero offset-X Z; -08; -0830; -08:30; -083015; -08:30:15;
x zone-offset offset-x +0000; -08; -0830; -08:30; -083015; -08:30:15;
Z zone-offset offset-Z +0000; -0800; -08:00;
p pad next pad modifier 1
' escape for text delimiter
'' single quote literal '
[ optional section start
] optional section end
# reserved for future use
{ reserved for future use
} reserved for future use
Run Code Online (Sandbox Code Playgroud)
Mar*_*ski 36
上面的两个答案都很好地解释了关于字符串模式的问题 但是,如果你正在使用ISO 8601,则无需申请,DateTimeFormatter
因为LocalDateTime已经为它准备好了:
将LocalDateTime转换为时区ISO8601字符串
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zdt = ldt.atZone(ZoneOffset.UTC); //you might use a different zone
String iso8601 = zdt.toString();
Run Code Online (Sandbox Code Playgroud)
从ISO8601 String转换回LocalDateTime
String iso8601 = "2016-02-14T18:32:04.150Z";
ZonedDateTime zdt = ZonedDateTime.parse(iso8601);
LocalDateTime ldt = zdt.toLocalDateTime();
Run Code Online (Sandbox Code Playgroud)
Ond*_*žka 11
将具有日期和时间的字符串解析为特定的时间点(Java将其称为“ Instant
”)非常复杂。Java已经通过多次迭代解决了这个问题。最新的java.time
和java.time.chrono
几乎可以满足所有需求(“ 时间膨胀”除外)。
但是,这种复杂性带来了很多混乱。
了解日期解析的关键是:
LocalDateTime
,ZonedDateTime
等人。太复杂了有时区。时区基本上是地球表面的“条带” * [1],其权限遵循何时具有哪个时间偏移的相同规则。这包括夏季时间规则。
时区会随时间变化,各个地区的变化主要取决于谁征服谁。一个时区的规则也会随着时间而改变。
有时间偏移。这与时区不同,因为时区可能是“ Prague”(布拉格),但是具有夏令时和冬令时。
如果获得带时区的时间戳,则偏移量可能会有所不同,具体取决于所在的年份。在the年期间,时间戳可能表示2个不同的时间,因此如果没有其他信息,它就无法可靠地显示出来。转换。
注意:通过时间戳记,我的意思是“包含日期和/或时间,以及时区和/或时间偏移的字符串。”
某些时区在某些时段可能共享相同的时间偏移。例如,当夏令时未生效时,GMT / UTC时区与“伦敦”时区相同。
使它更加复杂(但这对您的用例而言并不重要):
2040-12-31 24:00:00
可能是有效的日期时间。)这需要定期更新元数据,系统使用这些元数据来进行日期转换。例如,在Linux上,您会定期更新Java软件包,包括这些新数据。对于历史和将来的时间戳,更新并不总是保留以前的行为。因此,在不同版本的软件上运行时,围绕某个时区变化进行分析的两个时间戳进行比较可能会得出不同的结果。这也适用于在受影响的时区和其他时区之间进行比较。
如果这会导致软件中的错误,请考虑使用一些没有复杂规则的时间戳,例如UNIX timestamp。
由于7,对于将来的日期,我们无法完全确定地转换日期。因此,例如,当前的解析8524-02-17 12:00:00
可能与将来的解析相差几秒钟。
java.util.Date
假设只有年,月,日和时间,那么早期的Java版本只是采用了一些幼稚的方法。很快这还不够。java.sql.Date
引入了它,但它有其自身的局限性。Calendar
因此引入了API。java.time
使用时间戳字符串时,需要知道它包含哪些信息。这是关键点。如果您做不到这一点,您将遇到一个神秘的异常,例如“无法创建即时”或“缺少区域偏移”或“未知区域ID”等。
是否包含日期和时间?
它有时间偏移吗?
时间偏移是其中的+hh:mm
一部分。有时,+00:00
可以用Z
“祖鲁时间”,UTC
“世界协调时间” 或GMT
“格林威治标准时间” 代替。这些也设置了时区。
对于这些时间戳,请使用OffsetDateTime
。
是否有时区?
对于这些时间戳,请使用ZonedDateTime
。
区域由以下任一指定
时区列表由ICAAN支持的“ TZ数据库”进行编译。
根据ZoneId
的javadoc,区域ID也可以以某种方式指定为Z
和偏移量。我不确定这是如何映射到真实区域的。如果只有TZ的时间戳落入时间偏移量变化的hour时间,则表示该模棱两可,并且解释以为主ResolverStyle
,请参见下文。
如果两者都没有,则假定缺少的上下文或忽略该上下文。消费者必须决定。因此,需要通过添加缺少的信息将其解析为LocalDateTime
并转换为OffsetDateTime
:
Duration
),或者在您不知道且并不重要的时候(例如本地公交车时刻表)。部分时间信息
LocalDate
,LocalTime
,OffsetTime
,MonthDay
,Year
,或YearMonth
出来。如果您有完整的信息,则可以获取java.time.Instant
。这在内部也用于在OffsetDateTime
和之间进行转换ZonedDateTime
。
有大量的文档DateTimeFormatter
可以解析时间戳字符串和格式化为字符串。
在预先创建DateTimeFormatter
小号应涵盖所有收起标准时间戳的格式。例如,ISO_INSTANT
可以解析2011-12-03T10:15:30.123457Z
。
如果您有某种特殊格式,则可以创建自己的DateTimeFormatter(它也是解析器)。
private static final DateTimeFormatter TIMESTAMP_PARSER = new DateTimeFormatterBuilder()
.parseCaseInsensitive()
.append(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SX"))
.toFormatter();
Run Code Online (Sandbox Code Playgroud)
我建议您查看的源代码,DateTimeFormatter
并获得有关如何使用进行构建的启发DateTimeFormatterBuilder
。当您在那里时,还可以查看ResolverStyle
哪个控件可以分析格式和歧义信息的解析器是LENIENT,SMART还是STRICT。
现在,常见的错误是进入的复杂性TemporalAccessor
。这来自于开发人员过去与之合作的方式SimpleDateFormatter.parse(String)
。是的,DateTimeFormatter.parse("...")
给你TemporalAccessor
。
// No need for this!
TemporalAccessor ta = TIMESTAMP_PARSER.parse("2011-... etc");
Run Code Online (Sandbox Code Playgroud)
但是,有了上一节的知识,您可以方便地解析为所需的类型:
OffsetDateTime myTimestamp = OffsetDateTime.parse("2011-12-03T10:15:30.123457Z", TIMESTAMP_PARSER);
Run Code Online (Sandbox Code Playgroud)
您实际上并不需要DateTimeFormatter
。您要解析的类型具有parse(String)
方法。
OffsetDateTime myTimestamp = OffsetDateTime.parse("2011-12-03T10:15:30.123457Z");
Run Code Online (Sandbox Code Playgroud)
关于TemporalAccessor
,如果您对字符串中包含的信息有一个模糊的想法,并且想在运行时决定,则可以使用它。
我希望我对你的灵魂有所了解:)
注意:java.time
Java 6和7 有一个反向移植:ThreeTen-Backport。对于Android,它具有ThreeTenABP。
[1]不仅因为它们不是条纹,而且还有一些怪异的极端。例如,某些邻近的太平洋岛屿具有+14:00和-11:00时区。这就是说,在一个岛上,是5月1日下午3点,而在另一个岛上,则是4月30日,下午12点(如果我算错了:))
小智 6
另一件需要注意的事情LocalDateTime.parse
是,您不能将其与仅包含日期格式化程序字符的自定义格式化程序一起使用,例如uuuuMMdd
. 在这种情况下,您应该改用LocalDate.parse
。例如:
String s = "20210223";
// ok
LocalDate.parse(s, DateTimeFormatter.ofPattern("uuuuMMdd"));
// java.time.format.DateTimeParseException
LocalDateTime.parse(s, DateTimeFormatter.ofPattern("uuuuMMdd"));
Run Code Online (Sandbox Code Playgroud)
以所需格式获取当前 UTC 时间
// Current the UTC time
OffsetDateTime utc = OffsetDateTime.now(ZoneOffset.UTC);
// Get LocalDateTime
LocalDateTime localDateTime = utc.toLocalDateTime();
System.out.println("*************" + localDateTime);
// Formatted UTC time
DateTimeFormatter dTF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
System.out.println(" formats as " + dTF.format(localDateTime));
// Get the UTC time for the current date
Date now = new Date();
LocalDateTime utcDateTimeForCurrentDateTime = Instant.ofEpochMilli(now.getTime()).atZone(ZoneId.of("UTC")).toLocalDateTime();
DateTimeFormatter dTF2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
System.out.println(" formats as " + dTF2.format(utcDateTimeForCurrentDateTime));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
473797 次 |
最近记录: |