Java 以 (yyyy-MM-dd) 格式解析字符串日期

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

Parse String date in (yyyy-MM-dd) format

javadateparsing

提问by user2791546

I have a string in the form "2013-09-18". I want to convert it into a java.util.Date. I am doing this

我有一个“2013-09-18”形式的字符串。我想将其转换为 java.util.Date。我在做这个

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse(currentDateText);

The convertedCurrentDate is coming as 'Wed Sep 18 00:00:00 IST 2013'

转换后的CurrentDate 是'Wed Sep 18 00:00:00 IST 2013'

I want my output in the form '2013-09-18'

我希望我的输出格式为“2013-09-18”

Hour, minutes and seconds shouldnt come and it should be in the form yyyy-MM-dd.

小时、分钟和秒不应该出现,它应该是 yyyy-MM-dd 的形式。

回答by Ruchira Gayan Ranaweera

You may need to formatthe out put as follows.

您可能需要format按如下方式输出。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = sdf.parse("2013-09-18");
String date=sdf.format(convertedCurrentDate );
System.out.println(date);

Use

String convertedCurrentDate =sdf.format(sdf.parse("2013-09-18"));

Output:

输出:

2013-09-18

回答by Morfic

You are creating a Date object, which is a representation of a certain point in the timeline. This means that it will have all the parts necessary to represent it correctly, including minutes and seconds and so on. Because you initialize it from a string containing only a part of the date, the missing data will be defaulted.

您正在创建一个 Date 对象,它是时间线中某个点的表示。这意味着它将具有正确表示它所需的所有部分,包括分钟和秒等。因为您从仅包含日期的一部分的字符串中对其进行初始化,所以将默认丢失的数据。

I assume you are then "printing" this Date object, but without actually specifying a format like you did when parsing it. Use the same SimpleDateFormat but call the reverse method, format(Date) as Holger suggested

我假设您然后“打印”了这个 Date 对象,但实际上没有像解析它时那样指定格式。使用相同的 SimpleDateFormat 但调用相反的方法 format(Date) 作为 Holger 建议

回答by user5172411

DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String cunvertCurrentDate="06/09/2015";
Date date = new Date();
date = df.parse(cunvertCurrentDate);

回答by mahdi kallel

I convert String to Date in format ("yyyy-MM-dd") to save into Mysql data base .

我将字符串格式转换为日期(“yyyy-MM-dd”)以保存到 Mysql 数据库中。

 String date ="2016-05-01";
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 Date parsed = format.parse(date);
 java.sql.Date sql = new java.sql.Date(parsed.getTime());

sql it's my output in date format

sql 这是我的日期格式输出

回答by Basil Bourque

tl;dr

tl;博士

LocalDate.parse( "2013-09-18" )

… and …

… 和 …

myLocalDate.toString()  // Example: 2013-09-18

java.time

时间

The Question and other Answers are out-of-date. The troublesome old legacy date-time classes are now supplanted by the java.time classes.

问题和其他答案已过时。麻烦的旧日期时间类现在被 java.time 类取代。

ISO 8601

ISO 8601

Your input string happens to comply with standard ISO 8601format, YYYY-MM-DD. The java.time classes use ISO 8601 formats by default when parsing and generating string representations of date-time values. So no need to specify a formatting pattern.

您的输入字符串恰好符合标准ISO 8601格式 YYYY-MM-DD。java.time 类在解析和生成日期时间值的字符串表示时默认使用 ISO 8601 格式。所以不需要指定格式模式。

LocalDate

LocalDate

The LocalDateclass represents a date-only value without time-of-day and without time zone.

LocalDate级表示没有时间一天和不同时区的日期,唯一的价值。

LocalDate ld = LocalDate.parse( "2013-09-18" );


About java.time

关于java.time

The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date, Calendar, & SimpleDateFormat

The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.

现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310

You may exchange java.timeobjects directly with your database. Use a JDBC drivercompliant with JDBC 4.2or later. No need for strings, no need for java.sql.*classes.

您可以直接与您的数据库交换java.time对象。使用符合JDBC 4.2或更高版本的JDBC 驱动程序。不需要字符串,不需要类。java.sql.*

Where to obtain the java.time classes?

从哪里获得 java.time 类?

The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如IntervalYearWeekYearQuarter,和更多