Java Entity - storing dates -
how can store dates in format 14-04-2017 in entities?
but have parse string.
simpledateformat dateformat = new simpledateformat("yyyy-mm-dd"); try { today = dateformat.parse("2017-04-14"); } catch (parseexception e) { //catch exception } and get: fri apr 14 00:00:00 cest 2017
tl;dr
localdate.parse( "2017-04-14" ) using java.time
you using wrong classes.
avoid troublesome old legacy classes such date, calendar, , simpledateformat. supplanted java.time classes.
localdate
the localdate class represents date-only value without time-of-day , without time zone.
iso 8601
your input happens comply iso 8601 standard. java.time classes use standard formats default when parsing/generating strings. no need specify formatting pattern.
localdate ld = localdate.parse( "2017-04-14" ); to generate string in same format, call tostring.
string output = ld.tostring() ; do not conflate strings date-time objects. date-time objects can created parsing string. date-time objects can generate strings represent value. string , date-time separate , distinct.
your first line in question uses different format. hope mistake. should stick iso 8601 formats whenever possible, especially when serializing data exchange.
if want other formats, search datetimeformatter. covered many many times on stack overflow.
about java.time
the java.time framework built java 8 , later. these classes supplant troublesome old legacy date-time classes such java.util.date, calendar, & simpledateformat.
the joda-time project, in maintenance mode, advises migration java.time classes.
to learn more, see oracle tutorial. , search stack overflow many examples , explanations. specification jsr 310.
where obtain java.time classes?
- java se 8, java se 9, , later
- built-in.
- part of standard java api bundled implementation.
- java 9 adds minor features , fixes.
- java se 6 , java se 7
- much of java.time functionality back-ported java 6 & 7 in threeten-backport.
- android
- the threetenabp project adapts threeten-backport (mentioned above) android specifically.
- see how use threetenabp….
the threeten-extra project extends java.time additional classes. project proving ground possible future additions java.time. may find useful classes here such interval, yearweek, yearquarter, , more.
Comments
Post a Comment