如何将String日期变为Long?

时间:2018-05-12 16:16:02

标签: android string date datetime

我有一个String变量,我想把它变成long变量。

String dtStart = "2018-04-25 20:09:55";
                    Date date = null;
                    long dateLong = 0;
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                    try {
                        date = format.parse(dtStart);


                        dateLong = date.getTime();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }

变量dtStart改变了值,它显示dateLong = 0,我希望将此变量转换为long中的dateLong变量

此外,我可以将dtStartd变成"2010-10-15T09:27:37Z"这种格式,但我不知道如何。

1 个答案:

答案 0 :(得分:1)

TL;博士

LocalDateTime.parse(                             // Parse string as a `LocalDateTime`. Lacking any time zone or offset-from-UTC means this is *not* a moment, *not* a point on the timeline.
    "2018-04-25 20:09:55".replace( " " , "T" )   // Alter input to comply with ISO 8601 standard format, used by default in the *java.time* classes.
)                                                // Returns a `LocalDateTime` object.
.atZone(                                         // Give context and meaning to our `LocalDateTime`, to determine a point on the timeline.
    ZoneId.of( "America/Montreal" )              // Always specify time zone using proper `Continent/Region` format, never 3-4 letter pseudo-zones such as `PST`, `CST`, `IST`.
)                                                // Returns a `ZonedDateTime` object.
.toInstant()                                     // 2018-04-26T00:09:55Z. Extract a UTC value, `Instant` object, from the `ZonedDateTime`.
.toEpochMilli()                                  // Ask for count of milliseconds from 1970-01-01T00:00Z to this moment. 
  

1524701395000

java.time

更改字符串输入以符合ISO 8601标准。

String input = "2018-04-25 20:09:55".replace( " " , "T" ) ;
  

2018-04-25 20:09:55

解析。

LocalDateTime ldt = LocalDateTime.parse( input ) ;
  

ldt.toString():2018-04-25T20:09:55

使用ZoneId应用预期时区,以获得ZonedDateTime

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
  

zdt.toString():2018-04-25T20:09:55-04:00 [美国/蒙特利尔]

以UTC格式提取值Instant

Instant instant = zdt.toInstant();
  

instant.toString():2018-04-26T00:09:55Z

获取1970年UTC 1970-01-01T00:00Z的第一个时刻以来的毫秒数。

long millis = instant.toEpochMilli() ;
  

1524701395000

如果该输入字符串用于UTC而不是时区(您的问题在这方面不明确),请使用与上述相同的提示,但切换到OffsetDateTimeZoneOffset到位ZonedDateTimeZoneId

LocalDateTime.parse(                     
    "2018-04-25 20:09:55".replace( " " , "T" )  
)                                        
.atOffset(                                     
    ZoneOffset.UTC                        // Using `ZoneOffset` rather than `ZoneId`. For UTC, we have the constant `ZoneOffset.UTC`. 
)                                         // Returns an `OffsetDateTime` object.                                            
.toInstant()                              // 2018-04-25T20:09:55Z
.toEpochMilli()  

请注意,我们得到的结果与上述不同,因为UTC的晚上8点与蒙特利尔魁北克的晚上8点不同。

  

1524686995000

关于 java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和& SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore

相关问题