如何将日期转换为毫秒tl;博士时区java.time关于 java.time 城大时间国际标准化组织 ISO 8601示例代码
我想转换为 ?我在Google上寻找它,但我只能找到如何将ms转换为日期。String myDate = "2014/10/29 18:10:45"
long ms (i.e. currentinmlilies)
注意:为了清楚起见,我想从1970 /1/1格式的日期中获取ms。
我想转换为 ?我在Google上寻找它,但我只能找到如何将ms转换为日期。String myDate = "2014/10/29 18:10:45"
long ms (i.e. currentinmlilies)
注意:为了清楚起见,我想从1970 /1/1格式的日期中获取ms。
您没有 ,您有日期的表示形式。您应该将 转换为 a,然后获取毫秒。要将 a 转换为 a,反之亦然,您应该使用 SimpleDateFormat
类。Date
String
String
Date
String
Date
以下是您想要/需要执行的操作的示例(假设此处不涉及时区):
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.
1414602645000
接受的答案是正确的,只是它忽略了时区的关键问题。您的输入字符串是在巴黎还是蒙特利尔的下午6:10?还是UTC?
使用正确的时区名称。通常是大陆加城市/地区。例如。避免使用既不标准化也不唯一的3或4个字母代码。"Europe/Oslo"
现代方法使用 java.time 类。
更改输入以符合 ISO 8601 标准。将中间的空格键替换为 .并将斜杠字符替换为连字符。默认情况下,java.time 类在解析/生成字符串时使用这些标准格式。因此,无需指定格式设置模式。T
String input = "2014/10/29 18:10:45".replace( " " , "T" ).replace( "/" , "-" ) ;
LocalDateTime ldt = LocalDateTime.parse( input ) ;
与输入字符串一样,A 缺少任何时区或与 UTC 的偏移量的概念。如果没有区域/偏移的上下文,a 就没有真正的意义。是印度、欧洲还是加拿大的下午 6:10?这些地方中的每一个都在不同的时刻,在时间轴上的不同时间点经历下午6:10。因此,如果要确定时间轴上的特定点,则必须指定要记住的点。LocalDateTime
LocalDateTime
ZoneId z = ZoneId.of( "Europe/Oslo" ) ;
ZonedDateTime zdt = ldt.atZone( z ) ;
现在我们有一个特定的时刻,在那个.通过提取 .Instant
类以 UTC 格式表示时间轴上的某个时刻,分辨率为纳秒(最多九 (9) 位小数)。ZonedDateTime
Instant
Instant instant = zdt.toInstant() ;
现在,我们可以获得自 UTC 1970 年第一个时刻的纪元参考以来所需的毫秒数,1970-01-01T00:00Z。
long millisSinceEpoch = instant.toEpochMilli() ;
请注意可能的数据丢失。该物体能够携带微秒或纳秒,比毫秒细。在获取毫秒计数时,将忽略秒的更精细的小数部分。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-Extra 项目通过其他类扩展了 java.time。这个项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如Interval
,YearWeek
,YearQuarter
等。
更新:Joda-Time项目现在处于维护模式,团队建议迁移到java.time类。我将保留这一部分不变的历史。
下面是相同类型的代码,但使用Joda-Time 2.5库和处理时区。
The java.util.Date, .日历和 .SimpleDateFormat类是出了名的麻烦,令人困惑和有缺陷。避免它们。使用Joda-Time或Java 8中内置的java.time包(受Joda-Time启发)。
您的字符串几乎采用 ISO 8601 格式。斜杠必须是连字符,中间的空格应替换为 .如果我们调整它,那么生成的字符串可以直接馈送到构造函数中,而不必费心指定格式化程序。Joda-Time使用ISO 8701格式,因为它是解析和生成字符串的默认值。T
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();