compare dates in String format

后端 未结 10 1353
时光说笑
时光说笑 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:29

    Java or not, you must first ask these questions:

    • Do you need to compare time or just days ?
    • Do you need to check TimeZone, or all users are in the same timezone (or you don't care)

    If you need to compare days, then string in 'YYYY-MM-dd' format are just perfect and efficient :

    '2018-01-23' < '2018-03-21'. Always, in any language.

    With time and no timezone, then storing time in database as 'YYYY-MM-dd HH:mm:ss' is also good.

    With Timezone, then you have to learn your platforms (code and database), and understand what utc is (good luck !).

    (Used under CC BY-NC 2.5 license from http://xkcd.com/1179/.)

提交回复
热议问题