数据库中是 2009-09-27 格式的时间数据 我想查询出提前 5 天的数据,也就是 2009-09-22之前的。其中要考虑到情况包括 特殊情况 月初 和 年初 比如 月初是 2009-06-02 提前 5 天 就要是 2009-05-27或28 (每个月的天数可以不考虑) 年初 是 2009-01-02 提前 5 天就是 2008-12-27(没考虑每个月的天数),
当然考虑每个月的天数的更好 。哪位做过啊 谢谢了。
当然考虑每个月的天数的更好 。哪位做过啊 谢谢了。
如果要考虑每个月天数,很复查,得写过程了吧
另一种是直接用Calendar.add方法。
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()));
}
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);
}
-----------------------------------------------------------------------------------
楼主是要查询数据库中前5天的数据,你们给个得到前5天的时间有什么用?
数据库中getdate()函数已经有解析时间的功能了,直接+ or - 用就OK!
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
数据库的话会因为各个数据库之间的差异存在不同
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()));
}
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天前的日期。
另外多说几句,你要判断多少天之前,最好把时间精确到秒。不然这个误差就大了。