Java对⽇期Date类进⾏加减运算⼀⼆三⼀、充分利⽤SimpleDateFormat
SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateTestUtil {
public static void main(String[] args) throws Exception {
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String str="20110823";
Date dt=sdf.parse(str);
Calendar rightNow = Instance();
rightNow.setTime(dt);
rightNow.add(Calendar.YEAR,-1);//⽇期减1年
rightNow.add(Calendar.MONTH,3);//⽇期加3个⽉
rightNow.add(Calendar.DAY_OF_YEAR,10);//⽇期加10天
Date Time();
String reStr = sdf.format(dt1);
System.out.println(reStr);
}
}
注:在Calendar对象的add⽅法中,第⼆个参数为正数表⽰“加”,负数表⽰“减”。
⼆、java date ⽇期加减天数
SimpleDateFormat;
import java.util.Date;
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/*加减天数*/){
//⽇期格式
error parse newSimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(df.format(new Time() + n * 24 * 60 * 60 * 1000L)));
//System.out.println(dd.format(new Time() + n * 24 * 60 * 60 * 1000L)));
//注意这⾥⼀定要转换成Long类型,要不n超过25时会出现范围溢出,从⽽得不到想要的⽇期值
return new Time() + 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");
java.util.Calendar calstart = java.Instance();
calstart.setTime(dateTime);
calstart.add(java.util.Calendar.DAY_OF_WEEK, n);
System.out.println(df.Time()));
//System.out.println(dd.Time()));
Time();
}
}
不推荐这个,代码不够简洁,看着不爽。
三、⽹上的别⼈的,太长,没看,粘过来先
package com.data.utils;
SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateFormat {
/**
* ⽇期减⼏年
*/
public static String dateMinusYear(String str) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date dt = sdf.parse(str);
Calendar rightNow = Instance();
rightNow.setTime(dt);
rightNow.add(Calendar.YEAR, -1);// ⽇期减1年
Date dt1 = Time();
String reStr = sdf.format(dt1);
return reStr;
}
/**
* ⽇期加⼏年
*/
public static String dateAddYear(String str) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date dt = sdf.parse(str);
Calendar rightNow = Instance();
rightNow.setTime(dt);
rightNow.add(Calendar.YEAR, 1);// ⽇期加1年
Date dt1 = Time();
String reStr = sdf.format(dt1);
return reStr;
}
* ⽇期减⼏⽉
*/
public static String dateMinusMonth(String str) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date dt = sdf.parse(str);//将字符串⽣成Date
Calendar rightNow = Instance();
rightNow.setTime(dt);//使⽤给定的 Date 设置此 Calendar 的时间。
rightNow.add(Calendar.MONTH, -1);// ⽇期减1个⽉
Date dt1 = Time();//返回⼀个表⽰此 Calendar 时间值的 Date 对象。
String reStr = sdf.format(dt1);//将给定的 Date 格式化为⽇期/时间字符串,并将结果添加到给定的 StringBuffer。  return reStr;
}
/**
* ⽇期加⼏⽉
*/
public static String dateAddMonth(String str) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
Date dt = sdf.parse(str);
Calendar rightNow = Instance();
rightNow.setTime(dt);
rightNow.add(Calendar.MONTH, 1);// ⽇期加3个⽉
// rightNow.add(Calendar.DAY_OF_YEAR,10);//⽇期加10天
Date dt1 = Time();
String reStr = sdf.format(dt1);
return reStr;
}
/
**
* 获取当前年⽉的第⼀个⽉的str
* @param str
*  201505
* @return 201501
* @throws Exception
*/
public static String dateOneMonth(String str) {
str = str.substring(0, str.length() - 2);
str = str + "01";
return str;
}
/**
* 算出所选⽉份距离⼀⽉份有⼏个⽉。
* @param str 201509
* @return 9
*/
public static int dateDistanceMonth(String str) {
int i = Integer.parseInt(str);
int j = Integer.parseInt(DateFormat.dateOneMonth(str));
System.out.println(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 = Instance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  if (DAY_TIME_INTERVAL == timeInterval) {// 获取24⼩时的起始时间
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
String startTime = sdf.Time());
return startTime;
} else if (WEEK_TIME_INTERVAL == timeInterval) {
int weekday = (Calendar.DAY_OF_WEEK) - 1;
cal.add(Calendar.DATE, -weekday);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
String startTime = sdf.Time());
return startTime;
} else if (MONTH_TIME_INTERVAL == timeInterval) {
int dayofmonthMin = ActualMinimum(Calendar.DAY_OF_MONTH);
// c.add(Calendar.DATE, -dayofmonth);
cal.set(Calendar.DATE, dayofmonthMin);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
String startTime = sdf.Time());
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 = Instance();
cal.TimeZone("GMT+8"));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  if (DAY_TIME_INTERVAL == timeInterval) {
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(12, 59);
cal.set(13, 59);
long date = TimeInMillis();
String endTime = sdf.format(new Date(date));
return endTime;
} else if (WEEK_TIME_INTERVAL == timeInterval) {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。