尝试比较java中两个文件之间的秒数

oyt4ldly  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(268)

我有两个文件,我正在尝试比较之间的秒。我希望输出显示两个文件的日期,连接在小于或等于9秒之间。感谢stackoverflow社区花时间帮助一位noob程序员,以下是我迄今为止的代码:

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Testing {
    public static void main(String[] args) throws IOException, ParseException {

        Scanner drillingdata = new Scanner(new FileReader(
                "C:\\Users\\dvu\\Desktop\\GammaToolData\\DrillingData.txt"));
        Scanner mwddata = new Scanner(new FileReader(
                "C:\\Users\\dvu\\Desktop\\GammaToolData\\Mwddatalog.txt"));

        SimpleDateFormat formatter = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss");
        while (mwddata.hasNextLine() && drillingdata.hasNextLine())

        {
            // System.out.println(Mwddata.nextLine());
            String line = drillingdata.nextLine();
            String line1 = mwddata.nextLine();
            String[] value1 = line1.split("\t");
            String[] value = line.split("\t");

            for (int i=0; i < value1.length; i++) {
                Date valueFromList1 = formatter.parse(value1[i]);
                for (int z = 0; z < value.length; z++) {
                    Date valueFromList2 = formatter.parse(value[z]);
                    if (areSecondsEqual(valueFromList1, valueFromList2)) {
                        int index = z + 1;
                        System.out.println(valueFromList1 + "\t=\t" + valueFromList2 + "\t=\t" + index + ".");
                        break;
                    }
                }

            }

        }
        }

    public static String[] readFile(String filename) {
        String line;
        List<String> lines = new ArrayList<String>();
        try {

            FileReader fileReader = new FileReader(filename);

            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while ((line = bufferedReader.readLine()) != null) {
                lines.add(line);
            }

            bufferedReader.close();
        } catch (FileNotFoundException ex) {
            System.out.println("Unable to open file '" + filename + "'");
        } catch (IOException ex) {
            System.out.println("Error reading file '" + filename + "'");

        }
        return lines.toArray(new String[lines.size()]);
    }
    public static boolean areSecondsEqual(Date date1,Date date2) {
           // implement steps to compare date.\
        return date1.equals(date2);

    }
}
a7qyws3x

a7qyws3x1#

您的日期格式不正确。

new SimpleDateFormat("mm/dd/yyyy hh:mm:ss")

应该是这样的

new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")

小写 m 表示分钟和小写 h 是指上午/下午的时间。

相关问题