datetime - How to get the current date/time in Java -
what's best way current date/time?
it depends on form of date / time want:
if want date / time single numeric value,
system.currenttimemillis()gives that, expressed number of milliseconds after unix epoch (as javalong). value delta utc time-point, , independent of local time-zone ... assuming system clock has been set correctly.if want date / time in form allows access components (year, month, etc) numerically, use 1 of following:
new date()givesdateobject initialized current date / time. problemdateapi methods flawed ... , deprecated.calendar.getinstance()givescalendarobject initialized current date / time, using defaultlocale,timezone. other overloads allow use specificlocaleand/ortimezone. calendar works ... apis still cumbersome.new org.joda.time.datetime()gives joda-time object initialized current date / time, using default time zone , chronology. there lots of other joda alternatives ... many describe here.in java 8, calling
localdatetime.now(),zoneddatetime.now()give representations current date / time.
prior java 8, people know these things recommended joda-time having (by far) best java apis doing things involving time point , duration calculations. java 8, no longer true. however, if using joda time in codebase, there no strong1 reason migrate.
1 - code won't break if don't, , won't deprecation warnings. sure, joda codebase stop getting updates, unlikely need them. no updates means stability , good thing.
Comments
Post a Comment