[英]Convert String to java.util.Date in the (yyyy-MM-dd) format with return type of Date
I am trying to convert String to java.util.date this way, 我正在尝试通过这种方式将String转换为java.util.date,
My Input string is :"2013-09-18" 我的输入字符串是:“ 2013-09-18”
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateAndTime {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse("2013-09-18");
String strDate = new SimpleDateFormat("yyyy-MM-dd").format(convertedCurrentDate);
System.out.println(strDate);
}
}
Here convertedCurrentDate is giving output as ""Wed Sep 18 00:00:00 IST 2013"". 在这里,convertedCurrentDate的输出为““ IST 2013年9月18日00:00:00星期三”。
Actually I want to get output this way, 2013-09-18 and not ""Wed Sep 18 00:00:00 IST 2013"" 实际上,我想以这种方式获得输出,2013-09-18,而不是““ Wed Sep 18 00:00:00 IST 2013”“
So, I tried this way, 所以,我尝试过这种方式
String strDate = new SimpleDateFormat("yyyy-MM-dd").format(convertedCurrentDate);
System.out.println(strDate);
This is giving me output as 2013-09-18. 这给我的输出为2013-09-18。
But the issue is I am getting this '2013-09-18' as string. 但是问题是我将这个'2013-09-18'作为字符串。
What i want is 2013-09-18 with java.util.date datatype and not String 我想要的是2013-09-18使用java.util.date数据类型而不是String
ie, 即
need to cast strDate to java.util.date.
Can anyone help me in this issue? 谁能帮助我解决这个问题?
Thanks 谢谢
Reason: 原因:
When using println java explicitly calls toString method on that object. 使用println时,java会在该对象上显式调用toString方法。 When calling toString on Date object it may be using its default date format which leads to above output.
在Date对象上调用toString时,它可能使用其默认日期格式,从而导致上述输出。
Solution: For just printing purpose do what your doing 解决方案:仅出于打印目的,请执行您的操作
String strDate = new SimpleDateFormat("yyyy-MM-dd").format(convertedCurrentDate);
System.out.println(strDate);
When you invoke System.out.println on a Date object the String representation of that object is printed. 在Date对象上调用System.out.println时,将打印该对象的String表示形式。 This is done by calling method toString() of the corresponding Date object.
这是通过调用相应的Date对象的toString()方法来完成的。 The String representation of the Date object is predefined by the implementer of that class so you will always get a format like "Wed Sep 18 00:00:00 IST 2013"
Date对象的String表示形式由该类的实现者预定义,因此您将始终获得“ Wed Sep 18 00:00:00 IST 2013”之类的格式。
You should always invoke the following on the Date object in order to print it in the format you want: 您应该始终在Date对象上调用以下内容,以便以所需的格式打印它:
new SimpleDateFormat("yyyy-MM-dd").format(dateObject)
To make your life easier, create a static object of the Date formatter and call it everywhere in your code like: 为了使您的生活更轻松,请创建一个Date格式化程序的静态对象,并在代码中的任何地方调用它,例如:
public static SimpleDateFormat MY_DATE_FORMATTER= new SimpleDateFormat("yyyy-MM-dd");
And call it like: 并这样称呼:
System.out.println(MY_DATE_FORMATTER.format(dateObject));
Java不允许Date格式仅是Date部分,每当创建Date对象时,即使日期部分中也没有时间,它也应包含time部分,因此它写为00:00。因此,您不能使用格式化的Date对象,但是可以总是得到格式化的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.