Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查给定的时间是否在时间限制之间_Java_Date_Timestamp - Fatal编程技术网

Java 如何检查给定的时间是否在时间限制之间

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

下面是交易列表,我需要每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,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. 
        …
    }
}