我想转换String myDate = "2014/10/29 18:10:45"成
long ms (i.e. currentinmlilies)?我在Google上寻找它,但我只能找到如何将ms转换为日期.
注意:为了说清楚,我想从1970/1/1格式的日期获得ms.
Lui*_*oza 71
你没有Date,你有String一个日期的代表.您应该将其转换String为a Date然后获取毫秒.要将a转换String为a Date,反之亦然,您应该使用SimpleDateFormatclass.
以下是您想要/需要做的示例(假设此处不涉及时区):
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();
Run Code Online (Sandbox Code Playgroud)
但是,要小心,因为在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();
Run Code Online (Sandbox Code Playgroud)
在SimpleDateFormat的类可以解析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();
Run Code Online (Sandbox Code Playgroud)
请注意,这为您提供了一个long而不是双倍,但我认为这可能是您的意图。SimpleDateFormat该类的文档有大量关于如何设置它以解析不同格式的信息。
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.
Run Code Online (Sandbox Code Playgroud)
1414602645000
公认的答案是正确的,只是它忽略了时区这一关键问题。您的输入字符串是下午6:10在巴黎还是在蒙特利尔?还是UTC?
使用正确的时区名称。通常是一个大陆加上城市/地区。例如,"Europe/Oslo"。避免使用既不是标准化也不是唯一的3或4个字母代码。
现代方法使用java.time类。
更改输入以符合ISO 8601标准。用替换中间的空格T。并用连字符替换斜杠字符。解析/生成字符串时,java.time类默认使用这些标准格式。因此,无需指定格式化模式。
String input = "2014/10/29 18:10:45".replace( " " , "T" ).replace( "/" , "-" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;
Run Code Online (Sandbox Code Playgroud)
一LocalDateTime,喜欢你的输入字符串,没有时区的任何概念或偏移从-UTC。没有区域/偏移量的上下文,a LocalDateTime并不具有实际意义。是印度,欧洲或加拿大的下午6:10?这些地点中的每个地点在时间轴上的不同时间在不同时刻经历6:10 PM。因此,如果要确定时间线上的特定点,则必须指定要记住的一点。
ZoneId z = ZoneId.of( "Europe/Oslo" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
Run Code Online (Sandbox Code Playgroud)
现在,我们有一个特定的时刻ZonedDateTime。通过提取转换为UTC Instant。该Instant级表示时间轴上的时刻UTC,分辨率为纳秒(最多小数的9个位数)。
Instant instant = zdt.toInstant() ;
Run Code Online (Sandbox Code Playgroud)
现在,我们可以获取自1970年1月的UTC 1970-01-01T00:00Z的纪元参考以来的毫秒数。
long millisSinceEpoch = instant.toEpochMilli() ;
Run Code Online (Sandbox Code Playgroud)
注意可能的数据丢失。该Instant对象能够承载比毫秒更精细的微秒或纳秒。当获得毫秒数时,秒的小数部分将被忽略。
该java.time框架是建立在Java 8和更高版本。这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat。
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
要了解更多信息,请参见Oracle教程。并在Stack Overflow中搜索许多示例和说明。规格为JSR 310。
您可以直接与数据库交换java.time对象。使用与JDBC 4.2或更高版本兼容的JDBC驱动程序。不需要字符串,不需要类。java.sql.*
在哪里获取java.time类?
该ThreeTen-额外项目与其他类扩展java.time。该项目为将来可能在java.time中添加内容提供了一个试验场。你可能在这里找到一些有用的类,比如Interval,YearWeek,YearQuarter,和更多。
更新:该乔达时间的项目现在处于维护模式,与团队的建议迁移java.time类。我将保留此部分的历史记录。
下面是相同类型的代码,但使用的是Joda-Time 2.5库和处理时区。
众所周知,java.util.Date,.Calendar和.SimpleDateFormat类麻烦,混乱且有缺陷。避免他们。使用Java 8中内置的Joda-Time或java.time包(受Joda-Time启发)。
您的字符串几乎采用ISO 8601格式。斜线必须是连字符,中间的SPACE应该用代替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();
Run Code Online (Sandbox Code Playgroud)
2017 年的答案是:使用 Java 8 中引入的日期和时间类(并且也在ThreeTen Backport 中向后移植到 Java 6 和 7 )。
如果要解释计算机时区中的日期时间字符串:
long millisSinceEpoch = LocalDateTime.parse(myDate, DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss"))
.atZone(ZoneId.systemDefault())
.toInstant()
.toEpochMilli();
Run Code Online (Sandbox Code Playgroud)
如果是其他时区,请填写该时区而不是ZoneId.systemDefault()。如果是 UTC,请使用
long millisSinceEpoch = LocalDateTime.parse(myDate, DateTimeFormatter.ofPattern("uuuu/MM/dd HH:mm:ss"))
.atOffset(ZoneOffset.UTC)
.toInstant()
.toEpochMilli();
Run Code Online (Sandbox Code Playgroud)