Java SimpleDateFormat格式问题与yyyy [英] Java SimpleDateFormat format issue with yyyy

查看:37
本文介绍了Java SimpleDateFormat格式问题与yyyy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理SimpleDateFormat对象的 format 方法时遇到麻烦.

I'm having trouble with the format method of a SimpleDateFormat object.

有问题的代码是:

new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(date);

其中"date"是使用Calendar.getTimeInMillis()中的long int创建的Date对象.

Where "date" is a Date object created using a long int from Calendar.getTimeInMillis();

Calendar cal = Calendar.getInstance();
Date date = new Date(cal.getTimeInMillis());

除字符串的年份部分外,其他所有东西都可以正常工作.当我传递日期时,输出的字符串如下所示:

Everything is working fine except the year portion of the string. When I pass the date, the string outputted looks like this:

0044-09-10 05:30:24 

传递的日期对象是由以下类型的长整数创建的:

The date object that is passed is created from a long integer returned from:

Calendar.getTimeInMillis();

我相信从此方法返回的数字是从1月1日算起的毫秒数.1970年,我想这是个问题,但是即使我加上1970年的毫秒数(大声笑,可能做错了事,但值得一试:P),它仍然解析为0044年部分.

I believe the number returned from this method counts the milliseconds from Jan. 1st. 1970, which I'm thinking is the problem, but even if I add the number of milliseconds in 1970 years (lol, probably the wrong thing to do, but it was worth a shot :P), it still parses as 0044 in the year portion.

我已经完成了许多Google搜索,所有这些搜索似乎都指向传递给SimpleDateFormat构造函数的字符串中的简单问题,所以在这里我很茫然.

I've done numerous google searches and all seem to point simple issues in the string passed to the SimpleDateFormat constructor, so I'm at a loss here.

非常感谢您的帮助.

请让我知道是否需要其他信息,我会尽力提供.

Please let me know if any other information is needed and I will do my best to provide it.

再次感谢!

我想我会再解释一下上下文:

I figured I would explain the context a little more:

技术人员将进行呼叫,并在到达现场后将呼叫标记为已开始".然后,该应用程序将创建一个 Calendar 实例,并保存从 Calendar.getTimeInMillis()返回的long int.

A technician will run a call, and on arriving to the site, will mark the call as "Started." The app will then create a Calendar instance and save the long int returned from Calendar.getTimeInMillis().

技术人员完成呼叫后,将把呼叫标记为完成".再次,创建一个 Calendar 实例,并保存从 Calendar.getTimeInMillis()返回的long int.

When the technician is finished with the call, he/she will mark the call as "Complete." Again, a Calendar instance is created and the long int returned from Calendar.getTimeInMillis() is saved.

这使得轻松进行通话时间"计算.

This allows easy "call duration" calculation.

现在,当使用技术人员在应用程序中使用的结束表格时,将在调用过程中保存的长整数传递给 Date 对象,然后将其传递给 SimpleDateFormat.format()方法.

Now, when the closing form that the technician uses in the app is used, the long ints saved during the calls is passed to a Date object, which is then passed to a SimpleDateFormat.format() method.

这就是问题所在.再次感谢,希望能对您有所帮助.

This is where the issue arises. Thanks again, hope this helped.

推荐答案

要计算日期/时间之间的持续时间,请不要依赖于减去毫秒,这是非常不可靠的.

To calculate duration between date/times, don't rely on subtracting milliseconds, it is highly unreliable.

如果您使用的是Java 8,则应利用新的 java.time API,例如...

If you're using Java 8, you should take advantage of the new java.time API, for example...

LocalDateTime dt1 = LocalDateTime.of(2014, 9, 11, 10, 0);
LocalDateTime dt2 = LocalDateTime.of(2014, 9, 11, 12, 0);

Duration duration = Duration.between(dt1, dt2);
System.out.println(duration.toDays());
System.out.println(duration.toHours());
System.out.println(duration.toMinutes());
System.out.println(duration.getSeconds());

哪个输出...

0
2
120
7200

有关更多详细信息,请参见时间段和持续时间

Take a look at Period and Duration for more details.

或者,您可以使用 JodaTime ( java.time code>是基于)

Alternatively, you could use JodaTime (which the java.time is based off)

这篇关于Java SimpleDateFormat格式问题与yyyy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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