SimpleDateFormat似乎给出不正确的结果 [英] SimpleDateFormat appears to give an incorrect result

查看:290
本文介绍了SimpleDateFormat似乎给出不正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Java 7和Clojure 1.4 for this。



我在Clojure中写一些数据库测试用于包含



我需要比较日期我收到( Date 对象)与 String - 所以大概需要将该字符串转换为 Date 对象。经过一些谷歌搜索,我发现了Java的 SimpleDateFormat



这是我使用的/ p>

 (defn parseDate [date] 
(do(debug(strParsing date:date))
(let [dateobj(java.text.SimpleDateFormat。dd-MMM-YY)
parsed(do(。dateobj setLenient false)(。dateobj parse date))]
结果:parsed))parsed)))

我抛出一些日期,输出..

 解析日期:01-jan-12 
结果:Mon Jan 02 00:00:00 GMT 2012
解析日期:01-jan-13
结果:2012年12月31日00:00:00 GMT 2012
解析日期:00-jan-12
结果:Mon Jan 02 00:00:00 GMT 2012
解析日期:02-jan-13
结果:12月31日00:00:00 GMT 2012

这似乎不对。



日期 object返回的是这样的:#< Date Mon Jan 02 00:00:00 GMT 2012> ,这显然不等于我得到的从数据库中,例如#< Date 2012-01-01>





注意:我得到相同的结果,无论是使用 setLenient



回答(感谢Jon Skeet 链接回答



我在我的格式字符串中使用YY,我应该实际使用yy

我相信问题是你使用YY而不是使用YY,而不是使用YY。 yy。从 SimpleDateFormat 的文档, YY 是指周年而不是年份。



周 - 年有些奇怪,除非你想要使用它,你可能不会。特别是,混合周年 - 月和月几乎是不合适的。通常一起使用周年,周年,周日。



换句话说:这两个系统是一个位像RGB和HSV的颜色;它就好像你定义了一个红,绿,色的格式,这没有什么意义:)


I'm using Java 7 and Clojure 1.4 for this.

I'm writing up some database tests in Clojure for a table that contains Date objects, using OracleDB over JDBC.

I need to compare the date I receive (a Date object) with a String - so presumably I need to convert that string to a Date object. After some googling, I found Java's SimpleDateFormat.

This is what I use (with extra stuff for debugging)

(defn parseDate [date]
  (do (debug (str "Parsing date: " date ))
    (let [ dateobj (java.text.SimpleDateFormat. "dd-MMM-YY")
           parsed (do (. dateobj setLenient false) (. dateobj parse date))]
      (debug (str "Result: " parsed)) parsed)))

I throw in some dates, and I get the following output..

Parsing date: 01-jan-12
Result: Mon Jan 02 00:00:00 GMT 2012
Parsing date: 01-jan-13
Result: Mon Dec 31 00:00:00 GMT 2012
Parsing date: 00-jan-12
Result: Mon Jan 02 00:00:00 GMT 2012
Parsing date: 02-jan-13
Result: Mon Dec 31 00:00:00 GMT 2012

That doesn't seem right, at all.

The Date object returned is something like this: #<Date Mon Jan 02 00:00:00 GMT 2012>, which is clearly not equal to what I get back from the database, for example, #<Date 2012-01-01>.

Does anyone have any ideas about this?

NOTE: I get the same result whether I use setLenient or not (and with either true or false).

Answer (Courtesy of Jon Skeet Link to answer)

I was using YY in my format string, where I should have actually used yy (Since Y is the week-year and y is the simple year).

解决方案

I believe the problem is that you're using YY instead of yy. From the docs of SimpleDateFormat, YY refers to the week-year rather than the year.

Week-years are somewhat odd, and unless you know you want to use it, you probably don't. In particular, mixing week-year with "day of month" and "month" is almost never appropriate. You'd normally use "week-year, week-of-year, day-of-week" together.

To put it another way: the two systems are a bit like RGB and HSV for colours; it's as if you've defined a format of "red, green, hue" which doesn't make a lot of sense :)

这篇关于SimpleDateFormat似乎给出不正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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