java 为什么 SimpleDateFormat 不能正确解析我的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4129774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is SimpleDateFormat not parsing my date correctly?
提问by Danny
I'm trying to avoid reinstalling Eclipse to solve this, so I hope someone can help with this date-parsing problem. Here's what I do.
我试图避免重新安装 Eclipse 来解决这个问题,所以我希望有人可以帮助解决这个日期解析问题。这就是我所做的。
DateFormat dateFormat;
That's a variable in my class. I set it to the following.
这是我班上的一个变量。我将其设置为以下内容。
dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// Example input: 2010-11-07 16:00:00
When another method is called, I create a new java.sql.Date instance using strings that are passed in. For the sake of clarity, here's a shortened version.
当另一个方法被调用时,我使用传入的字符串创建一个新的 java.sql.Date 实例。为了清楚起见,这里有一个缩短的版本。
public void aMethod (String activateTime, String expireTime) {
Date example = new Date(dateFormat.parse(activateTime).getTime());
Date example2 = new Date(dateFormat.parse(expireTime).getTime());
}
When I look at the strings resulting from these operations (just adding .toString() to instantiations above), I get output like the following
当我查看这些操作产生的字符串时(只是将 .toString() 添加到上面的实例化),我得到如下输出
2010-10-30
2010-11-29
... instead of the input strings, which are reported to be...
... 而不是输入字符串,据报道是...
2010-10-30 17:00:00
2010-11-29 16:00:00
Anyone know why it doesn't give me a date in the pattern I specified?
任何人都知道为什么它没有按照我指定的模式给我一个日期?
回答by BalusC
The java.sql.Date
contains only the date part of datetime. You probably want to use java.sql.Timestamp
instead.
该java.sql.Date
只包含日期时间的日期部分。您可能想java.sql.Timestamp
改用。
Timestamp example = new Timestamp(dateFormat.parse(activateTime).getTime());
Timestamp example2 = new Timestamp(dateFormat.parse(expireTime).getTime());
It also works like that in the average SQL database. The DATE
data type represents only the date, the TIME
data type represents only the time. The TIMESTAMP
data type (sometimes called DATETIME
) represents the date and time.
它也像在普通 SQL 数据库中一样工作。该DATE
数据类型仅代表日期,TIME
数据类型仅代表时间。的TIMESTAMP
数据类型(有时称为DATETIME
)表示的日期和时间。
Note that your hour pattern is incorrect. With the given examples, you'd rather like to use HH
instead of hh
. See also the SimpleDateFormat
javadoc.
请注意,您的小时模式不正确。对于给定的示例,您更愿意使用HH
而不是hh
. 另请参阅SimpleDateFormat
javadoc。
See also:
也可以看看:
回答by Cameron Skinner
The javadocs for java.sql.Date
specify that the toString method returns a string in the format "yyyy-mm-dd", so you are seeeing the correct behaviour. The Date
object has no knowledge of the formatter you used to create it. To get output in the same format use the DateFormat.format
method rather than toString
:
用于java.sql.Date
指定 toString 方法返回格式为“yyyy-mm-dd”的字符串的 javadocs ,因此您看到了正确的行为。该Date
对象不知道您用来创建它的格式化程序。要获得相同格式的输出,请使用该DateFormat.format
方法而不是toString
:
public void aMethod (String activateTime, String expireTime) {
Date example = new Date(dateFormat.parse(activateTime).getTime());
Date example2 = new Date(dateFormat.parse(expireTime).getTime());
System.out.println(example.toString() + " vs " + dateFormat.format(example));
System.out.println(example2.toString() + " vs " + dateFormat.format(example2));
}
回答by Maurizio Cucchiara
Did you try to change java.sql.Date to java.util.Date?
您是否尝试将 java.sql.Date 更改为 java.util.Date?