在纪元中转换日期格式 [英] Convert a date format in epoch

查看:50
本文介绍了在纪元中转换日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日期格式的字符串,例如

I have a string with a date format such as

Jun 13 2003 23:11:52.454 UTC

包含毫秒...我想在纪元中转换.我可以使用 Java 中的实用程序进行此转换吗?

containing millisec... which I want to convert in epoch. Is there an utility in Java I can use to do this conversion?

推荐答案

这段代码展示了如何使用 java.text.SimpleDateFormat 解析 java.util.Date 来自字符串:

This code shows how to use a java.text.SimpleDateFormat to parse a java.util.Date from a String:

String str = "Jun 13 2003 23:11:52.454 UTC";
SimpleDateFormat df = new SimpleDateFormat("MMM dd yyyy HH:mm:ss.SSS zzz");
Date date = df.parse(str);
long epoch = date.getTime();
System.out.println(epoch); // 1055545912454

Date.getTime() 以毫秒为单位返回纪元时间.

Date.getTime() returns the epoch time in milliseconds.

这篇关于在纪元中转换日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆