从文本文件中获取时间

时间:2017-11-30 14:06:47

标签: java arraylist

到目前为止,我已将文本文件放入列表数组中,并且能够搜索当天。但我找不到从文本文件中获取时间范围的方法。

文字档案

Jack Johnson
Thursday 07:00 17:45,

有没有办法从文本文件中获取这些时间值,并比较当前时间以检查它是否介于这两个时间值之间。我已经有了当前时间,但我无法弄清楚如何从文本文件中获取这两个时间值

Boolean isAvail = false;
Date day = new Date();

SimpleDateFormat simpleDateformat = new SimpleDateFormat("EEEE");
String token1 = "";

Scanner inFile1 = new Scanner(new File("E:\\Folder\\text.txt")).useDelimiter(",\\s*");

List<String> availability = new ArrayList<String>();

while (inFile1.hasNext()) 
{
    token1 = inFile1.next();
    availability.add(token1);
}
inFile1.close();


String[] availabilityArray = availability.toArray(new String[0]);

String searchArray = simpleDateformat.format(day);
for (String curVal : availabilityArray)
{
    if (curVal.contains(searchArray))
    {
    System.out.println(curVal);
    }
}

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm");
LocalDateTime now = LocalDateTime.now();
System.out.println(dtf.format(now));

3 个答案:

答案 0 :(得分:0)

如果你可以抓取字符串,那么你可以使用LocalTime.parse()。

// Say you got that from the file
String timeStart = "07:00";
String timeEnd = "17:45";

// Then
LocalTime start = LocalTime.parse(timeStart);
LocalTime stop = LocalTime.parse(timeEnd);
LocalTime now = LocalTime.now();
if (now.isAfter(start) && now.isBefore(stop)) {
    // whatnot
}

答案 1 :(得分:0)

您可以使用旧函数来解析维护运行位置:

    String input = "Thursday 07:00 17:45,";

    SimpleDateFormat weekdayFormat = new SimpleDateFormat("EEEE", Locale.US);
    ParsePosition pos = new ParsePosition(0);
    Date wd = weekdayFormat.parse(input, pos);
    System.out.println("wd " + wd);

    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.US);
    Date from = timeFormat.parse(input, pos);
    Date to = timeFormat.parse(input, pos);
    System.out.println("" + from + " - " + to);

结果是一些未使用的字段。

wd Thu Jan 01 00:00:00 CET 1970
Thu Jan 01 07:00:00 CET 1970 - Thu Jan 01 17:45:00 CET 1970

使用Date.toInstant()或任何人都可以将它们转换为新的时间类型,例如LocalTime。

新时间API:

    String input = "Thursday 07:00 17:45,";

    DateTimeFormatter weekdayFormat = DateTimeFormatter.ofPattern("EEEE", Locale.US);
    ParsePosition pos = new ParsePosition(0);
    TemporalAccessor wd = weekdayFormat.parse(input, pos);
    System.out.println("wd " + wd);

    DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern(" HH:mm", Locale.US);
    TemporalAccessor from = timeFormat.parse(input, pos);
    TemporalAccessor to = timeFormat.parse(input, pos);
    System.out.println("" + from + " - " + to);

给予

wd {DayOfWeek=4},ISO
{},ISO resolved to 07:00 - {},ISO resolved to 17:45

答案 2 :(得分:0)

使用Java 8,您可以使用LocalTime类来表示时间,并使用regex从文件中提取时间值。例如,使用您提供的字符串作为输入,您可以这样做:

    String stringWithTime = "Jack Johnson Thursday 07:00 17:45,";
    Pattern p = Pattern.compile("(\\d{2}:\\d{2})\\s(\\d{2}:\\d{2})");
    Matcher m = p.matcher(stringWithTime);

    while(m.find()) {
        LocalTime firstTime = LocalTime.parse(m.group(1));
        LocalTime secondTime = LocalTime.parse(m.group(2));

        LocalTime nowTime = LocalTime.now();

        boolean isBetween = firstTime.isBefore(nowTime) && secondTime.isAfter(nowTime);
    }