以String格式比较日期 [英] compare dates in String format

查看:372
本文介绍了以String格式比较日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到两个日期作为字符串值,我想检查开始时间早于结束时间。我比较它们,因为它不使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆