将iso日期格式的字符串转换为毫秒.ISO日期格式包括("yyyy-MM-dd"),并且没有时区 [英] Convert string in iso date format to milliseconds. Iso date format includes ("yyyy-MM-dd") and doesn't have timezone
问题描述
正在努力将iso日期格式的字符串转换为毫秒.我知道也有类似的问题,但是我找不到包含"yyyy-MM-dd"并且没有时区的问题.
简短的代码段供参考.
主类{公共静态void main(String [] args){今天的LocalDate = LocalDate.now();字符串dateformat = DateTimeFormatter.ISO_DATE.format(today);System.out.println(dateformat);}}
tl; dr
您问:
将iso日期格式的字符串转换为毫秒.ISO日期格式包含("yyyy-MM-dd"),并且没有时区
OffsetDateTime//将时刻表示为日期,时间和偏移量..of(//工厂方法从日期,时间和偏移量实例化`OffsetDateTime`.LocalDate.parse("2021-01-23"),//解析您的输入文本.LocalTime.MIN,//常数为00:00.ZoneOffset.UTC//表示自UTC的零小时-分钟-秒的偏移量的常数.)//返回`OffsetDateTime`对象..toInstant()//返回一个即时"对象..toEpochMilli()//返回一个long,表示从1970-01-01T00:00:00Z开始的毫秒数.
请参见此
关于 java.time
旧版日期时间类,例如 SimpleDateFormat
.
要了解更多信息,请参见 Oracle教程.并在Stack Overflow中搜索许多示例和说明.规范为 JSR 310 .
Joda-Time 项目,现在位于<一个href ="https://en.wikipedia.org/wiki/Maintenance_mode" rel ="nofollow noreferrer">维护模式,建议迁移到 JDBC驱动程序/jeps/170"rel =" nofollow noreferrer> JDBC 4.2 或更高版本.不需要字符串,不需要 java.sql.*
类.休眠5&JPA 2.2支持 java.time .
在哪里获取java.time类?
- Java SE 8 , Java SE 9 , Java SE 11 ,以及更高版本-具有捆绑实施的标准Java API的一部分.
- Java 9添加了一些次要功能和修复.
- Java SE 6 和 Java SE 7
- 大多数 java.time 功能都向后移植到Java 6&7在> ThreeTen-Backport 中./li>
- Android
- java.time 类的Android捆绑包实现的最新版本.
- 对于较早的Android(< 26), ThreeTenABP 项目适应> ThreeTen-Backport (如上所述).请参阅 如何使用ThreeTenABP… .
Struggling to convert string in iso date format to milliseconds. I know there are similar questions to this, but I couldn't find one that included "yyyy-MM-dd" and didn't have timezone.
Short snippet of code for reference.
class Main {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
String dateformat = DateTimeFormatter.ISO_DATE.format(today);
System.out.println(dateformat); }
}
tl;dr
You asked:
Convert string in iso date format to milliseconds. Iso date format includes ("yyyy-MM-dd") and doesn't have timezone
OffsetDateTime // Represent a moment as a date, a time, and an offset.
.of( // Factory method to instantiate a `OffsetDateTime` from a date, a time, and an offset.
LocalDate.parse( "2021-01-23" ) , // Parse your input text.
LocalTime.MIN , // Constant for 00:00.
ZoneOffset.UTC // Constant representing an offset-from-UTC of zero hours-minutes-seconds.
) // Returns an `OffsetDateTime` object.
.toInstant() // Returns an `Instant` object.
.toEpochMilli() // Returns a `long` representing a count of milliseconds since 1970-01-01T00:00:00Z.
See this code run live at IdeOne.com.
1611360000000
Details
You said:
string in iso date format
LocalDate localDate = LocalDate.of( 2021 , Month.JANUARY , 23 ) ;
String output = localDate.toString() ; // Uses ISO 8601 format by default.
output: 2021-01-23
You said:
convert string … milliseconds
Parse string as LocalDate
.
LocalDate localDate = LocalDate.parse( "2021-01-23" ) ;
For milliseconds since the epoch reference of first moment of 1970 in UTC, we need a time of day. For time-of-day, use LocalTime
class with its constant LocalTime.MIN
.
LocalTime localTime = LocalTime.MIN ;
Combine with UTC as the offset to get a OffsetDateTime
object, which represents a moment, a point on the timeline.
OffsetDateTime odt = OffsetDateTime.of( localDate , localTime , ZoneOffset.UTC ) ;
Extract an Instant
from the OffsetDateTime
.
Instant instant = odt.toInstant() ;
Interrogate the Instant
for a count of milliseconds from epoch reference 1970-01-01T00:00Z.
long millisecondsSinceEpoch = instant.toEpochMilli() ;
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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
- Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and Java SE 7
- Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….
这篇关于将iso日期格式的字符串转换为毫秒.ISO日期格式包括("yyyy-MM-dd"),并且没有时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!