将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

查看:125
本文介绍了将iso日期格式的字符串转换为毫秒.ISO日期格式包括("yyyy-MM-dd"),并且没有时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在努力将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类?

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?

这篇关于将iso日期格式的字符串转换为毫秒.ISO日期格式包括("yyyy-MM-dd"),并且没有时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆