数据库中是 2009-09-27 格式的时间数据 我想查询出提前 5 天的数据,也就是 2009-09-22之前的。其中要考虑到情况包括 特殊情况 月初  和 年初 比如 月初是 2009-06-02 提前 5 天 就要是 2009-05-27或28 (每个月的天数可以不考虑) 年初 是 2009-01-02 提前 5 天就是 2008-12-27(没考虑每个月的天数),
当然考虑每个月的天数的更好 。哪位做过啊  谢谢了。

解决方案 »

  1.   

    date - 5
    如果要考虑每个月天数,很复查,得写过程了吧
      

  2.   

    有2种方法,一种 是直接用SQL语句减5天,sysdate-5
    另一种是直接用Calendar.add方法。
      

  3.   

    select * from 表名 where 时间 in(getdate()-5,getdate())
      

  4.   


    private static void displayDay(int day){
    SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE,day);
    System.out.println(sdf.format(cal.getTime()));
    }
      

  5.   

    Calendar已经考虑了这些特殊情况
      

  6.   


    public static void main(String[] args) throws ParseException{
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
    Calendar c=Calendar.getInstance();
    c.setTime(sdf.parse("2009-09-27"));   
    c.add(Calendar.DATE, -5);    
    String result = sdf.format(c.getTime());    
        System.out.println("相加/减后的日期:"+result);   
    }
      

  7.   

    你是什么数据库,如果是oracle数据库那在oracle函数里就查以完成了.
      

  8.   

    数据库中是 2009-09-27 格式的时间数据 我想查询出提前 5 天的数据,也就是 2009-09-22之前的。
    -----------------------------------------------------------------------------------
    楼主是要查询数据库中前5天的数据,你们给个得到前5天的时间有什么用?
    数据库中getdate()函数已经有解析时间的功能了,直接+ or - 用就OK!
      

  9.   

    补上,我的是SQL2005数据库,测试过了!如果是mysql 你用now()函数!
      

  10.   

    大连知名IT企业急聘 : DB DesignerPosition: DB Designer
    Work Location:China-Dalian
     
    Responsibilities:
    1.        Engaged in developing on DB design standard, templates, guidelines/checklists and tools.
    2.        Participating in enforcing agreed standards, guidelines, and principles
    3.        Close cooperation with the project manager, team members, clients’ representatives and other technical personnel and stakeholders
     
    Requirements:
    1.        4 year+ years gathering and analyzing data requirements and data modeling
    2.        2 year+ experience with documenting DB design using E-R diagram or UML diagram
    3.        Experience on data migration planning is a plus.
    4.        Working knowledge in agile, RUP or equivalent methodologies and project processes is an asset.
    5.        Excellent customer-facing and communication skill
    6.        Proven ability to work creatively and analytically in a problem-solving environment
    7.        Excellent communication (written and oral) and interpersonal skillsIf  you are interested in this position, please forward your resume to [email protected]
    Please feel free to contact me by QQ :617922204, 15940853951(PHONE) 
    Headhunting consultant : Eric 
      

  11.   

    楼主可以考虑使用数据库函数来,oracle的sql函数支持日期的加减
      

  12.   

    Calendar
    数据库的话会因为各个数据库之间的差异存在不同
      

  13.   

    private static void displayDay(int day){
            SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.DATE,day);
            System.out.println(sdf.format(cal.getTime()));
        }
      

  14.   


    Date date = new Date();//得到当前的时刻
    long second = date.getTime() / 1000 - 3600 * 24 * 5;
    date.setTime(second * 1000);//得到5天前的时刻,精确到秒。
    //再把date转化为字符型,按你的要求就是转为("yyyy-mm-dd");我说一下,这里根本就不用考虑什么月的天数啊什么的。5天前,因为date里面存的是1970年到今天此刻的毫秒数。date会自动去判断5天前的日期。
    另外多说几句,你要判断多少天之前,最好把时间精确到秒。不然这个误差就大了。