以String格式比较日期 [英] compare dates in String format
问题描述
SimpleDateFormat
将它们转换为日期,像这样: String startTime =2013-01-02 14:25:56;
String endTime =2013-01-02 14:30:56;
if(endTime.compareTo(startTime)> 0){
System.out.println(结束时间大于开始时间);
}
真的有必要将它们转换为日期和比较?我会想念什么吗?我做正确的事情吗?
真的有必要将它们转换为日期和比较?
如果您不必包括时区,并且可以确保您始终拥有此格式,则词法顺序将起作用。 p>
我会想念什么吗?
灵活性
我做正确的事情?
这取决于观点。我在专门的搜索引擎中使用类似的东西(仅出于性能原因)。通常我转换为日期并比较这些对象。
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 SimpleDateFormat
, like this:
String startTime = "2013-01-02 14:25:56";
String endTime = "2013-01-02 14:30:56";
if(endTime.compareTo(startTime) > 0){
System.out.println("End time is greater than start time");
}
Is it really necessary to convert them to date and compare? Will I miss anything? Am I doing the right thing?
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.
这篇关于以String格式比较日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!