TL; dr
Instant.now()
Use only java.time classes. Avoid the troubled old obsolete time classes added before Java 8.
Using java.time
Programmer, before you use the new java.time modern classrooms, which are now replacing the infamous old outdated time classes such as Date
, Calendar
, Timestamp
.
Instant
Instant
UTC ( (9) ). UTC : Instant.now
.
Instant instant = Instant.now();
java.time . , , , java.time, / java.time. . java.util.Date
Instant
.
java.util.Date d = java.util.Date.from( myInstant); // To legacy from modern.
Instant instant = myJavaUtilDate.toInstant(); // To modern from legacy.
JDBC
. java.time.
JDBC 4.2 java.time, PreparedStatement::setObject
ResultSet::getObject
.
myPreparedStatement.setObject( … , instant ) ;
… …
Instant instant = myResultSet.getObject( … , Instant.class ) ;
, java.sql, . , .
myPreparedStatement.setTimestamp( … , java.sql.Timestamp.from( instant ) ) ;
… …
Instant instant = myResultSet.getTimestamp( … ).toInstant() ;
ZonedDateTime
, ZonedDateTime
, UTC. Instant
UTC. , , :
Date.from(ZonedDateTime.now(ZoneOffset.UTC)).toInstant();
... :
Date.from( Instant.now() ) ;
, java.util.Date
UTC. toString
, JVM String. - , , Stack Overflow.
UTC Instant
, ZoneId
ZoneDateTime
.
continent/region
, , America/Montreal
, Africa/Casablanca
Pacific/Auckland
. 3-4 , CDT
EST
IST
, (!).
ZoneId z = ZoneId.of( "America/Chicago" );
ZonedDateTime zdt = instant.atZone( z );
java.time
java.time Java 8 . , java.util.Date
, Calendar
SimpleDateFormat
.
Joda-Time, , java.time.
, Oracle. Qaru . - JSR 310.
java.time . JDBC-, JDBC 4.2 . , java.sql.*
.
java.time?
ThreeTen-Extra java.time . java.time. , Interval
, YearWeek
, YearQuarter
.