在时代转换日期格式 [英] Convert a date format in epoch

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

问题描述

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

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 from a String:

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

public static void main(String[] args) throws Exception
{
    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天全站免登陆