How to convert Date object format in another Data object format in Java?(No string) -
i know maybe replicated question, want parse string convert date object.i'm able doing:
simpledateformat parser = new simpledateformat("yyyy-m-d (hh:mm:ss.s)"); date date = parser.parse(propertiesvalue[i]);
and returns "date" in format: fri jan 30 13:55:00 cet 2015 want return like:2015-01-30 (13:55:00.00) data object (not string object). need insert date in local google datastore.
string != datetime
do not conflate date-time object string may represent value.
a date-time object has no format. date-time format represents moment. date-time object can generate string. date-time object can created parsing string. date-time object , string separate , distinct.
avoid legacy classes
you using troublesome old date-time classes legacy, supplanted java.time classes.
using java.time
instant instant = instant.now(); // current moment in utc zoneid z = zoneid.of( "pacific/auckland" ); zoneddatetime zdt = instant.atzone( z ); // same moment viewed wall-clock time of particular region.
generate string represent object’s value in format.
datetimeformatter f = datetimeformatter.ofpattern( "uuuu-m-d (hh:mm:ss.s)" , locale.us ); string output = zdt.format( f );
dump console.
system.out.println( "instant.tostring(): " + instant ); // standard iso 8601 format. system.out.println( "zdt.tostring(): " + zdt ); // standard iso 8601 format. system.out.println( "output: " + output ); // custom format.
see code run live @ ideone.com.
instant.tostring(): 2017-04-15t06:00:58.521z
zdt.tostring(): 2017-04-15t18:00:58.521+12:00[pacific/auckland]
output: 2017-4-15 (18:00:58.5)
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….
Comments
Post a Comment