简体   繁体   English

以(yyyy-MM-dd)格式将String转换为java.util.Date,返回类型为Date

[英]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.

相关问题 将java.util.Date转换为yyyy-MM-dd格式的String,而不创建大量对象 - Convert java.util.Date to String in yyyy-MM-dd format without creating a lot of objects 将YYYY-MM-DD HH:MM:SS格式的java.util.Date转换为与MySQL DATETIME兼容 - Convert java.util.Date in YYYY-MM-DD HH:MM:SS format to compatible with MySQL DATETIME java.util.Date 格式转换 yyyy-mm-dd 到 mm-dd-yyyy - java.util.Date format conversion yyyy-mm-dd to mm-dd-yyyy 如何将 java.util.Date 转换为 soap 支持的日期格式“yyyy-MM-dd'T'HH:mm:ss”与区域 id - How to convert java.util.Date to soap suppported date format “yyyy-MM-dd'T'HH:mm:ss” with zone id 如何将dd / MM / yyyy格式的java.sql.Date转换为java.util.Date? - How to convert java.sql.Date to java.util.Date in dd/MM/yyyy format? 无法使用@DateTimeFormat(pattern =“dd / MM / yyyy”)在java.util.Date中转换字符串 - Failed to convert string in java.util.Date with @DateTimeFormat(pattern=“dd/MM/yyyy”) java.sql.Date (YYYY-MM-DD) 到 java.util.Date (DD-MMM-YYYY) 的转换 - java.sql.Date (YYYY-MM-DD) to java.util.Date (DD-MMM-YYYY) conversion 如何将用户以MM-dd-yyyy格式选择的日期转换为Java中的yyyy-MM-dd格式 - How to convert a date a user selects in MM-dd-yyyy format to the format yyyy-MM-dd in Java 如何在ruby中将java.util.Date格式化为“MM / dd / yyyy”格式的日期? - How to format java.util.Date to a date in “MM/dd/yyyy” format in ruby? 如何将 YY/MM/DD 类型的字符串转换为 java.util.Date? - How to convert string of type YY/MM/DD to java.util.Date?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM