如何在日期和时间中分割完整日期格式? [英] How to split the full date format in date and time?
问题描述
我有很多必须解析的示例所示格式的字符串。我正在尝试确定今天是哪些弦乐。
I have a lot of Strings in the format shown my example that I have to parse. I'm trying to determine which of the Strings are today.
我的问题是,时间快到了,我只需要比较一下日期即可。
My problem is, that the time is almost there and I just need to compare that date.
下一步我想检查时间是否在带有.after和.before的两个时间戳 HH:mm:ss之间,但是有一个问题,日期快到了。
Next I want to check if time is between two timestamps "HH:mm:ss" with .after and .before, but there is the problem, that the date is almost there.
如何在日期和时间上拆分解析后的格式,以自己的方式处理每种格式?
How do I split that parsed format in date and time to handle each in its own way?
如果相关,我正在Android Studio中工作。
I'm working in Android Studio, if that's relevant.
String dtStart = "2016-05-23 07:24:59";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
if (new Date().equals(format.parse(dtStart)) ) System.out.println("true");
else System.out.println("false");
list.add(new LatLng(lat, lng));
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
推荐答案
java.time
使用 java.time 类。
许多功能已反向移植到Java 6& ThreeTen-Backport 中的第7部分,并进一步在 ThreeTen-ABP 。
Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport, and further adapted to Android in ThreeTen-ABP.
String dateToParse = "2016-05-23 07:24:59";
LocalDateTime dateTime = LocalDateTime.parse(dateToParse, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDate localDate = dateTime.toLocalDate();
LocalTime localTime = dateTime.toLocalTime();
// Compare here to your date & time
这篇关于如何在日期和时间中分割完整日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!