简体   繁体   English

如何从Java中的毫秒时间戳中提取一天中的时间段?

[英]How to extract the period of the day from milliseconds timestamp in Java?

Could somebody please shed some light on how can I get a period of the day (AM/PM) from the milliseconds timestamp value?有人可以解释一下如何从毫秒时间戳值中获取一天中的某个时间段(AM/PM)? This is currently being done by the following code:这是目前通过以下代码完成的:

Calendar cal = Calendar.getInstance();
cal.setTime(new Date(prevDate));
assertEquals(cal.get(Calendar.AM_PM), Calendar.PM));

I just need to get rid of java.util.Calendar .我只需要摆脱java.util.Calendar

My only idea is to convert long value to LocalDateTime and then to access DateTimeFormatter.ofPattern("a");我唯一的想法是将long值转换为LocalDateTime然后访问DateTimeFormatter.ofPattern("a"); . . Perhaps somebody could suggest another approach?也许有人可以建议另一种方法?

AmPm enum AmPm枚举

I suggest you add the ThreeTen-Extra library to your project.我建议您将ThreeTen-Extra库添加到您的项目中。 This gives you a AmPm class to represent the AM or PM value that you seek.这为您提供了一个AmPm类来表示您寻求的 AM 或 PM 值。 That AmPm class is an enum, defining two objects, one for AM and one for PM. AmPm类是一个枚举,定义了两个对象,一个用于 AM,一个用于 PM。 The class carries methods, including the from method to determine the AM or PM of a given moment.该类携带方法,包括用于确定给定时刻的 AM 或 PM 的from方法。

Using an object here makes your code more self-documenting , provides type-safety , and ensures valid values.在这里使用对象可以使您的代码更加自文档化,提供类型安全,并确保有效值。

Note that a time zone is crucial here.请注意,时区在这里至关重要。 For any given moment, the time-of-day (and even the date) varies around the globe by zone.对于任何给定时刻,全球各地的时间(甚至日期)都会因地区而异。 So while a moment may be morning in Tokyo Japan, it may still be evening in Toledo Ohio US.因此,虽然日本东京可能是早上,但美国俄亥俄州托莱多可能仍然是晚上。

ZoneId z = ZoneId.of( "Africa/Tunis" );
ZonedDateTime zdt = ZonedDateTime.now( z );
AmPm amPm = AmPm.from( zdt );

You could then proceed to branch on which enum object was obtained.然后,您可以继续分支获得枚举对象。

switch ( amPm )
{
    case AM:
        System.out.println( "Good morning." );
        break ;
    case PM:
        System.out.println( "Good day." );
        break ;

    default:
        System.out.println( "ERROR Should never reach this point." );
}

You mentioned the formatting code, so it's not clear whether you are looking for a (localized?) string representation of AM/PM, but given a Clock in the appropriate zone, this will indicate the status:您提到了格式化代码,因此不清楚您是否正在寻找 AM/PM 的(本地化的?)字符串表示形式,但是在适当的区域中给定一个Clock ,这将指示状态:

boolean am = LocalTime.now(clock).isBefore(LocalTime.NOON);

If you have a specific Unix time as a number of milliseconds since the epoch, you'll need to specify the local time zone or offset in which you are interested to complete the conversion:如果您有一个特定的 Unix 时间作为自纪元以来的毫秒数,您需要指定您有兴趣完成转换的本地时区或偏移量:

LocalTime when = Instant.ofEpochMilli(prevDate).atZone(zoneId).toLocalTime();
int meridiem = when.isBefore(LocalTime.NOON) ? Calendar.AM : Calendar.PM;
Assert.assertEquals(meridiem, Calendar.PM);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM