So, I was searching for this for some time but I didn't find an answer. Thats why I'm asking here. My problem goes like this: With Swing I made an app that will get text from JTextArea and then save it inside a .txt document. Also it will save 2 more files (as .txt documents too). In one, there would be date (i.e 2016.03.05) and in other would be time (i.e 09:50 AM). What I need is compare the date and time to system date and time and check if they match. I only need a way on how to exactly do this, since they are stored as string, what would be good way to compare them to system date and time. I think that is should be like this:
所以,我一直在寻找这个,但我找不到答案。这就是为什么我在这里问。我的问题是这样的:使用Swing我创建了一个应用程序,它将从JTextArea获取文本,然后将其保存在.txt文档中。此外,它还将节省2个文件(也作为.txt文件)。其中一个是日期(即2016.03.05),另一个是时间(即09:50 AM)。我需要的是将日期和时间与系统日期和时间进行比较,并检查它们是否匹配。我只需要一种方法来完成这项工作,因为它们存储为字符串,将它们与系统日期和时间进行比较的好方法。我想应该是这样的:
if(date in date file is equal to system date) {/do stuff}
I wouldn't rly be looking for spoonfeeding, but I need to have a good and efficient way of doing this.
我不会在寻找喂食,但我需要有一个好的和有效的方法来做这件事。
2 个解决方案
#1
1
Let's pretend the op has read the date/time strings from a file.
让我们假装op已经从文件中读取了日期/时间字符串。
String date = "2016.03.05";
String time = "09:50 AM";
We want to compare that to the "system time" so how about.
我们想将它与“系统时间”进行比较,那么怎么样。
LocalDateTime now = LocalDateTime.now();
Now we have a date and time and we want to compare it to the values in the file. One way, is to create a LocalDate object from the date string.
现在我们有一个日期和时间,我们想将它与文件中的值进行比较。一种方法是从日期字符串创建LocalDate对象。
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
LocalDate localDate = LocalDate.parse(date, dateFormatter);
if(now.toLocaleDate().equals(localDate)){
//date is equal so now what?
}
We can do the same for the local time.
我们可以在当地时间做同样的事情。
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:m a");
LocalTime localTime = LocalTime.parse(time, timeFormatter);
if(now.toLocalTime().equals(localTime)){
//do stuff if the time is equal (which it will rarely be)
}
#2
0
If you are using java 8 then there is a very good article on how to parse a String to Date. Once you parse a String into class LocalDateTime then you can then run:
如果您使用的是Java 8,那么有一篇关于如何将String解析为Date的非常好的文章。一旦将String解析为类LocalDateTime,然后您就可以运行:
if(LocalDateTime.now().equals(yourTime)) {
//your code
}
to see if they match. Please read about java.time package here https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
看他们是否匹配。请阅读这里的java.time包https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
and here is an article about String parsing to date: https://www.linkedin.com/pulse/java-8-javatime-package-parsing-any-string-date-michael-gantman?trk=pulse_spock-articles
这是一篇关于字符串解析的文章:https://www.linkedin.com/pulse/java-8-javatime-package-parsing-any-string-date-michael-gantman?strk = pulse_spock-articles
#1
1
Let's pretend the op has read the date/time strings from a file.
让我们假装op已经从文件中读取了日期/时间字符串。
String date = "2016.03.05";
String time = "09:50 AM";
We want to compare that to the "system time" so how about.
我们想将它与“系统时间”进行比较,那么怎么样。
LocalDateTime now = LocalDateTime.now();
Now we have a date and time and we want to compare it to the values in the file. One way, is to create a LocalDate object from the date string.
现在我们有一个日期和时间,我们想将它与文件中的值进行比较。一种方法是从日期字符串创建LocalDate对象。
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
LocalDate localDate = LocalDate.parse(date, dateFormatter);
if(now.toLocaleDate().equals(localDate)){
//date is equal so now what?
}
We can do the same for the local time.
我们可以在当地时间做同样的事情。
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:m a");
LocalTime localTime = LocalTime.parse(time, timeFormatter);
if(now.toLocalTime().equals(localTime)){
//do stuff if the time is equal (which it will rarely be)
}
#2
0
If you are using java 8 then there is a very good article on how to parse a String to Date. Once you parse a String into class LocalDateTime then you can then run:
如果您使用的是Java 8,那么有一篇关于如何将String解析为Date的非常好的文章。一旦将String解析为类LocalDateTime,然后您就可以运行:
if(LocalDateTime.now().equals(yourTime)) {
//your code
}
to see if they match. Please read about java.time package here https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
看他们是否匹配。请阅读这里的java.time包https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
and here is an article about String parsing to date: https://www.linkedin.com/pulse/java-8-javatime-package-parsing-any-string-date-michael-gantman?trk=pulse_spock-articles
这是一篇关于字符串解析的文章:https://www.linkedin.com/pulse/java-8-javatime-package-parsing-any-string-date-michael-gantman?strk = pulse_spock-articles