工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 1963|回复: 16

怎样取得指定日期的前N天

[复制链接]
发表于 2006-9-10 02:56 | 显示全部楼层 |阅读模式
假如,我要取某个时间的前N天.

比如 20060904的 5天 前,这应该是 20060830

有什么方法可以取得这个时间呢?

欢迎大家一起讨论.

方法不限.
 楼主| 发表于 2006-9-10 03:12 | 显示全部楼层
我想到的一种方法是利用mysql的函数.

用到了msyql的date_format()和date_add()或date_sub()

select date_format(date_add($inputdate,interval $i day),'%Y%m%d');

注意到,这里用了$inputdate表示基日期.$i表示要得到的天数,要想得到前N天,就要在前面加个-号.
表示在$inputdate的基础上加$i天.

select date_format(date_sub($inputdate,interval $i day),'%Y%m%d');

使用date_sub时,$i不用-号,就可以表示取的是前$i天.加了-号就表示取后$i天了.
表示在$inputdate基础上减$i天.

看下运行情况:

  1. mysql> select date_format(date_add(20060904,interval 5 day),'%Y%m%d');
  2. +---------------------------------------------------------+
  3. | date_format(date_add(20060904,interval 5 day),'%Y%m%d') |
  4. +---------------------------------------------------------+
  5. | 20060909                                                |
  6. +---------------------------------------------------------+
  7. 1 row in set (0.01 sec)

  8. mysql> select date_format(date_add(20060904,interval -5 day),'%Y%m%d');
  9. +----------------------------------------------------------+
  10. | date_format(date_add(20060904,interval -5 day),'%Y%m%d') |
  11. +----------------------------------------------------------+
  12. | 20060830                                                 |
  13. +----------------------------------------------------------+
  14. 1 row in set (0.00 sec)

  15. mysql> select date_format(date_sub(20060904,interval 5 day),'%Y%m%d');
  16. +---------------------------------------------------------+
  17. | date_format(date_sub(20060904,interval 5 day),'%Y%m%d') |
  18. +---------------------------------------------------------+
  19. | 20060830                                                |
  20. +---------------------------------------------------------+
  21. 1 row in set (0.00 sec)

  22. mysql> select date_format(date_sub(20060904,interval -5 day),'%Y%m%d');
  23. +----------------------------------------------------------+
  24. | date_format(date_sub(20060904,interval -5 day),'%Y%m%d') |
  25. +----------------------------------------------------------+
  26. | 20060909                                                 |
  27. +----------------------------------------------------------+
  28. 1 row in set (0.00 sec)

复制代码
回复

使用道具 举报

发表于 2006-9-10 09:16 | 显示全部楼层
好方法。
不过如果不用mysql怎么办?
腾讯现在还在用mysql呀~~
回复

使用道具 举报

发表于 2006-9-10 12:11 | 显示全部楼层
原来MYSQL还有这样的函数!
平时只用 select insert update ,看来得再好好学习,天天向上了!
方法有了,不用MYSQL,其它数据厍也应该会有相应的函数的.
楼上说腾讯现在还在用mysql呀~~ ,hjack用就行啦!呵呵~
回复

使用道具 举报

 楼主| 发表于 2006-9-10 18:52 | 显示全部楼层
try another way....
回复

使用道具 举报

发表于 2006-9-10 20:47 | 显示全部楼层
最笨最原始的方法是记录各个月的天数。。。
回复

使用道具 举报

 楼主| 发表于 2006-9-10 21:11 | 显示全部楼层
用java实现:

使用了java.sql.Date类.


  1.                 int N = 5;
  2.                 String date = "2006-9-4";
  3.                 System.out.println(new Date(Date.valueOf(date).getTime()-N*24*60*60*1000));

复制代码


运行结果:2006-08-30
回复

使用道具 举报

发表于 2006-9-10 21:52 | 显示全部楼层
java.util.Date可以实现所有关于日期的计算...
回复

使用道具 举报

 楼主| 发表于 2006-9-10 22:11 | 显示全部楼层
原帖由 wool王 于 2006-9-10 21:52 发表
java.util.Date可以实现所有关于日期的计算...

write one .:time::time:
回复

使用道具 举报

发表于 2006-9-10 23:35 | 显示全部楼层
又授教啦~~
收藏起来哈哈
回复

使用道具 举报

 楼主| 发表于 2006-9-11 01:01 | 显示全部楼层
毕业设计时接触了一下php.但不熟.用php写了一下.见笑了.


  1. <?php
  2. $date='2006-09-04';
  3. list($year,$month,$day)=split('-',$date);
  4. $N=5;
  5. echo date('Y-m-d',mktime(0,0,0,$month,$day,$year)-$N*24*60*60);
  6. ?>

复制代码

运行结果:2006-08-30
回复

使用道具 举报

发表于 2006-9-11 01:13 | 显示全部楼层
都是斑竹们在讨论呀
回复

使用道具 举报

发表于 2006-9-11 21:33 | 显示全部楼层
这样啊?
我用java来实现吧
1.把字符串“2006-09-04”转换成long形
public static long stringToLong(String str) throws ParseException {
DateFormat df = DateFormat.getDateInstance();
date = df.parse(str);
return date.getTime();
}
2.得到了long形格式的时间后分别计算出年,月,日
public static int getYear(long timeLong) {
date = new Date(timeLong);
GregorianCalendar today = new GregorianCalendar();
today.setTime(date);
year = today.get(1);
return year;
}
public static int getMonth(long timeLong) {
date = new Date(timeLong);
GregorianCalendar today = new GregorianCalendar();
today.setTime(date);
mon = today.get(2);
return mon;
}
public static int getDay(long timeLong) {
date = new Date(timeLong);
GregorianCalendar today = new GregorianCalendar();
today.setTime(date);
day = today.get(5);
return day;
}
3.得到了年,月,日然后就计算N天前的日期啦。
public static long getDayBeforeN(long nowLong,int n){
GregorianCalendar moncal = new GregorianCalendar(this.getYear(nowLong),this.getMonth(long nowLong), this.getDay(nowLong)-n);
long day= moncal.getTimeInMillis();
return day;
}
4.再把上面得到的时间转换为String
public static String longToString(long nowLong) {
DateFormat df = DateFormat.getDateInstance();
String nowString = df.format(Long.valueOf(nowLong));
return nowString;
}
回复

使用道具 举报

发表于 2006-9-11 21:38 | 显示全部楼层
当然可以这个可以计算任何日期任何天之前的日期
如:我要计算2006-05-01 10天前的日期
public static void main(String args[]) throws ParseException {
String now="2006-05-01 ";
int n=10;
long nowLong=this.stringToLong(now);
System.out.println(this.longToString(this.getDayBeforeN(this.stringToLong(now),n)));
}
回复

使用道具 举报

发表于 2006-9-21 00:46 | 显示全部楼层
发现论坛好冷清啊,首页居然有10多天前的帖子。
回复

使用道具 举报

发表于 2006-9-21 12:33 | 显示全部楼层
编程版是这样的了。。。

有技术的没时间,没技术的也没时间。。。
回复

使用道具 举报

 楼主| 发表于 2006-9-22 01:12 | 显示全部楼层
谁可以说说bash下的实现方法..
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 06:59

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表