4

I am using a Java library that has a method that wants a time info. it is described like this:

public void function(byte[] time);

time: The time is provided in 1/100th seconds since midnight as a three bytes integer, starting with the most significant byte.

I don't know how to do that. I can get time in milliseconds(Sysem.currentTimeInMillisecond). but that gives me milliseconds past since 1 January 1970.

Can anybody help?

apaderno
  • 28,547
  • 16
  • 75
  • 90
emin
  • 742
  • 9
  • 21

3 Answers3

2

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

Here is the code I have just written. I think it is self explainable enough. The logic of converting int to byte array is stolen from DataOutputStream.writeInt()

    Calendar c = Calendar.getInstance(); //now

    Calendar m = Calendar.getInstance(); //midnight
    m.set(Calendar.HOUR_OF_DAY, 0);
    m.set(Calendar.MINUTE, 0);
    m.set(Calendar.SECOND, 0);
    m.set(Calendar.MILLISECOND, 0);

    int diff = (int) (c.getTimeInMillis() - m.getTimeInMillis()) ;
    int v = diff / 10;

    byte[] bytes = new byte[4];
    bytes[0] = (byte) ((v >>> 24) & 0xFF);
    bytes[1] = (byte) ((v >>> 16) & 0xFF);
    bytes[2] = (byte) ((v >>>  8) & 0xFF);
    bytes[4] = (byte) ((v >>>  0) & 0xFF);
AlexR
  • 114,158
  • 16
  • 130
  • 208
0

Create a Gregorian Calendar for now. Clone it and set the hours, minutes, seconds, and milliseconds to zero. Get milliseconds from both and find the difference, then convert to hundredths.

Thom
  • 14,013
  • 25
  • 105
  • 185