从午夜开始的时间

发布于 2024-12-11 12:37:13 字数 300 浏览 0 评论 0 原文

我正在使用一个 Java 库,该库有一个需要时间信息的方法。 它是这样描述的:

public void function(byte[] time);

时间:时间以自午夜以来的 1/100 秒为单位,以三字节整数形式提供,从最高有效字节开始。

我不知道该怎么做。我可以获得以毫秒为单位的时间(Sysem.currentTimeInMillisecond)。但这给了我自 1970 年 1 月 1 日以来的毫秒数。

有人可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风无影 2024-12-18 12:37:13

这是我刚刚编写的代码。我认为这是可以自我解释的。将int转换为字节数组的逻辑是从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);

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);
再可℃爱ぅ一点好了 2024-12-18 12:37:13

tl;dr

ZonedDateTime.now( 
    ZoneId.of( "Africa/Tunis" ) 
)
.get( ChronoField.MILLI_OF_DAY )  
/ 10L 

java.time

现代方法使用 java.time 类来取代麻烦的旧遗留类,例如 Date 和 Date 类。 日历

时区对于确定当前日期和时间至关重要。

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;

获得当天的第一刻。

ZonedDateTime startOfToday = now.toLocalDate().atStartOfDay( z ) ;

将经过的时间计算为 Duration 对象。

Duration d = Duration.between( startOfToday , now ) ;

获取该总时间跨度中的毫秒数。

long millis = d.toMillis() ;

但您需要 1/100 秒的粒度,而不是 1/1,000 秒(即毫秒)。所以除以十。

long countOfOneHundrethsOfASecondSinceStartOfDay = ( millis / 10L ) ;

快捷方式是访问 ZonedDateTime 对象的 MILLI_OF_DAY 字段,从而无需获取一天的开始时间。

ZonedDateTime now = ZonedDateTime.now( ZoneId.of( "Pacific/Auckland" ) ) ;
long millis = now.get( ChronoField.MILLI_OF_DAY ) ; 
long countOfOneHundrethsOfASecondSinceStartOfDay = ( millis / 10L ) ;

关于 java.time

java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如java.util.Date, 日历, & ; SimpleDateFormat

Joda-Time 项目,现已在 维护模式,建议迁移到java.time 类。

要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310

您可以直接与数据库交换java.time对象。使用符合 JDBC 驱动程序 /jeps/170" rel="nofollow noreferrer">JDBC 4.2 或更高版本。不需要字符串,不需要 java.sql.* 类。

从哪里获取 java.time 类?

ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如 间隔YearWeekYearQuarter更多

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.

油焖大侠 2024-12-18 12:37:13

现在创建一个公历。克隆它并将小时、分钟、秒和毫秒设置为零。从两者中获取毫秒并找出差异,然后转换为百分之一。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文