简体   繁体   English

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

[英]Parse String date in (yyyy-MM-dd) format

I have a string in the form "2013-09-18". 我有一个“2013-09-18”形式的字符串。 I want to convert it into a java.util.Date. 我想将其转换为java.util.Date。 I am doing this 我这样做

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' convertedCurrentDate将于'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的形式。

You may need to format the 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

tl;dr TL;博士

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

… and … ......而且......

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

java.time 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 8601 format, YYYY-MM-DD. 您的输入字符串符合标准ISO 8601格式YYYY-MM-DD。 The java.time classes use ISO 8601 formats by default when parsing and generating string representations of date-time values. 在解析和生成日期时间值的字符串表示时,java.time类默认使用ISO 8601格式。 So no need to specify a formatting pattern. 因此无需指定格式化模式。

LocalDate

The LocalDate class 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.time framework is built into Java 8 and later. java.time框架内置于Java 8及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat . 这些类取代了麻烦的旧遗留日期时间类,如java.util.DateCalendarSimpleDateFormat

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes. 现在处于维护模式Joda-Time项目建议迁移到java.time类。

To learn more, see the Oracle Tutorial . 要了解更多信息,请参阅Oracle教程 And search Stack Overflow for many examples and explanations. 并搜索Stack Overflow以获取许多示例和解释。 Specification is JSR 310 . 规范是JSR 310

You may exchange java.time objects directly with your database. 您可以直接与数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later. 使用符合JDBC 4.2或更高版本的JDBC驱动程序 No need for strings, no need for java.sql.* classes. 不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes? 从哪里获取java.time类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目使用其他类扩展了java.time。 This project is a proving ground for possible future additions to java.time. 该项目是未来可能添加到java.time的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more . 您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

You are creating a Date object, which is a representation of a certain point in the timeline. 您正在创建一个Date对象,它表示时间轴中的某个点。 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. 因为您从仅包含日期的一部分的字符串初始化它,所以缺少的数据将被默认。

I assume you are then "printing" this Date object, but without actually specifying a format like you did when parsing it. 我假设你正在“打印”这个Date对象,但没有像解析它那样实际指定格式。 Use the same SimpleDateFormat but call the reverse method, format(Date) as Holger suggested 使用相同的SimpleDateFormat,但调用反向方法,格式(日期)为Holger建议

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

I convert String to Date in format ("yyyy-MM-dd") to save into Mysql data base . 我将String格式转换为Date(“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它是我日期格式的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM