简体   繁体   中英

Getting a time from a text file

So far I have put the text file into a list array and have been able to search for the current day. But I cannot find a way to get a time range out of the text file.

Text File

Jack Johnson
Thursday 07:00 17:45,

Is there any way to get these time values out of the text file and compare the current time to check if it falls between these 2 time values. I have already got the current time but I just cannot figure out how to get these 2 time values out of the text file

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));

If you can snatch the string then you can use 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
}

You can use the old functions, that can parse maintaining a running position:

    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);

The result being a bit of unused fields.

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

With Date.toInstant() or whatever one can convert them to new time types, like LocalTime.


New time 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);

Giving

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

Using Java 8, you can use the LocalTime class for the times and extract the time values from the file using regex . For example using the string you provided as input you can do this:

    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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM