简体   繁体   English

如何将日期转换为毫秒

[英]How to convert a date to milliseconds

I want to convert String myDate = "2014/10/29 18:10:45" to long ms (ie currentinmlilies) ?我想将String myDate = "2014/10/29 18:10:45"转换为long ms (ie currentinmlilies) I look for it on Google, but I can only find how to convert ms to date .我在谷歌上找它,但我只能找到如何将ms转换为date

Note: To make it clear, I want to get the ms from the date in 1970/1/1 format.注意:为了清楚起见,我想从 1970/1/1 格式的日期中获取毫秒。

You don't have a Date , you have a String representation of a date.你没有Date ,你有一个日期的String表示。 You should convert the String into a Date and then obtain the milliseconds.您应该将String转换为Date ,然后获取毫秒。 To convert a String into a Date and vice versa you should use SimpleDateFormat class.要将String转换为Date ,反之亦然,您应该使用SimpleDateFormat类。

Here's an example of what you want/need to do (assuming time zone is not involved here):这是您想要/需要做的一个示例(假设此处不涉及时区):

String myDate = "2014/10/29 18:10:45";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = sdf.parse(myDate);
long millis = date.getTime();

Still, be careful because in Java the milliseconds obtained are the milliseconds between the desired epoch and 1970-01-01 00:00:00.不过,要小心,因为在 Java 中获得的毫秒数是所需时期和 1970-01-01 00:00:00 之间的毫秒数。


Using the new Date/Time API available since Java 8:使用自 Java 8 起可用的新日期/时间 API:

String myDate = "2014/10/29 18:10:45";
LocalDateTime localDateTime = LocalDateTime.parse(myDate,
    DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss") );
/*
  With this new Date/Time API, when using a date, you need to
  specify the Zone where the date/time will be used. For your case,
  seems that you want/need to use the default zone of your system.
  Check which zone you need to use for specific behaviour e.g.
  CET or America/Lima
*/
long millis = localDateTime
    .atZone(ZoneId.systemDefault())
    .toInstant().toEpochMilli();

tl;dr tl;博士

LocalDateTime.parse(           // Parse into an object representing a date with a time-of-day but without time zone and without offset-from-UTC.
    "2014/10/29 18:10:45"      // Convert input string to comply with standard ISO 8601 format.
    .replace( " " , "T" )      // Replace SPACE in the middle with a `T`.
    .replace( "/" , "-" )      // Replace SLASH in the middle with a `-`.
)
.atZone(                       // Apply a time zone to provide the context needed to determine an actual moment.
    ZoneId.of( "Europe/Oslo" ) // Specify the time zone you are certain was intended for that input.
)                              // Returns a `ZonedDateTime` object.
.toInstant()                   // Adjust into UTC.
.toEpochMilli()                // Get the number of milliseconds since first moment of 1970 in UTC, 1970-01-01T00:00Z.

1414602645000 1414602645000

Time Zone时区

The accepted answer is correct, except that it ignores the crucial issue of time zone .接受的答案是正确的,只是它忽略了time zone 的关键问题 Is your input string 6:10 PM in Paris or Montréal?您的输入字符串是在巴黎还是蒙特利尔的下午 6:10? Or UTC ?还是UTC

Use a proper time zone name .使用正确的时区名称 Usually a continent plus city/region.通常是一个大陆加上城市/地区。 For example, "Europe/Oslo" .例如, "Europe/Oslo" Avoid the 3 or 4 letter codes which are neither standardized nor unique.避免使用既不标准化也不唯一的 3 或 4 个字母代码。

java.time java.time

The modern approach uses the java.time classes.现代方法使用java.time类。

Alter your input to conform with the ISO 8601 standard.更改您的输入以符合ISO 8601标准。 Replace the SPACE in the middle with a T .T替换中间的空格。 And replace the slash characters with hyphens.并用连字符替换斜线字符。 The java.time classes use these standard formats by default when parsing/generating strings. java.time类在解析/生成字符串时默认使用这些标准格式。 So no need to specify a formatting pattern.所以不需要指定格式模式。

String input = "2014/10/29 18:10:45".replace( " " , "T" ).replace( "/" , "-" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;

A LocalDateTime , like your input string, lacks any concept of time zone or offset-from-UTC. LocalDateTime与您的输入字符串一样,缺少任何时区概念或与 UTC 的偏移量。 Without the context of a zone/offset, a LocalDateTime has no real meaning.如果没有区域/偏移量的上下文, LocalDateTime就没有真正的意义。 Is it 6:10 PM in India, Europe, or Canada?印度、欧洲或加拿大是下午 6:10 吗? Each of those places experience 6:10 PM at different moments, at different points on the timeline.这些地方中的每一个都会在不同的时刻、时间轴上的不同点经历下午 6:10。 So you must specify which you have in mind if you want to determine a specific point on the timeline.因此,如果要确定时间轴上的特定点,则必须指定要考虑的内容。

ZoneId z = ZoneId.of( "Europe/Oslo" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;  

Now we have a specific moment, in that ZonedDateTime .现在我们有一个特定的时刻,在ZonedDateTime中。 Convert to UTC by extracting a Instant .通过提取Instant转换为 UTC。 The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). Instant类表示UTC时间线上的时刻,分辨率为纳秒(最多九 (9) 位小数)。

Instant instant = zdt.toInstant() ;

Now we can get your desired count of milliseconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z.现在,我们可以获得自 1970 年 UTC 第一时刻的纪元参考 1970-01-01T00:00Z 以来所需的毫秒数。

long millisSinceEpoch = instant.toEpochMilli() ; 

Be aware of possible data loss.请注意可能的数据丢失。 The Instant object is capable of carrying microseconds or nanoseconds, finer than milliseconds. Instant对象能够承载微秒或纳秒,比毫秒更精细。 That finer fractional part of a second will be ignored when getting a count of milliseconds.在获取毫秒数时,将忽略一秒的小数部分。


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

You may exchange java.time objects directly with your database.您可以直接与您的数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later.使用符合JDBC 4.2或更高版本的JDBC 驱动程序 No need for strings, no need for 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等等


Joda-Time乔达时间

Update: The Joda-Time project is now in maintenance mode , with the team advising migration to the java.time classes.更新: Joda-Time项目现在处于维护模式,团队建议迁移到java.time类。 I will leave this section intact for history.我将保留这部分完整的历史。

Below is the same kind of code but using the Joda-Time 2.5 library and handling time zone.下面是相同类型的代码,但使用Joda-Time 2.5 库和处理时区。

The java.util.Date, .Calendar, and .SimpleDateFormat classes are notoriously troublesome, confusing, and flawed. java.util.Date、.Calendar 和 .SimpleDateFormat 类是出了名的麻烦、混乱和缺陷。 Avoid them.避开他们。 Use either Joda-Time or the java.time package (inspired by Joda-Time) built into Java 8.使用 Java 8 中内置的 Joda-Time 或 java.time 包(受 Joda-Time 启发)。

ISO 8601 ISO 8601

Your string is almost in ISO 8601 format.您的字符串几乎是 ISO 8601 格式。 The slashes need to be hyphens and the SPACE in middle should be replaced with a T .斜杠需要是连字符,中间的空格应该替换为T If we tweak that, then the resulting string can be fed directly into constructor without bothering to specify a formatter.如果我们对其进行调整,那么生成的字符串可以直接输入到构造函数中,而无需指定格式化程序。 Joda-Time uses ISO 8701 formats as it's defaults for parsing and generating strings. Joda-Time 使用 ISO 8701 格式作为解析和生成字符串的默认格式。

Example Code示例代码

String inputRaw = "2014/10/29 18:10:45";
String input = inputRaw.replace( "/", "-" ).replace( " ", "T" );
DateTimeZone zone = DateTimeZone.forID( "Europe/Oslo" ); // Or DateTimeZone.UTC
DateTime dateTime = new DateTime( input, zone );
long millisecondsSinceUnixEpoch = dateTime.getMillis();

The SimpleDateFormat class allows you to parse a String into a java.util.Date object. SimpleDateFormat类允许您将String解析为java.util.Date对象。 Once you have the Date object, you can get the milliseconds since the epoch by calling Date.getTime() .拥有 Date 对象后,您可以通过调用Date.getTime()获取自纪元以来的毫秒数。

The full example:完整的例子:

String myDate = "2014/10/29 18:10:45";
//creates a formatter that parses the date in the given format
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = sdf.parse(myDate);
long timeInMillis = date.getTime();

Note that this gives you a long and not a double, but I think that's probably what you intended.请注意,这会给您一个long而不是 double,但我认为这可能是您的意图。 The documentation for the SimpleDateFormat class has tons on information on how to set it up to parse different formats. SimpleDateFormat类的文档有大量关于如何设置它来解析不同格式的信息。

The 2017 answer is: Use the date and time classes introduced in Java 8 (and also backported to Java 6 and 7 in the ThreeTen Backport ). 2017 年的答案是:使用 Java 8 中引入的日期和时间类(并且在ThreeTen Backport中也向后移植到 Java 6 和 7)。

If you want to interpret the date-time string in the computer's time zone:如果要解释计算机时区中的日期时间字符串:

    long millisSinceEpoch = LocalDateTime.parse(myDate, DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss"))
            .atZone(ZoneId.systemDefault())
            .toInstant()
            .toEpochMilli();

If another time zone, fill that zone in instead of ZoneId.systemDefault() .如果是另一个时区,请填写该区域而不是ZoneId.systemDefault() If UTC, use如果是 UTC,请使用

    long millisSinceEpoch = LocalDateTime.parse(myDate, DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss"))
            .atOffset(ZoneOffset.UTC)
            .toInstant()
            .toEpochMilli();

You can solve issue with using LocalDateTime and DateTimeFormatter您可以使用 LocalDateTime 和 DateTimeFormatter 解决问题

    String date = "21/04/2022 12:42:29";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
    LocalDateTime dt = LocalDateTime.parse(date, formatter);
    System.out.println(dt.toEpochSecond(ZoneOffset.UTC));

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

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