gpt4 book ai didi

java - 我如何在 Java 中管理工作时间?

转载 作者:行者123 更新时间:2023-11-30 11:42:05 25 4
gpt4 key购买 nike

我想问问是否有人知道任何 API 或类似的东西可以让我管理一天中的具体部分(例如工作时间)

我的问题是我必须在下一个上下文中管理时间:

假设我在一家工作时间为“上午 8 点至下午 2 点”和“下午 3 点至下午 6 点”并且夏令时为“上午 8 点至下午 2 点”的公司工作。我想知 Prop 体日期的具体时刻是劳动时刻还是不是。

例如,如果我有提到的日历,并且我询问 API“2012 年 8 月 13 日晚上 9 点”是否是工作时间,它必须检查它并返回正确答案(在本例中为 false),如果我询问如果“2012 年 8 月 13 日上午 9 点”是工作时间,则它必须返回“true”

其他重要的事情相关。我必须使用提到的日历计算两个日期之间的间隔。例如,如果我将开始时间设置为“今天下午 5 点”和结束时间“明天上午 10 点”,它必须返回 3 小时(或以秒或毫秒为单位的等效时间),因为它是开始日期和之间经过的正确时间段此日历中的结束日期。

它还必须与假期一起工作(特别是每个国家/地区)。我找到了一个 API 调用“JollyTime”,但是,虽然它适用于假期,但它不支持工作时间......

有什么想法吗?

最佳答案

更新:Joda-Time 库现在处于维护模式,其主要作者 Stephen Colebourne 继续领导定义 java.time 类的 JSR 310内置于 Java 8 及更高版本中。


一个对日期时间有完善支持的好数据库在这里可能会有帮助。一个这样的数据库是 Postgres , 具有良好的日期时间 data typescommands ("functions") .

Joda-Time框架也可能有帮助。 Interval类及其父类定义一对开始和停止日期时间之间的时间跨度。他们提供比较方法,例如:包含、重叠、isBefore、is After。

这里有一些示例代码可以帮助您开始使用 Joda-Time 2.3 和 Java 7。

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;

DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );

List<Interval> workIntervalsFor13Aug2012 = new ArrayList<Interval>( 2 );
DateTime start, stop;
Interval interval;

start = new DateTime( 2012, 8, 13, 8, 0, 0, timeZone );
stop = new DateTime( 2012, 8, 13, 14, 0, 0, timeZone );
interval = new org.joda.time.Interval( start, stop );
workIntervalsFor13Aug2012.add( interval );

start = new DateTime( 2012, 8, 13, 15, 0, 0, timeZone );
stop = new DateTime( 2012, 8, 13, 18, 0, 0, timeZone );
interval = new org.joda.time.Interval( start, stop );
workIntervalsFor13Aug2012.add( interval );

// Check a date-time against those work intervals.
DateTime test09 = new DateTime( 2012, 8, 13, 9, 0, 0, timeZone );
DateTime test21 = new DateTime( 2012, 8, 13, 21, 0, 0, timeZone );

// You should write a "dateTimeIsInWorkingInterval" method that performs this loop.
Boolean hit = false;
for ( Interval nthInterval : workIntervalsFor13Aug2012 ) {
if( nthInterval.contains( test09 )) {
hit = true;
break;
}
}
if( hit ) {
System.out.println( "This date-time: " + test09 + " occurs during a work interval.");
} else {
System.out.println( "This date-time: " + test09 + " occurs outside a work interval.");
}

hit = false;
for ( Interval nthInterval : workIntervalsFor13Aug2012 ) {
if( nthInterval.contains( test21 )) {
hit = true;
break;
}
}
if( hit ) {
System.out.println( "This date-time: " + test21 + " occurs during a work interval.");
} else {
System.out.println( "This date-time: " + test21 + " occurs outside a work interval.");
}

运行时...

This date-time: 2012-08-13T09:00:00.000+02:00 occurs during a work interval.
This date-time: 2012-08-13T21:00:00.000+02:00 occurs outside a work interval.

关于java - 我如何在 Java 中管理工作时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11934029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com