简体   繁体   English

如何比较日期-自定义格式为“ dd.MM.yyyy,HH:mm:ss”的字符串与另一个字符串?

[英]How to compare a date - String in the custom format “dd.MM.yyyy, HH:mm:ss” with another one?

I have to check 2 date String s in the custom format "dd.MM.yyyy, HH:mm:ss" against each other. 我必须相互检查以自定义格式"dd.MM.yyyy, HH:mm:ss" 2个日期String How can I get the older one? 我怎样才能长大一点呢?

For example : 例如 :

String date1 = "05.02.2013, 13:38:14";
String date2 = "05.02.2013, 09:38:14";

Is there a way to get date2 with comparing? 有没有办法通过比较来获取date2

I'll just give you a brief explanation of how you should proceed. 我只给您简要说明您应该如何进行。 You need to first parse those date strings into Date object. 您需要首先将这些日期字符串解析为Date对象。 Use DateFormat#parse(String) for that. 为此使用DateFormat#parse(String) After you get the Date object out of those strings, you can compare those objects using Date#compareTo(Date) method. 从这些字符串中获取Date对象之后,可以使用Date#compareTo(Date)方法比较那些对象。

You already have the date format, use it with SimpleDateFormat and then compare the created Date objects. 您已经有了日期格式,将其与SimpleDateFormat一起使用,然后比较创建的Date对象。

See demo here. 在此处查看演示。 Sample code: 样例代码:

public static void main(String[] args)  Exception {
    String date1 = "05.02.2013, 13:38:14";
    String date2 = "05.02.2013, 09:38:14";
    System.out.println(getOlder(date1, date2)); // 05.02.2013, 13:38:14
    System.out.println(getOlder(date2, date1)); // 05.02.2013, 13:38:14
}

public static String getOlder(String one, String two) throws ParseException {
    Date dateOne = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss").parse(one);
    Date dateTwo = new SimpleDateFormat("dd.MM.yyyy, HH:mm:ss").parse(two);
    if (dateOne.compareTo(dateTwo) > -1) {
        return one; // or return dateOne;
    }
    return two; // or return dateTwo;
}

Of course, the method above returns the oldest date string entered. 当然,以上方法将返回最早输入的日期字符串。 You can changed it (see the comments) to return the Date object instead. 您可以对其进行更改(请参见注释)以返回Date对象。

Calulations with date and time are always tricky and I strongly recommend to use appropiate classes for this, you might want to check Joda Time for this. 使用日期和时间进行计算总是很棘手,我强烈建议为此使用适当的类,您可能需要为此检查Joda Time

Their classes provide methods such as daysBetween that give you the result you want. 他们的类提供了诸如daysBetween之类的方法,可为您提供所需的结果。

Of course you have to parse your String to give you an Object representing this date and time first. 当然,您必须解析您的String才能首先给您一个代表该日期和时间的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何制作自定义字符串格式,例如(yyyy_MM_dd_HH_mm_ss_mobileno_Out.mp3) - How to make custom string format like (yyyy_MM_dd_HH_mm_ss_mobileno_Out.mp3) 如何比较 java 格式“EEE MMM dd HH:mm:ss zzz yyyy”和“yyyy-MM-dd hh:mm:sss”的日期时间? - How to compare the datetime of format "EEE MMM dd HH:mm:ss zzz yyyy" and "yyyy-MM-dd hh:mm:sss" in java? SimpleFormatter dd.mm.yyyy HH:mm:ss - SimpleFormatter dd.mm.yyyy HH:mm:ss 如何将日期和时间格式从“ yyyy-MM-dd HH:mm:ss”更改为“ h:mm a”? - How to change date and time format from“yyyy-MM-dd HH:mm:ss” to “h:mm a”? 如何解析 yyyy-MM-dd HH:mm:ss 到日期? - How to parse a yyyy-MM-dd HH:mm:ss to Date? 在Java 7中将字符串日期转换为yyyy-MM-dd'T'HH:mm:ss.SSSSSS格式的字符串 - Converting string date to string in yyyy-MM-dd'T'HH:mm:ss.SSSSSS format in java 7 解析自定义日期与MM / dd / yyyy hh:mm:ss zzz格式在android中 - Parsing custom date with MM/dd/yyyy hh:mm:ss zzz format in android 格式为“ MM / dd / yyyy HH:mm:ss a”的Java String to Date对象 - Java String to Date object of the format “MM/dd/yyyy HH:mm:ss a” 以(yyyy.MM.dd HH.mm.ss.S)格式解析字符串日期 - Parse String date in (yyyy.MM.dd HH.mm.ss.S) format 格式为“ yyyy-mm-dd HH:mm:ss.SSSS”的Java String to Date对象 - Java String to Date object of the format “yyyy-mm-dd HH:mm:ss.SSSS”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM