compare dates in String format

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

    You can compare the date time without properly parsing it if and only if these 3 conditions are met:

    1. The date time are always in the same format:

      • Same number of fields
      • The fields must be purely numeric. (String comparison: Jan > April, Wed > Thu).
      • The sizes of the fields are fixed (0 padded if necessary). For example, no proper padding will result in 10:01 < 1:01 (: has larger ASCII code than digits).
      • The non-numeric parts (including separators) must be the same down to the spacing.
    2. The fields are ordered in descending order by the size of the unit (larger unit to the right, and smaller unit to the left). For example: YEAR-MONTH-DAY HOUR:MINUTE:SECOND.MILISECOND

    3. They must be in the same time zone. The time zone information, if present, should have the same presentation (SGT and UTC+8 are currently equivalent, but the String comparison won't know about this). Note that the above ambiguous condition "same time zone" is enough for comparing equal, but to compare larger/smaller, there must be no change in time zone happening between the 2 dates being compare.

提交回复
热议问题