我想将 String myDate = "2014/10/29 18:10:45"
转换为
长毫秒(即当前毫秒数)"?我在 Google 上查找,但只能找到如何将 ms 转换为 date。
注:为了清楚起见,我想从 1970/1/1 格式的日期中获取毫秒数。
你没有一个 "日期",你有一个日期的 "字符串 "表示法。您应该将字符串'转换为
日期',然后获取毫秒数。要将 String
转换为 Date
,反之亦然,应使用 SimpleDateFormat
类。
下面是一个你想要/需要做的示例(假设这里不涉及时区):
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();
不过还是要小心,因为在 Java 中,所获得的毫秒是所需纪元与 1970-01-01 00:00:00 之间的毫秒。
使用 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();
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.
String input = "2014/10/29 18:10:45".replace( " " , "T" ).replace( "/" , "-" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;
与输入字符串一样,"LocalDateTime "也没有时区或从夏令时偏移的概念。如果没有时区/偏移的上下文,"LocalDateTime "就没有任何实际意义。现在是印度、欧洲还是加拿大的下午 6:10?这些地方都会在时间轴上的不同时间点经历下午 6:10。因此,如果你想确定时间轴上的某个具体时间点,就必须说明你想到的是哪个时间点。
ZoneId z = ZoneId.of( "Europe/Oslo" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
现在我们有了 "ZonedDateTime "中的特定时刻。通过提取 "Instant",将其转换为 UTC。Instant](http://docs.oracle.com/javase/10/docs/api/java/time/Instant.html)类表示时间轴上的[UTC](https://en.wikipedia.org/wiki/Coordinated_Universal_Time)时刻,分辨率为[纳秒](https://en.wikipedia.org/wiki/Nanosecond)(最多为小数点后九(9)位)。
Instant instant = zdt.toInstant() ;
现在,我们可以得到自 1970 年第一时刻(世界协调时,1970-01-01T00:00Z)的纪元参考起的毫秒数。
long millisSinceEpoch = instant.toEpochMilli() ;
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,如 java.util.Date
、Calendar
、& SimpleDateFormat
。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
要了解更多信息,请参阅Oracle 教程。还可以搜索 Stack Overflow,了解更多示例和解释。规范是 JSR 310。
您可以直接与数据库交换 java.time 对象。使用符合 JDBC 4.2 或更高版本的 JDBC 驱动程序。无需字符串,无需 java.sql.*
类。
从哪里获取 java.time 类?
Interval
、YearWeek
、YearQuarter
和 more。更新: Joda-Time 项目现在处于 维护模式,团队建议迁移到 java.time 类。我将保留这一部分作为历史资料。 下面是同类代码,但使用了 Joda-Time 2.5 库并处理时区。 java.util.Date、.Calendar 和 .SimpleDateFormat 类是出了名的麻烦、混乱和缺陷。请避免使用它们。使用 Joda-Time 或 Java 8 内置的 java.time 包(受 Joda-Time 的启发)。
您的字符串几乎是 ISO 8601 格式。斜线应改为连字符,中间的空格应改为 "T"。如果我们稍作调整,就可以将生成的字符串直接输入构造函数,而不必费心指定格式。Joda-Time 默认使用 ISO 8701 格式来解析和生成字符串。
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();
SimpleDateFormat](http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html)类允许您将`String`解析为`java.util.Date`对象。有了 Date 对象后,您就可以通过调用 Date.getTime()
来获取从纪元开始的毫秒数。
完整示例
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();
请注意,这样得到的是 "long "而不是 double,但我想这可能正是你想要的。SimpleDateFormat` 类的文档中有大量关于如何设置它以解析不同格式的信息。