java使用值字符串创建日期对象 [英] java create date object using a value string
问题描述
我正在使用它来获取当前时间:
I am using this to get the current time :
java.util.Calendar cal = java.util.Calendar.getInstance();
System.out.println(new java.text.SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss")
.format(cal.getTime()));
我想将值(我打印它)放入日期对象中,我尝试过:
I want to put the value (which I print it) into a date object, I tried this:
Date currentDate = new Date(value);
但是eclipse告诉我这个功能不好。
but eclipse tells me that this function is not good.
编辑
值
是使用system.out.println打印给您的值
Edit
the value
is the value that I printed to you using system.out.println
推荐答案
无论何时要将String转换为Date对象,然后使用 SimpleDateFormat#parse
br>
尝试使用
Whenever you want to convert a String to Date object then use SimpleDateFormat#parse
Try to use
String dateInString = new java.text.SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss")
.format(cal.getTime())
SimpleDateFormat formatter = new SimpleDateFormat("EEEE, dd/MM/yyyy/hh:mm:ss");
Date parsedDate = formatter.parse(dateInString);
。另外的事情是如果你想转换一个 Date
到 String
然后你应该使用 SimpleDateFormat#格式
函数。
现在你的点是
new Date(String)
已被弃用,现在不推荐。只要有人想要解析,那么他/她应该使用 SimpleDateFormat#parse
。
.Additional thing is if you want to convert a Date
to String
then you should use SimpleDateFormat#format
function.
Now the Point for you is
new Date(String)
is deprecated and not recommended now.Now whenever anyone wants to parse , then he/she should use SimpleDateFormat#parse
.
这篇关于java使用值字符串创建日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!