tl;dr
ZonedDateTime.now(
ZoneId.of( "Africa/Tunis" )
)
.get( ChronoField.MILLI_OF_DAY )
/ 10L
java.time
The modern approach uses the java.time classes that supplanted the troublesome old legacy classes such as Date
& Calendar
.
Time zone is crucial in determining the current date and time-of-day.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;
Get first moment of that day.
ZonedDateTime startOfToday = now.toLocalDate().atStartOfDay( z ) ;
Calculate the elapsed time as a Duration
object.
Duration d = Duration.between( startOfToday , now ) ;
Get the number of milliseconds in that total span of time.
long millis = d.toMillis() ;
But you want 1/100th of a second granularity, not the 1/1,000th of a second that is milliseconds. So divide by ten.
long countOfOneHundrethsOfASecondSinceStartOfDay = ( millis / 10L ) ;
A shortcut would be to access the MILLI_OF_DAY
field of the ZonedDateTime
object, obviating the need to get the start of day.
ZonedDateTime now = ZonedDateTime.now( ZoneId.of( "Pacific/Auckland" ) ) ;
long millis = now.get( ChronoField.MILLI_OF_DAY ) ;
long countOfOneHundrethsOfASecondSinceStartOfDay = ( millis / 10L ) ;
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.