实现代码一:
import ;
import ;
import ;
public class DateTestUtil {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String str="20110823";
Date dt=(str);
Calendar rightNow = ();
(dt);
(,-1);//日期减1年
(,3);//日期加3个月
(Calendar.DAY_OF_YEAR,10);//日期加10天
Date dt1=();
String reStr = (dt1);
(reStr);
}
}
注:在Calendar对象的add方法中,第二个参数为正数表示“加”,负数表示“减”。
代码二: java date 日期加减天数
测试类代码:
import ;
import ;
public class DateTest {
public static void main(String[] arg){
Date now = new Date();
addAndSubtractDaysByGetTime(now,-5);
addAndSubtractDaysByGetTime(now,5);
addAndSubtractDaysByCalendar(now,-5);
addAndSubtractDaysByCalendar(now,5);
}
public static Date addAndSubtractDaysByGetTime(Date dateTime/*待处理的日期*/,int n/*加减天数*/){
//日期格式
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
((new Date(() + n * 24 * 60 * 60 * 1000L)));
//((new Date(() + n * 24 * 60 * 60 * 1000L)));
//注意这里一定要转换成Long类型,要不n超过25时会出现范围溢出,从而得不到想要的日期值
return new Date(() + n * 24 * 60 * 60 * 1000L);
}
public static Date addAndSubtractDaysByCalendar(Date dateTime/*待处理的日期*/,int n/*加减天数*/){
//日期格式
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
calstart = ();
(dateTime);
(.DAY_OF_WEEK, n);
((()));
//((()));
return ();
}
}
运行结果:
2014-10-06
2014-10-16
2014-10-06
2014-10-16
代码三:
在网上查阅资料,加上自己总结的一些关于Date类的工具类。
package ;
import ;
import ;
import ;
public class DateFormat {
/**
* 日期减几年
*/
public static String dateMinusYear(String str) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date dt = (str);
Calendar rightNow = ();
(dt);
(, -1);// 日期减1年
Date dt1 = ();
String reStr = (dt1);
return reStr;
}
/**
* 日期加几年
*/
public static String dateAddYear(String str) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date dt = (str);
Calendar rightNow = ();
(dt);
(, 1);// 日期加1年
Date dt1 = ();
String reStr = (dt1);
return reStr;
}
/**
* 日期减几月
*/
public static String dateMinusMonth(String str) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date dt = (str);//将字符串生成Date
Calendar rightNow = ();
(dt);//使用给定的 Date 设置此 Calendar 的时间。
(, -1);// 日期减1个月
Date dt1 = ();//返回一个表示此 Calendar 时间值的 Date 对象。
String reStr = (dt1);//将给定的 Date 格式化为日期/时间字符串,并将结果添加到给定的 StringBuffer。
return reStr;
}
/**
* 日期加几月
*/
public static String dateAddMonth(String str) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date dt = (str);
Calendar rightNow = ();
(dt);
(, 1);// 日期加3个月
// (Calendar.DAY_OF_YEAR,10);//日期加10天
Date dt1 = ();
String reStr = (dt1);
return reStr;
}
/**
* 获取当前年月的第一个月的str
* @param str
* 201505
* @return 201501
* @throws Exception
*/
public static String dateOneMonth(String str) {
str = (0, () - 2);
str = str + "01";
return str;
}
/**
* 算出所选月份距离一月份有几个月。
* @param str 201509
* @return 9
*/
public static int dateDistanceMonth(String str) {
int i = (str);
int j = ((str));
(i - j);
return i - j + 1;
}
/**
* 获取两个时间的时间差,精确到毫秒
* @param str
* @return
*/
public static String TimeDifference(long start, long end) {
long between = end - start;
long day = between / (24 * 60 * 60 * 1000);
long hour = (between / (60 * 60 * 1000) - day * 24);
long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
- min * 60 * 1000 - s * 1000);
String timeDifference = day + "天" + hour + "小时" + min + "分" + s + "秒" + ms
+ "毫秒";
return timeDifference;
}
}
/**
* 获取24小时、一周、一个月的起始时间
*
* @param timeInterval
* : DAY_TIME_INTERVAL WEEK_TIME_INTERVAL MONTH_TIME_INTERVAL
* @return "yyyy-mm-dd hh:mm:ss"
*/
public static String getStartTime(int timeInterval) {
Calendar cal = ();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (DAY_TIME_INTERVAL == timeInterval) {// 获取24小时的起始时间
(Calendar.HOUR_OF_DAY, 0);
(, 0);
(, 0);
String startTime = (());
return startTime;
} else if (WEEK_TIME_INTERVAL == timeInterval) {
int weekday = (Calendar.DAY_OF_WEEK) - 1;
(, -weekday);
(Calendar.HOUR_OF_DAY, 0);
(, 0);
(, 0);
String startTime = (());
return startTime;
} else if (MONTH_TIME_INTERVAL == timeInterval) {
int dayofmonthMin = (Calendar.DAY_OF_MONTH);
// (, -dayofmonth);
(, dayofmonthMin);
(Calendar.HOUR_OF_DAY, 0);
(, 0);
(, 0);
String startTime = (());
return startTime;
}
return null;
}
/**
* 获取24小时、一周、一个月的结束时间
*
* @param timeInterval
* : DAY_TIME_INTERVAL WEEK_TIME_INTERVAL MONTH_TIME_INTERVAL
* @return "yyyy-mm-dd hh:mm:ss"
*/
public static String getEndTime(int timeInterval) {
Calendar cal = ();
(("GMT+8"));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (DAY_TIME_INTERVAL == timeInterval) {
(Calendar.HOUR_OF_DAY, 23);
(12, 59);
(13, 59);
long date = ();
String endTime = (new Date(date));
return endTime;
} else if (WEEK_TIME_INTERVAL == timeInterval) {
int weekday = (Calendar.DAY_OF_WEEK) - 1;
(, -weekday);
(, 6);
(Calendar.HOUR_OF_DAY, 23);
(12, 59);
(13, 59);
long date = ();
String endTime = (new Date(date));
return endTime;
} else if (MONTH_TIME_INTERVAL == timeInterval) {
int dayOfMonthMax = (Calendar.DAY_OF_MONTH);
(, dayOfMonthMax);
(Calendar.HOUR_OF_DAY, 23);
(, 59);
(, 59);
String endTime = (());
return endTime;
}
return null;
}
/**
* 判断dateStr是否在start和end中间,start和end都可以为null yyyyMMddHHmmss或者yyyyMMdd格式
*
* @author
* @date 2015年8月19日下午3:11:46
* @param dateStr
* @param start
* @param end
* @return
*/
public static boolean checkDateVal(String dateStr, String start, String end) {
boolean isDateRight = false;
Date date = null;
Date startDate = null;
Date endDate = null;
SimpleDateFormat sdf = null;
// 判断日期格式
if (14 == ()) {
sdf = new SimpleDateFormat("yyyyMMddHHmmss");
} else if (8 == ()) {
sdf = new SimpleDateFormat("yyyyMMdd");
} else
return false;
try {
// 更改判断日期格式
date = (dateStr);
} catch (ParseException e) {
(e, e);
}
if ((start == null) && (end != null)) {
try {
endDate = (end);
} catch (ParseException ex1) {
(ex1, ex1);
}
if ((date != null) && (endDate != null))// Check parameters for
{
if ((endDate) <= 0)
isDateRight = true;
}
} else if ((start != null) && (end == null)) {
try {
startDate = (start);
} catch (ParseException ex1) {
(ex1, ex1);
}
if ((date != null) && (startDate != null)) {
if ((startDate) >= 0)
isDateRight = true;
}
} else if ((start != null) && (end != null)) {
try {
startDate = (start);
endDate = (end);
} catch (ParseException ex2) {
(());
}
if ((startDate != null) && (date != null) && (endDate != null)) {
if (((startDate) >= 0)
&& ((endDate) <= 0))
isDateRight = true;
}
}
return isDateRight;
}
/**
* 判断dateStr是否在start和end中间,start和end都可以为null long形格式
*
* @author
* @date 2015年8月19日下午3:12:35
* @param dateStr
* @param start
* @param end
* @return
*/
public static boolean checkDateV(String dateStr, String start, String end) {
boolean isDateRight = false;
long date = -1;
long fromDate = -1;
long toDate = -1;
date = (dateStr);
if ((start == null) && (end == null)) {
isDateRight = true;
} else if ((start == null) && (end != null)) {
try {
toDate = (end);
} catch (NumberFormatException nfe) {
(nfe, nfe);
}
if (date <= toDate) {
isDateRight = true;
}
} else if ((start != null) && (end == null)) {
try {
fromDate = (start);
} catch (NumberFormatException nfe) {
(nfe, nfe);
}
if (date >= fromDate) {
isDateRight = true;
}
} else if ((start != null) && (end != null)) {
try {
toDate = (end);
fromDate = (start);
} catch (NumberFormatException nfe) {
(nfe, nfe);
}
if ((date <= toDate) && (date >= fromDate)) {
isDateRight = true;
}
}
return isDateRight;
}
目前就用到了这些,随时添加,有简单方便的时间工具类,希望和大家一起学习,在评论中指出。thanks!!!