日期计算 请教大家,我有一个日期属性:如2009-01-31 这个是到期日期 我怎么判断在这个日期的前30天 就开始提醒用户即将到期 简单说就是 一个东西 1月31号到期 我1月1号的时候 就要给用户显示提示 请问这样怎么实现?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主只需把我这个函数稍作修改: /** * 获得距离今天几天的一个具体时刻 如:getCurrentTim(1,2,0,0) 获取明天的凌晨2点0分0秒的这个Date * getCurrentTim(-1,2,0,0) 获取昨天的凌晨2点0分0秒的这个Date create date:2009-2-12 create * date:2009-6-9 author:Administrator * * @param day * (-1:代表昨天;0:代表今天;1:代表明天) * @param hour * @param minute * @param second * @return */ public static Date getCurrentTim(int day, int hour, int minute, int second) { Calendar calen = Calendar.getInstance(); calen.add(Calendar.DATE, day); calen.set(Calendar.HOUR_OF_DAY, hour); Date date = calen.getTime(); // 这里也个用SimpleDateFormat的format()进行格式化,然后以字符串形式返回格式化后的date // SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // dateFormat.format(date); return date; } Calendar.set(Calendar.DAY_OF_MONTH, int date) LZ参考一下:package date;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class CalculateDate { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { String date = "2009-05-01"; java.text.SimpleDateFormat format = new java.text.SimpleDateFormat( "yyyy-MM-dd"); Date input = format.parse(date); System.out.println(((new Date()).getTime() - input.getTime()) / 24 / 60 / 60 / 1000); String oldDate = "2008-02-28";// SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Date oDate = null; oDate = format.parse(oldDate); Date curDate = new Date(); long temp = (curDate.getTime() - oDate.getTime()) / 1000; int years = (int) temp / (60 * 60 * 24 * 365); int num = (int) temp % (60 * 60 * 24 * 365); System.out.print("距今:" + years + "年"); int months = num / (60 * 60 * 24 * 30); num = num % (60 * 60 * 24 * 30); System.out.print(months + "月"); int days = num / (60 * 60 * 24); num = num % (60 * 60 * 24); System.out.print(days + "天"); int hours = num / (60 * 60); num = num % (60 * 60); System.out.print(hours + "时"); int minutes = num / 60; num = num % 60; System.out.print(minutes + "分"); int seconds = num; System.out.print(seconds + "秒"); }} ---如2009-01-31 这个是到期日期判断到期是根据当前时间的前30天是吧public static String get30Date(){ Calendar day = Calendar.getInstance(); //30天前 day.add(day.DAY_OF_YEAR, -30); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); return (String)dateFormat.format(day.getTime()); } 用Calendar,然后可以通过Calendar.DAY_OF_YEAR取到,今天是这一年中的第几天这个值然后根据你的要求慢慢改就行了,1楼2楼都这个思路 一个小练习 关于java输出中文到文件时总是变成?号的求助 水晶报表问题, 用那些jar能生成word文档呢? 高分 请教关于在prototype.js 的ajax.request post的时候中文乱码的问题 使用xml的简单问题~50分,在线等 十万伙计,马上要签约,有哪些兄弟了解下面公司 怎样定时运行java程序 frame显示问题 为什么无法调用库文件? 求解:不用数据库,用程序实现数据的统计: 学习java需要阅读的书
/**
* 获得距离今天几天的一个具体时刻 如:getCurrentTim(1,2,0,0) 获取明天的凌晨2点0分0秒的这个Date
* getCurrentTim(-1,2,0,0) 获取昨天的凌晨2点0分0秒的这个Date create date:2009-2-12 create
* date:2009-6-9 author:Administrator
*
* @param day
* (-1:代表昨天;0:代表今天;1:代表明天)
* @param hour
* @param minute
* @param second
* @return
*/
public static Date getCurrentTim(int day, int hour, int minute, int second) {
Calendar calen = Calendar.getInstance();
calen.add(Calendar.DATE, day);
calen.set(Calendar.HOUR_OF_DAY, hour);
Date date = calen.getTime(); // 这里也个用SimpleDateFormat的format()进行格式化,然后以字符串形式返回格式化后的date
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// dateFormat.format(date);
return date;
}
import java.text.SimpleDateFormat;
import java.util.Date;public class CalculateDate { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
String date = "2009-05-01";
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
Date input = format.parse(date);
System.out.println(((new Date()).getTime() - input.getTime()) / 24 / 60
/ 60 / 1000);
String oldDate = "2008-02-28";
// SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
Date oDate = null;
oDate = format.parse(oldDate); Date curDate = new Date();
long temp = (curDate.getTime() - oDate.getTime()) / 1000;
int years = (int) temp / (60 * 60 * 24 * 365);
int num = (int) temp % (60 * 60 * 24 * 365);
System.out.print("距今:" + years + "年");
int months = num / (60 * 60 * 24 * 30);
num = num % (60 * 60 * 24 * 30);
System.out.print(months + "月");
int days = num / (60 * 60 * 24);
num = num % (60 * 60 * 24);
System.out.print(days + "天");
int hours = num / (60 * 60);
num = num % (60 * 60);
System.out.print(hours + "时");
int minutes = num / 60;
num = num % 60;
System.out.print(minutes + "分");
int seconds = num;
System.out.print(seconds + "秒"); }}
判断到期是根据当前时间的前30天是吧public static String get30Date(){
Calendar day = Calendar.getInstance();
//30天前
day.add(day.DAY_OF_YEAR, -30);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return (String)dateFormat.format(day.getTime());
}
然后根据你的要求慢慢改就行了,1楼2楼都这个思路