格式为“yyyy-mm-dd HH:mm:ss”的Java String to Date对象 [英] Java String to Date object of the format "yyyy-mm-dd HH:mm:ss"

查看:100
本文介绍了格式为“yyyy-mm-dd HH:mm:ss”的Java String to Date对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将包含日期的String转换为日期对象。字符串的格式为yyyy-mm-dd HH:mm:ss.SSSSSS,我想在日期对象中使用相同的格式。

I need to convert a String containing date into an date object. The String will be of the format "yyyy-mm-dd HH:mm:ss.SSSSSS" and I want the same format in an date object.

例如我有一个字符串2012-07-10 14:58:00.000000,我需要的结果日期对象是相同的格式。

For instance I have a string "2012-07-10 14:58:00.000000", and I need the resultant date object to be of the same format.

我已经尝试了下面方法,但结果不如预期。

I have tried the below methods but, the resultant is not as expected.

java.util.Date temp = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss.SSSSSS").parse("2012-07-10 14:58:00.000000");

DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
                Date thisDate = dateFormat.parse("2012-07-10 14:58:00.000000");

结果是1月10日14:58:00 EST 2012。请让我知道我在哪里错了。

The result is "Tue Jan 10 14:58:00 EST 2012". Please let me know where I am going wrong.

谢谢,
Yeshwanth Kota

Thanks, Yeshwanth Kota

推荐答案

java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS").parse("2012-07-10 14:58:00.000000");

mm 是你想要的分钟 MM

代码

public class Test {

    public static void main(String[] args) throws ParseException {
        java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS")
                .parse("2012-07-10 14:58:00.000000");
        System.out.println(temp);
    }
}

打印:


Tue Jul 10 14:58:00 EDT 2012

Tue Jul 10 14:58:00 EDT 2012

这篇关于格式为“yyyy-mm-dd HH:mm:ss”的Java String to Date对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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