在 JAVA 中将字符串转换为日期格式 (yyyy/MM/dd)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36399119/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:24:00  来源:igfitidea点击:

Converting string to date format (yyyy/MM/dd) in JAVA

javadatesql-updatesimpledateformat

提问by Shashi Kiran

I am facing an issue when I convert my string to date format.

将字符串转换为日期格式时遇到问题。

This is what I have tried -

这是我尝试过的-

First try:

第一次尝试:

        String completionDate1 = request.getParameter("completion_date");
        System.out.println(completionDate1); // O/P --> 21/10/2016 (Correct)
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        date = df.parse(completionDate1);
        System.out.println(date); // O/P --> Tue Apr 08 00:00:00 IST 27 (Inorrect)

Second try:

第二次尝试:

        String completionDate1 = request.getParameter("completion_date");
        System.out.println(completionDate1); // O/P --> 21/10/2016 (Correct)
        Date completionDate = new SimpleDateFormat("yyyy/MM/dd").parse(completionDate1);
        System.out.println(completionDate); // O/P --> Tue Apr 08 00:00:00 IST 27 (Inorrect)

The output I'm expecting is that, the completionDateshould be Dateformat not String. I'm just printing the completionDate just to make sure the date format is getting getting converted.

我期待的输出是,completionDate应该是Dateformat not String。我只是打印完成日期只是为了确保日期格式得到转换。

The main intention of getting it in date format is I need this for an Update query so it has to be Date and not String.

以日期格式获取它的主要目的是我需要它来进行更新查询,因此它必须是日期而不是字符串。

Kindly let me know where I'm going wrong. I need this date format as I need to store this date in a database.

请让我知道我哪里出错了。我需要这种日期格式,因为我需要将此日期存储在数据库中。

Thanks

谢谢

回答by silentprogrammer

You have to format the date after parsing it but your parsing format is also incorrect so its giving wrong output. Try

您必须在解析后格式化日期,但您的解析格式也不正确,因此它给出了错误的输出。尝试

 String completionDate1 = "21/10/2016";
 System.out.println(completionDate1);
 DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
 Date date = new Date();
 date = df.parse(completionDate1);
 DateFormat df1 = new SimpleDateFormat("yyyy/MM/dd");
 System.out.println(df1.format(date));

DEMO

演示

回答by Basil Bourque

Wrong pattern

错误的模式

First problem is that your defined parsing pattern does not match your input string. For 21/10/2016pattern should be dd/MM/yyyy.

第一个问题是您定义的解析模式与您的输入字符串不匹配。对于21/10/2016模式应该是dd/MM/yyyy.

java.sql.Date versus java.util.Date

java.sql.Date 与 java.util.Date

Second problem is that for database access you should be using the java.sql.Dateclass for a date-only value without time-of-day and without time zone. Not to be confused with java.util.Datewhich is a date andtime-of-day value. You should specify the fully-qualified class name in such discussions as this Question.

第二个问题是,对于数据库访问,您应该将该java.sql.Date类用于没有时间和时区的仅日期值。不要与java.util.Date日期时间值混淆。您应该在像这个问题这样的讨论中指定完全限定的类名。

Neither of these classes have a “format”. They have their own internal representation, a count from epoch.

这些类都没有“格式”。它们有自己的内部表示,从epoch 开始计数。

java.time

时间

Third problem is that you are using old outdated classes. In Java 8 and later, use java.timeframework now built-in. In Java 6 & 7, use its back-port, the ThreeTen-Backportproject. For Android, use the adaptation of that back-port, ThreeTenABP.

第三个问题是您使用的是过时的旧类。在 Java 8 及更高版本中,使用现在内置的java.time框架。在 Java 6 & 7 中,使用它的 back-port,ThreeTen-Backport项目。对于 Android,请使用该后向端口 ThreeTenABP 的改编版本。

In the java.time classes we now have the LocalDateclass for date-only values.

在 java.time 类中,我们现在拥有LocalDate仅用于日期值的类。

String input = "21/10/2016"
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "dd/MM/yyyy" );
LocalDate localDate = LocalDate.parse( input , formatter );

Hopefully some day JDBC drivers will be updated to directly use the java.time types. Until then, use the new methods added to the old java.sql types to convert.

希望有一天 JDBC 驱动程序将更新为直接使用 java.time 类型。在此之前,使用添加到旧 java.sql 类型的新方法进行转换。

java.sql.Date sqlDate = java.sql.Date.valueOf( localDate );

Now pass that java.sql.Dateobject to the setDatemethod on a PreparedStatementobject for transmission to a database.

现在将该java.sql.Date对象传递给对象上的setDate方法PreparedStatement以传输到数据库。

Note that at no time did we make use of any more strings. We went from original input Stringto java.time.Dateto java.sql.Date.

请注意,我们再也没有使用过任何字符串。我们从原来的输入就Stringjava.time.Datejava.sql.Date

At no point did we use java.util.Date. Check your importstatements to eliminate java.util.Date.

我们从未使用过java.util.Date. 检查您的import报表以消除java.util.Date.

By the way, to go the other direction from java.sql.Dateto LocalDate:

顺便说一句,从java.sql.Date到的另一个方向LocalDate

LocalDate localDate = mySqlDate.toLocalDate();

回答by Jeet

Use below code

使用下面的代码

 String completionDate1 = request.getParameter("completion_date");
 System.out.println(completionDate1); // O/P --> 21/10/2016 (Correct)
 Date completionDate = new SimpleDateFormat("dd/MM/yyyy").parse(completionDate1);

 String date = new SimpleDateFormat("yyyy/MM/dd").format(completionDate);
 System.out.println(date);

回答by Julian S

You can try this

你可以试试这个

DateFormat df = new SimpleDateFormat("yyyy/mm/dd");
String completionDate = df.format(request.getParameter("completion_date"));
System.out.println(completionDate);