比较日期不正常 [英] comparing dates not working

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

问题描述

我有一个问题。
我的数据库中的开始/结束日期(date,date2)存储为dd / mm / yyyy。
我想测试如果今天的日期是在我刚才提到的两个日期之间。
我尝试了这个布尔函数,但它不工作,我不知道为什么它返回false所有的时间:

  private Boolean test(){
Boolean bool = false;
String ch,ch2;
日期d = new Date();
Date date2 = new Date();
Date date = new Date();
try

{
rs = st.executeQuery(select * from mytab);
if(rs.next())
{
ch = rs.getString(2);
ch2 = rs.getString(3);
try
{
date = new SimpleDateFormat(dd-MM-YYYY,Locale.ENGLISH).parse(ch);
date2 = new SimpleDateFormat(dd-MM-YYYY,Locale.ENGLISH).parse(ch2);
} catch(ParseException s){
System.out.println(检查解析);
}
if(d.before(date2)&& d.after(date))
{
bool = true;
}
}
}
catch(SQLException e){System.out.println(检查SQl);}

return bool;
}


解决方案

因为你的日期值被存储在两种不同的格式中...为了将日期/时间信息存储为文本,您需要尝试多个格式化程序才能解析值返回值。



Apache Commons有一个API可以做到这一点,但你也可以自己编写,例如...

  import java。 text.DateFormat; 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date

public class Test {

public static void main(String [] args){
String startDate =14-08-2010;
String endDate =20/08/2020;

SimpleDateFormat sdf [] = new SimpleDateFormat [] {
new SimpleDateFormat(dd / MM / yyyy),
new SimpleDateFormat(dd-MM-yyyy)} ;

Date dateStart = parse(startDate,sdf);
Date dateEnd = parse(endDate,sdf);

System.out.println(Is today bewteen+ dateStart +和+ dateEnd);
日期date = new Date();
if(today.after(dateStart)&& today.before(dateEnd)){
System.out.println(... Yes);
} else {
System.out.println(... No);
}
}

public static Date parse(String value,DateFormat ... formatters){
Date date = null;
for(DateFormat formatter:formatters){
try {
date = formatter.parse(value);
break;
} catch(ParseException e){
}
}
返回日期;
}

}

打印出...



 今天是星期六8月14日00:00:00 EST 2010和8月20日00:00:00 EST 2020 
...是


Hey guys I have a problem. I have a start/end dates in my database (date,date2) stored as dd/mm/yyyy. I want to test if today's date is between the two dates that I've just mentionned. I tried this boolean function but it doesn't work I don't know why it returns false all the time:

private Boolean test() {
    Boolean bool = false;
    String ch,ch2;
    Date d=new Date();
    Date date2=new Date();
    Date date=new Date();
    try

    {   
        rs=st.executeQuery("select *from mytab");
        if(rs.next())
        {
            ch = rs.getString(2);
            ch2 = rs.getString(3);
            try
            {
                date = new SimpleDateFormat("dd-MM-YYYY", Locale.ENGLISH).parse(ch);
                date2 = new SimpleDateFormat("dd-MM-YYYY", Locale.ENGLISH).parse(ch2);
            }catch(ParseException s){
                System.out.println("Check parsing");
            }
            if(d.before(date2) && d.after(date))
            { 
                bool=true;
            }
        }
    }
    catch (SQLException e){System.out.println("Check the SQl");}    

return bool;
}

解决方案

Because your date values are stored in two different formats...yea for storing date/time information as text, you need to try multiple formatters in order to parse the values back to date values.

Apache Commons has a API which can do this, but you could write your own as well, for example...

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {

  public static void main(String[] args) {
    String startDate = "14-08-2010";
    String endDate = "20/08/2020";

    SimpleDateFormat sdf[] = new SimpleDateFormat[] {
      new SimpleDateFormat("dd/MM/yyyy"),  
      new SimpleDateFormat("dd-MM-yyyy")};

    Date dateStart = parse(startDate, sdf);
    Date dateEnd = parse(endDate, sdf);

    System.out.println("Is today bewteen " + dateStart + " and " + dateEnd);
    Date today = new Date();
    if (today.after(dateStart) && today.before(dateEnd)) {
      System.out.println("...Yes");
    } else {
      System.out.println("...No");
    }
  }

  public static Date parse(String value, DateFormat... formatters) {
    Date date = null;
    for (DateFormat formatter : formatters) {
      try {
        date = formatter.parse(value);
        break;
      } catch (ParseException e) {
      }
    }
    return date;
  }

}

Prints out...

Is today bewteen Sat Aug 14 00:00:00 EST 2010 and Thu Aug 20 00:00:00 EST 2020
...Yes

这篇关于比较日期不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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