Java 如何检查给定的时间是否在时间限制之间
下面是交易列表,我需要每15分钟,00:01到00:15,00:16到00:30,00:31到00:45…23:45(直到一天结束)完成一次交易计数,如果在该特定时间没有完成交易,则需要填充0.00 样本数据:Java 如何检查给定的时间是否在时间限制之间,java,date,timestamp,Java,Date,Timestamp,下面是交易列表,我需要每15分钟,00:01到00:15,00:16到00:30,00:31到00:45…23:45(直到一天结束)完成一次交易计数,如果在该特定时间没有完成交易,则需要填充0.00 样本数据: 124385 20191029001650 124385 20191029002050 124385 20191029102050 124391 20191029135007 124391 20191029135507 124392 20191029144229 预期输出应为0.00
124385 20191029001650
124385 20191029002050
124385 20191029102050
124391 20191029135007
124391 20191029135507
124392 20191029144229
预期输出应为0.00,2,0.00,0.00,0.00….
我们需要发送数据,例如00:15到00:30“2”事务已完成,以及10:16到10:30“1”事务。定义一个
事件
类,该类有两个成员字段:一个长
整数对象作为事务编号,一个LocalDateTime作为日期时间值(或
Instant`如果您知道预期时区或UTC的偏移量)
对于日期时间,定义格式模式
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmmss" ) ;
解析每个字符串
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
按每个LocalDateTime
对对象进行排序。实现Comparable
或创建Comparator
对象。这两个对象在堆栈溢出中都被多次涉及,因此搜索以了解更多信息
// --------| Comparator |-----------------------
/**
* Comparator by `when`
*/
public static Comparator <Event> WhenComparator = new Comparator<Event>() {
@Override
public int compare(Event e1, Event e2) {
return e1.getWhen().compareTo( e2.getWhen() );
}
};
这会给你一个开始和停止的时间
- 如果要将这些
对象调整为偏移量或时区,请为开始和停止提取一个LocalDateTime
。然后在将库添加到项目中后将其作为对象包含。Instant
类提供了方便的比较方法,例如Interval
contains
- 如果使用<代码> LoalDATeTime</C>,请考虑制作自己的<代码> LocalDateTimeRange <代码>类,以保存一对<代码> LoalDATeTime< /Cord>对象,并实现<代码>包含(LoalDATECTIME)< /Cord>方法> < /LI>
LocalDateTimeRange
可能看起来像这个未经测试的原始代码:
package work.basil.example;
import java.time.LocalDateTime;
import java.util.Objects;
public class LocalDateTimeRange
{
final private LocalDateTime start, end;
public LocalDateTimeRange ( LocalDateTime start , LocalDateTime end )
{
this.start = Objects.requireNonNull( start );
this.end = Objects.requireNonNull( end );
}
public LocalDateTime getStart ( )
{
return this.start;
}
public LocalDateTime getEnd ( )
{
return this.end;
}
public boolean contains ( LocalDateTime ldt )
{
// Contains is true if the target is greater-than-or-equal-to (not before) the start, and is less-than (before) the ending.
// This approach is known as Half-Open, where the beginning is inclusive while the ending is exclusive.
return ( ! ldt.isBefore( this.getStart() ) ) && ldt.isBefore( this.getEnd() );
}
@Override
public boolean equals ( Object o )
{
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;
LocalDateTimeRange that = ( LocalDateTimeRange ) o;
return getStart().equals( that.getStart() ) &&
getEnd().equals( that.getEnd() );
}
@Override
public int hashCode ( )
{
return Objects.hash( getStart() , getEnd() );
}
@Override
public String toString ( )
{
return start + "/" + end; // Return string in standard ISO 8601 format.
}
static public LocalDateTimeRange parse ( )
{
// Parse a string in standard ISO 8601 format.
…
}
}
比较手头的对象,看看它是否不小于开始和停止。这种半开放的方法定义时间跨度通常是最好的,其中开始是包含的,而结束是独占的
继续循环,直到找到包含手头对象的日期时间的日期时间范围。找到后,添加到映射中,或者:
- 递增计数的
映射
- 一个
映射>
将事件
对象添加到列表
或集合
如果在该特定时间没有完成任何事务,则需要填充0.0
将这样的值添加到映射中,或者将该LocalDateRange
键的映射值保留为null
循环到下一个对象
完成后,分析地图
提示:教育发布该数据的人员:
- 序列化为文本的日期时间值的标准格式
- 始终指示时区或UTC偏移的重要性
上面讨论的所有这些主题在堆栈溢出方面都已被多次提及。因此,请搜索以了解更多信息。在发布之前,请彻底搜索堆栈溢出。到目前为止您做了哪些尝试?我建议您查看Java时间API,特别是Instant类。它提供了检查一个时间戳是在之前还是之后的便捷方法或者在其他时间戳之间。您的示例列表正确吗?您有txn id值的倍数,以及txn和时间戳的重复对。
// --------| Comparator |-----------------------
/**
* Comparator by `when`
*/
public static Comparator <Event> WhenComparator = new Comparator<Event>() {
@Override
public int compare(Event e1, Event e2) {
return e1.getWhen().compareTo( e2.getWhen() );
}
};
Duration d = Duration.ofMinutes( 15 ) ;
package work.basil.example;
import java.time.LocalDateTime;
import java.util.Objects;
public class LocalDateTimeRange
{
final private LocalDateTime start, end;
public LocalDateTimeRange ( LocalDateTime start , LocalDateTime end )
{
this.start = Objects.requireNonNull( start );
this.end = Objects.requireNonNull( end );
}
public LocalDateTime getStart ( )
{
return this.start;
}
public LocalDateTime getEnd ( )
{
return this.end;
}
public boolean contains ( LocalDateTime ldt )
{
// Contains is true if the target is greater-than-or-equal-to (not before) the start, and is less-than (before) the ending.
// This approach is known as Half-Open, where the beginning is inclusive while the ending is exclusive.
return ( ! ldt.isBefore( this.getStart() ) ) && ldt.isBefore( this.getEnd() );
}
@Override
public boolean equals ( Object o )
{
if ( this == o ) return true;
if ( o == null || getClass() != o.getClass() ) return false;
LocalDateTimeRange that = ( LocalDateTimeRange ) o;
return getStart().equals( that.getStart() ) &&
getEnd().equals( that.getEnd() );
}
@Override
public int hashCode ( )
{
return Objects.hash( getStart() , getEnd() );
}
@Override
public String toString ( )
{
return start + "/" + end; // Return string in standard ISO 8601 format.
}
static public LocalDateTimeRange parse ( )
{
// Parse a string in standard ISO 8601 format.
…
}
}