Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.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_Datetime_Sum_Timestamp_Duration - Fatal编程技术网

Java中文本文档的时间戳总和

Java中文本文档的时间戳总和,java,datetime,sum,timestamp,duration,Java,Datetime,Sum,Timestamp,Duration,我对追踪视频游戏市场内的交易日志感兴趣。我已经在使用一个记录所有交易日志的程序。它生成一个文本文档,如下所示: [00:01:30]购买了1支深海鞭子(2213157支)[00:00:18] [00:02:10]购买了1支深海鞭子(2213157支)[00:00:04] [00:02:11]售出1支深海鞭子(2214442支)[00:00:18] [00:02:49]售出1支深海鞭子(221575支)[00:00:15] 括号中有两个时间戳,第一个是交易完成的时间,第二个是持续时间。我需要对每行

我对追踪视频游戏市场内的交易日志感兴趣。我已经在使用一个记录所有交易日志的程序。它生成一个文本文档,如下所示:

[00:01:30]购买了1支深海鞭子(2213157支)[00:00:18]

[00:02:10]购买了1支深海鞭子(2213157支)[00:00:04]

[00:02:11]售出1支深海鞭子(2214442支)[00:00:18]

[00:02:49]售出1支深海鞭子(221575支)[00:00:15]

括号中有两个时间戳,第一个是交易完成的时间,第二个是持续时间。我需要对每行末尾的所有时间戳求和

我的一些研究告诉我,我应该抓取每个时间戳并将其保存为单独的字符串,然后将其转换为日期、持续时间或其他内容。我可能需要为每一行循环它,但我没有一个明确的过程

我不是要求任何人为我编写代码,那样会破坏学习目的。但如果有人能给我指出正确的方向,这将使我走上完成这件事的快车道


谢谢

尝试使用regexp分析每一行,该行以[date]开头,后跟一些文本,后跟[duration],并捕获结尾部分。请参阅类模式和匹配器。然后用DateFormat解析每个找到的持续时间,并将它们全部相加。没有您要求的详细信息。

尝试使用regexp分析每一行,该行以[date]开头,后面是一些文本,后跟[duration],并捕获结尾部分。请参阅类模式和匹配器。然后用DateFormat解析每个找到的持续时间,并将它们全部相加。没有你要求的细节。

我会:

  • 使用正则表达式提取您感兴趣的字符串部分
  • 从字符串创建一个持续时间(您可以设计自己的类或使用)——如果使用jdk类,它有一个方法,但您需要首先重新格式化字符串。或者,您可以使用
    LocalTime::parse
    从字符串创建
    LocalTime
    ,并使用
    Duration.between(LocalTime.MIDNIGHT,timestamp)
  • 将所有持续时间相加
    • 我会:

      • 使用正则表达式提取您感兴趣的字符串部分
      • 从字符串创建一个持续时间(您可以设计自己的类或使用)——如果使用jdk类,它有一个方法,但您需要首先重新格式化字符串。或者,您可以使用
        LocalTime::parse
        从字符串创建
        LocalTime
        ,并使用
        Duration.between(LocalTime.MIDNIGHT,timestamp)
      • 将所有持续时间相加
      我需要对每行末尾的所有时间戳求和

      您可以通过以下步骤完成此操作:

    • 将文件的所有行读入
    • 使用Java正则表达式API从流中检索位于每行末尾的持续时间字符串
    • 将每个持续时间字符串解析为
    • 将生成的
      收集到
      列表
    • 将持续时间初始化为0小时,并将列表中的所有持续时间添加到其中
    • 演示:

      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Paths;
      import java.time.Duration;
      import java.util.List;
      import java.util.regex.MatchResult;
      import java.util.regex.Pattern;
      import java.util.stream.Collectors;
      
      public class Main {
          public static void main(String[] args) throws IOException {
      
              List<Duration> list = 
                  Files.lines(Paths.get("log.txt")) // Read all lines from the file as a Stream 
                      .map(line -> Pattern.compile("(?<=\\[)\\d{2}:\\d{2}:\\d{2}(?=\\]$)")
                              .matcher(line) // Match the regex in each line
                              .results()
                              .map(MatchResult::group)
                              .collect(Collectors.joining("")) // Join the only string in the stream to a single string 
                      ).map(s -> Duration.parse(toIsoDurationString(s))) // Parse the text to Duration
                      .collect(Collectors.toList());
      
              // Initialize a duration with 0 hours and add all Durations from the list to it
              Duration sum = Duration.ofHours(0);
              for (Duration duration : list) {
                  sum = sum.plus(duration);
              }
      
              System.out.println("Total duration: " + sum);
      
              // Custom format
              // ####################################Java-8####################################
              System.out.println("Total duration: "
                      + String.format("%02d:%02d:%02d", sum.toHours(), sum.toMinutes() % 60, sum.toSeconds() % 60));
              // ##############################################################################
      
              // ####################################Java-9####################################
              System.out.println("Total duration: "
                      + String.format("%02d:%02d:%02d", sum.toHoursPart(), sum.toMinutesPart(), sum.toSecondsPart()));
              // ##############################################################################
          }
      
          static String toIsoDurationString(String duration) {
              char[] symbols = "HMS".toCharArray();
              String[] durationParts = duration.split(":");
              StringBuilder sb = new StringBuilder("PT");
              for (int i = 0; i < durationParts.length; i++) {
                  sb.append(durationParts[i]).append(symbols[i]);
              }
              return sb.toString();
          }
      }
      
      Total duration: PT55S
      Total duration: 00:00:55
      Total duration: 00:00:55
      
      正则表达式的解释:

      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Paths;
      import java.time.Duration;
      import java.util.List;
      import java.util.regex.MatchResult;
      import java.util.regex.Pattern;
      import java.util.stream.Collectors;
      
      public class Main {
          public static void main(String[] args) throws IOException {
      
              List<Duration> list = 
                  Files.lines(Paths.get("log.txt")) // Read all lines from the file as a Stream 
                      .map(line -> Pattern.compile("(?<=\\[)\\d{2}:\\d{2}:\\d{2}(?=\\]$)")
                              .matcher(line) // Match the regex in each line
                              .results()
                              .map(MatchResult::group)
                              .collect(Collectors.joining("")) // Join the only string in the stream to a single string 
                      ).map(s -> Duration.parse(toIsoDurationString(s))) // Parse the text to Duration
                      .collect(Collectors.toList());
      
              // Initialize a duration with 0 hours and add all Durations from the list to it
              Duration sum = Duration.ofHours(0);
              for (Duration duration : list) {
                  sum = sum.plus(duration);
              }
      
              System.out.println("Total duration: " + sum);
      
              // Custom format
              // ####################################Java-8####################################
              System.out.println("Total duration: "
                      + String.format("%02d:%02d:%02d", sum.toHours(), sum.toMinutes() % 60, sum.toSeconds() % 60));
              // ##############################################################################
      
              // ####################################Java-9####################################
              System.out.println("Total duration: "
                      + String.format("%02d:%02d:%02d", sum.toHoursPart(), sum.toMinutesPart(), sum.toSecondsPart()));
              // ##############################################################################
          }
      
          static String toIsoDurationString(String duration) {
              char[] symbols = "HMS".toCharArray();
              String[] durationParts = duration.split(":");
              StringBuilder sb = new StringBuilder("PT");
              for (int i = 0; i < durationParts.length; i++) {
                  sb.append(durationParts[i]).append(symbols[i]);
              }
              return sb.toString();
          }
      }
      
      Total duration: PT55S
      Total duration: 00:00:55
      Total duration: 00:00:55
      
    • (?
      我需要对每行末尾的所有时间戳求和

      您可以通过以下步骤完成此操作:

    • 将文件的所有行读入
    • 使用Java正则表达式API从流中检索位于每行末尾的持续时间字符串
    • 将每个持续时间字符串解析为
    • 将生成的
      收集到
      列表
    • 将持续时间初始化为0小时,并将列表中的所有持续时间添加到其中
    • 演示:

      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Paths;
      import java.time.Duration;
      import java.util.List;
      import java.util.regex.MatchResult;
      import java.util.regex.Pattern;
      import java.util.stream.Collectors;
      
      public class Main {
          public static void main(String[] args) throws IOException {
      
              List<Duration> list = 
                  Files.lines(Paths.get("log.txt")) // Read all lines from the file as a Stream 
                      .map(line -> Pattern.compile("(?<=\\[)\\d{2}:\\d{2}:\\d{2}(?=\\]$)")
                              .matcher(line) // Match the regex in each line
                              .results()
                              .map(MatchResult::group)
                              .collect(Collectors.joining("")) // Join the only string in the stream to a single string 
                      ).map(s -> Duration.parse(toIsoDurationString(s))) // Parse the text to Duration
                      .collect(Collectors.toList());
      
              // Initialize a duration with 0 hours and add all Durations from the list to it
              Duration sum = Duration.ofHours(0);
              for (Duration duration : list) {
                  sum = sum.plus(duration);
              }
      
              System.out.println("Total duration: " + sum);
      
              // Custom format
              // ####################################Java-8####################################
              System.out.println("Total duration: "
                      + String.format("%02d:%02d:%02d", sum.toHours(), sum.toMinutes() % 60, sum.toSeconds() % 60));
              // ##############################################################################
      
              // ####################################Java-9####################################
              System.out.println("Total duration: "
                      + String.format("%02d:%02d:%02d", sum.toHoursPart(), sum.toMinutesPart(), sum.toSecondsPart()));
              // ##############################################################################
          }
      
          static String toIsoDurationString(String duration) {
              char[] symbols = "HMS".toCharArray();
              String[] durationParts = duration.split(":");
              StringBuilder sb = new StringBuilder("PT");
              for (int i = 0; i < durationParts.length; i++) {
                  sb.append(durationParts[i]).append(symbols[i]);
              }
              return sb.toString();
          }
      }
      
      Total duration: PT55S
      Total duration: 00:00:55
      Total duration: 00:00:55
      
      正则表达式的解释:

      import java.io.IOException;
      import java.nio.file.Files;
      import java.nio.file.Paths;
      import java.time.Duration;
      import java.util.List;
      import java.util.regex.MatchResult;
      import java.util.regex.Pattern;
      import java.util.stream.Collectors;
      
      public class Main {
          public static void main(String[] args) throws IOException {
      
              List<Duration> list = 
                  Files.lines(Paths.get("log.txt")) // Read all lines from the file as a Stream 
                      .map(line -> Pattern.compile("(?<=\\[)\\d{2}:\\d{2}:\\d{2}(?=\\]$)")
                              .matcher(line) // Match the regex in each line
                              .results()
                              .map(MatchResult::group)
                              .collect(Collectors.joining("")) // Join the only string in the stream to a single string 
                      ).map(s -> Duration.parse(toIsoDurationString(s))) // Parse the text to Duration
                      .collect(Collectors.toList());
      
              // Initialize a duration with 0 hours and add all Durations from the list to it
              Duration sum = Duration.ofHours(0);
              for (Duration duration : list) {
                  sum = sum.plus(duration);
              }
      
              System.out.println("Total duration: " + sum);
      
              // Custom format
              // ####################################Java-8####################################
              System.out.println("Total duration: "
                      + String.format("%02d:%02d:%02d", sum.toHours(), sum.toMinutes() % 60, sum.toSeconds() % 60));
              // ##############################################################################
      
              // ####################################Java-9####################################
              System.out.println("Total duration: "
                      + String.format("%02d:%02d:%02d", sum.toHoursPart(), sum.toMinutesPart(), sum.toSecondsPart()));
              // ##############################################################################
          }
      
          static String toIsoDurationString(String duration) {
              char[] symbols = "HMS".toCharArray();
              String[] durationParts = duration.split(":");
              StringBuilder sb = new StringBuilder("PT");
              for (int i = 0; i < durationParts.length; i++) {
                  sb.append(durationParts[i]).append(symbols[i]);
              }
              return sb.toString();
          }
      }
      
      Total duration: PT55S
      Total duration: 00:00:55
      Total duration: 00:00:55
      
    • (?