[英]Getting 2 seperate time values using regex in Java
So far I have successfully got the time values from a text file. 到目前为止,我已经成功地从文本文件中获取了时间值。 '09:00 19:00'.
“ 09:00 19:00”。 This is what the time values are set out like in the text file.
这就是在文本文件中设置时间值的方式。 Is there any way to get it so these 2 values are seperate.
有没有什么办法可以得到,所以这两个值是分开的。 Like "Start time - 09:00" and "End time - 19:00".
例如“开始时间-09:00”和“结束时间-19:00”。 I have been able to get the values '09:00 19:00' from the text file.
我已经能够从文本文件中获取值“ 09:00 19:00”。 It is just the ability to seperate them that I cannot find
只是我无法找到的将他们分开的能力
String available = null;
Boolean isAvail;
Date day = new Date();
SimpleDateFormat simpleDateformat = new SimpleDateFormat("EEEE");
String token1 = "";
Scanner inFile1 = new Scanner(new File("D:\\Jordanstown\\NetBeans Projects\\COM373\\Availabilty Update File.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))
{
Pattern pattern = Pattern.compile("'(.*?)'");
Matcher matcher = pattern.matcher(curVal);
if (matcher.find())
{
//System.out.println(matcher.group(1));
available = matcher.group(1);
}
}
}
//I would want to have these 2 strings being the 2 times from the text file
String timeStart = "14:00";
String timeEnd = "20:00";
LocalTime start = LocalTime.parse(timeStart);
LocalTime stop = LocalTime.parse(timeEnd);
LocalTime now = LocalTime.now();
if (now.isAfter(start) && now.isBefore(stop))
{
isAvail = true;
System.out.println(isAvail);
}else{
isAvail=false;
System.out.println(isAvail);
}
Thank you for any help in advance 谢谢您的任何帮助
Simply split
the string on the separating space. 只需在分隔空间上
split
字符串即可。
String input = "09:00 19:00";
String[] values = input.split(" ");
String startTime = values[0];
String endTime = values[1];
System.out.println(startTime); // prints: 09:00
System.out.println(endTime); // prints: 19:00
You can also use indexOf
and substring
: 您还可以使用
indexOf
和substring
:
String input = "09:00 19:00";
int idx = input.indexOf(' ');
String startTime = input.substring(0, idx);
String endTime = input.substring(idx + 1);
System.out.println(startTime); // prints: 09:00
System.out.println(endTime); // prints: 19:00
These are all common methods on the String
class, and you should have been able to find them yourself, if you just read the documentation , ie the javadoc of String
. 这些都是
String
类上的所有常用方法,如果您只是阅读文档 (即String
的javadoc),则应该可以自己找到它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.