将字符串日期以dd / MM / yyyy格式转换为java.util.Date [英] Convert String date into java.util.Date in the dd/MM/yyyy format
问题描述
我在网上阅读更多问题,但我没有找到解决方案。
I read more questions on the web but I dont' find a solution yet.
我有一个 String ,如14/05 / 1994,完全以这种格式。
I have a String like "14/05/1994", exactly in this format.
我需要将其隐藏为相同格式的 java.util.Date / strong>。
I need to covert it into java.util.Date in the same format.
我试过:
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");
但结果是:Sat May 14 00:00:00 CET 1994
But the result is: Sat May 14 00:00:00 CET 1994
可能的结果是:14/05/1994不是一个String,而是作为java.util.Date?
It's possibile have as a result: 14/05/1994 not as a String, but as java.util.Date?
推荐答案
A java.util.Date
不有格式。这只是一个日期。
A java.util.Date
doesn't have a format. It's just a date.
当您打印出来,例如使用 toString()
,它使用默认格式,这是你看到的。但你有那个日期。
When you print it out, e.g. using toString()
, it uses a default format, which is what you're seeing. But you have that date.
Date dataFrom = new Date();
dataFrom = df.format("14/05/1994");
我不认为这可以是你的代码,因为 DateFormat.format
接受 Date
并返回一个 String
,而不是其他方式。您可能意味着 df.parse
,这将使您得到您所描述的结果。但是,如果您在 Date $ c中使用
SimpleDateFormat
并使用其格式
方法$ c>,那么你应该根据需要退回 14/05/1994
。不过, java.util.Date
不 有一种格式。
I don't think that can be your code because DateFormat.format
accepts a Date
and returns a String
, not the other way around. You might mean df.parse
, which would get you the results you describe. But if you take your SimpleDateFormat
and use its format
method on the Date
, then you should get back out 14/05/1994
as you want. A java.util.Date
doesn't have a format, though.
这篇关于将字符串日期以dd / MM / yyyy格式转换为java.util.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!