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 java long). 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() gives date object initialized current date / time. problem date api methods flawed ... , deprecated.

    • calendar.getinstance() gives calendar object initialized current date / time, using default locale , timezone. other overloads allow use specific locale and/or timezone. 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

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -