SimpleDateFormat显示分钟,秒和毫秒错误 [英] SimpleDateFormat showing minutes, seconds and milliseconds wrong

查看:471
本文介绍了SimpleDateFormat显示分钟,秒和毫秒错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了这个示例程序,我想将日期转换为另一种格式。使用简单的日期格式时,我看不到预期的日期。

I have written this sample program where I want to convert a date into another format. I don't see the expected date when using simple date format.

 public class TestDate {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SimpleDateFormat originalformat = new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSSSSS");
        SimpleDateFormat targetformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
        try {

            //Use Simple Date Format
            Date date = originalformat.parse("2015-04-09-17.18.48.677862");
            System.out.println("Using SDF "+targetformat.format(date));

            //Use Manual Translation
            String eventTime = "2015-04-09-17.18.48.677862";
            StringBuffer timeBuffer = new StringBuffer();
            for (int i = 0; i < eventTime.length(); i++) {
                if (i == 10) {
                    timeBuffer.append(" ");
                    continue;
                } else if (i == 13 || i == 16) {
                    timeBuffer.append(":");
                    continue;
                } else {
                    timeBuffer.append(eventTime.charAt(i));
                }
            }
            timeBuffer.append("000");
            String transformedTime = timeBuffer.toString().trim();
            System.out.println("Manual Translation "+transformedTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

我看到以下输出。第一个逻辑是使用简单的日期格式,第二个是手动翻译。

I see the following outputs. The first logic is using simple date format and the second one is manual translation.

Using SDF 2015-04-09 17:30:05.000862
Manual Translation 2015-04-09 17:18:48.677862000

那么如何使简单的日期格式输出一个完全相似的值,如手册一个

So How to make the simple date format to output a exactly similar value like the manual one

推荐答案

看起来你不能使用6数字为毫秒。你的节目是11分17秒。 660秒= 11分钟,你有17秒钟。所以它只是将输入转换成几秒钟,因为它不能接受超过3毫秒的数字。

It would appear that you can't use 6 digits for the milliseconds. Your program is 11 minutes and 17 seconds off. 660 seconds = 11 minutes and you have 17 seconds off. So its just converting your input into minutes from seconds since it can't accept more than 3 milliseconds digits.

这篇关于SimpleDateFormat显示分钟,秒和毫秒错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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