[英]Getting a time from a text file
到目前為止,我已經將文本文件放入列表數組中,並且能夠搜索當天。 但是我找不到從文本文件中獲取時間范圍的方法。
文本文件
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));
如果可以抓取字符串,則可以使用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
}
您可以使用舊的函數,這些函數可以解析維護的跑步姿勢:
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
使用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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.