compare dates in String format

后端 未结 10 1328
时光说笑
时光说笑 2021-02-12 16:30

I\'m getting two dates as String values and I wanted to check start time is earlier than the end time. I compare them as it is without converting them to date using Simple

相关标签:
10条回答
  • 2021-02-12 17:02

    What you will be missing is the verification if the dates are in fact well-formatted.

    If the dates are formatted exactly as you showed every time, then this will work. If there's any chance that it might be different, then parsing and comparing the resulting Date objects will add at least somewhat of a check.

    For example if one of the two dates happens to be formatted as 2013.01.02 14:30:56 or it even included an unformatted date such as yesterday then your code would silently assume some order (that most likely has little to do with the actual order) and proceed. What it should do is notify the user (or the log file, ...) that some expectation was not met.

    0 讨论(0)
  • 2021-02-12 17:04

    Is it really necessary to convert them to date and compare?

    If you don't have to include timezones and can ensure that you always have this format the lexical order will work.

    Will I miss anything?

    You lose the flexibility

    Am I doing the right thing?

    That depends on the point of view. I use something similar in a specialized search-enginge (only for performance reasons). Usually I convert to Date and compare these objects.

    0 讨论(0)
  • 2021-02-12 17:05

    Yes, it is better to convert the String to Date and compare.

    1. It ensures they are actually valid dates so no 20-20-2012.
    2. Once the conversion is complete only a single number comparison remains.
    3. It allows for great flexibility when comparing dates of different formats.
    4. The alternative is writing code that parses each number and compares it to the other, which is just as much work.
    0 讨论(0)
  • 2021-02-12 17:06

    My dates are coming from a string-based source and are always formatted YYYY-MM-DD, without time zone info or other complications. So when comparing a long list of dates, it's simpler and more efficient to compare them as strings rather than to convert them to date objects first.

    This was never a problem until I made this mistake in my code:

    boolean past = (dateStart.compareTo(now) == -1);
    

    This was giving some incorrect results because compareTo doesn't only return values as -1, 0 or 1. This was the simple fix:

    boolean past = (dateStart.compareTo(now) < 0);
    

    I'm including this gotcha here because this is one of the SO questions I found when trying to figure out what I was doing wrong.

    0 讨论(0)
  • 2021-02-12 17:13

    It is not good practice and a code smell.

    You lose semantic correct and readable code. (and extensibility, timezones, and the other right things, that already had been said)

    You don't want to compare two Strings, you want to compare 2 Dates - so just do this, compare two Date objects.

    If you create unit-tests and test your comparing method, you will never write a method that compares 2 "string"-dates correctly in every case, without converting them to dates.

    0 讨论(0)
  • 2021-02-12 17:20

    I would recommend converting them to date first to be a little safer, but as long as you are SURE the format (or timezone, etc...) will never change on you, then a date compare and comparing that format of string should always be equivalent.

    0 讨论(0)
提交回复
热议问题