java 如何在日期类型对象中存储日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39586036/
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
How to store date in Date type object
提问by Karmesh Madhavi
How do I store the current date in a Date object? I tried this code but it's getting an error:
如何将当前日期存储在 Date 对象中?我尝试了此代码,但出现错误:
SimpleDateFormat dtf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
Date date = new Date();
date=dtf.parse(dtf.format(( cal.get(cal.YEAR)+""+cal.get(cal.MONTH)+""+cal.get(cal.DAY_OF_MONTH))));
I want to store the formatted date of today [2016-09-20] in the "date" object.
我想将今天 [2016-09-20] 的格式化日期存储在“日期”对象中。
回答by Basil Bourque
tl;dr
tl;博士
LocalDate.parse( "2016-09-20" )
LocalDate
LocalDate
You are use troublesome old legacy classes now supplanted by java.time classes. Even worse, you are trying to cram a date-only value into a date-time object with the java.util.Date
class.
您正在使用麻烦的旧遗留类,现在已被 java.time 类取代。更糟糕的是,您正试图将一个仅限日期的值填充到具有java.util.Date
该类的日期时间对象中。
The LocalDate
class represents a date-only value without time-of-day and without time zone.
该LocalDate
级表示没有时间一天和不同时区的日期,唯一的价值。
Your input string complies with ISO 8601 standard formats. The java.time classes use ISO 8601 formats by default for parsing/generating strings. So no need to specify any formatting pattern.
您的输入字符串符合 ISO 8601 标准格式。java.time 类默认使用 ISO 8601 格式来解析/生成字符串。所以不需要指定任何格式模式。
LocalDate ld = LocalDate.parse( "2016-09-20" ) ;
To generate a String representing this value, call toString
.
要生成表示此值的字符串,请调用toString
。
String output = ld.toString() ; // 2016-09-20
About java.time
关于 java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧日期时间类,例如java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to java.time.
现在处于维护模式的Joda-Time项目建议迁移到 java.time。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backportand further adapted to Androidin ThreeTenABP(see How to use…).
大部分的java.time功能后移植到Java 6和7 ThreeTen,反向移植,并进一步用于安卓在ThreeTenABP(见如何使用......)。
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 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。
回答by venkey
SimpleDateFormat dtf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
System.out.println(dtf.format(cal.getTime()));
Date date=new Date();
date=dtf.parse(dtf.format(cal.getTime()));