java.util.Date date=new date();//取时间 Calendar calendar = new GregorianCalendar(); calendar.setTime(date); calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动 date=calendar.getTime(); //这个时间就是日期往后推一天的结果
我用了上面这个类 可是好像只能对当前的时间操作
我想要的是对输入的任意一个时间加一天的类 请各位大哥帮帮忙
我用了上面这个类 可是好像只能对当前的时间操作
我想要的是对输入的任意一个时间加一天的类 请各位大哥帮帮忙
然后yearOfInt就是年的整数值,monthOfInt,dayOfInt分别是月和日
Date date=sdf.parse(inputDate);
return date.setDate(date.getDate()+1);
Calendar c=Calendar.getInstance();
c.setTime(d);
c.add(Calendar.DAY_OF_MONTH, 1);
calendar.add(calendar.DATE,1);yearOfInt,monthOfInt,dayOfInt三个参数就是你自己输入的年月日~~
Parameters:
year
the year of the date
month
the month of the date. This value is 0-based; for example, 0 for January
day
the day of the month
hour
the hour (between 0 and 23)
minutes
the minutes (between 0 and 59)
seconds
the seconds (between 0 and 59)
// construct d as current date
GregorianCalendar d = new GregorianCalendar();
d.set(1999, 11, 15);
d.add(Calendar.DAY_OF_MONTH, 1);
public static Timestamp getTimePriviousDay(Timestamp date){
long time = date.getTime();
time = time - 24*60*60*1000;
return new Timestamp(time);
}
/**
* 得到给定日期的前n天
* @param String date YYYY-MM-DD
* @return String dateYYYY-MM-DD
* @author lxb 2007-1-31
*/
public static String getTimePriTenDay(String date,int n){
java.util.Date temDate = ToolsUtil.getTimeStringToDate(date);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(temDate);
gc.add(5,-n);
gc.set(gc.get(gc.YEAR),gc.get(gc.MONTH),gc.get(gc.DATE));
return df.format(gc.getTime());
}
import java.util.*;
/**
* <p>Title: Time </p>
* <p>Description: </p>
* 此类主要用来取得本地系统的系统时间并用下面几种格式显示(可扩充)
* 1. yyyyMMdd 2006年11月13日
* 2. yyMMdd 06年11月13日
* 3. MMdhhmmss 11月13日11时38分25秒
* 4. YYMMDDhhmmss 06年11月13日11时38分25秒
* 5. YYMMDDhhmmssxxx 06年11月13日11时38分25秒234毫秒
* <p>Copyright: computech Copyright (c) 2006</p>
* <p>Company: computech </p>
* @author computech
* @version 1.0
*/
public class CTime {
/*时间格式*/
public static final String YYYYMMDD="yyyyMMdd";
public static final String YYMMDD="yyMMdd";
public static final String MMDDHHMMSS="MMdhhmmss";
public static final String YYMMDDhhmmss="yyMMddhhmmss";
public static final String YYMMDDhhmmssxxx="yyMMddhhmmssSSS";
/*时间便宜类型*/
public static final int directTypeOfYear=1;
public static final int directTypeOfMonth=2;
public static final int directTypeOfDay=7;
/*偏移方向*/
public static final int toBefore=0;/*向前*/
public static final int toAfter=1;/*向后*/
/**
* 取得本地系统的时间,时间格式由参数决定
* @param format 时间格式由常量决定
* @return String 具有format格式的字符串
*/
public static String getTime(String format1)
{
String result=null;
Calendar time=Calendar.getInstance();
SimpleDateFormat format=new SimpleDateFormat(format1);
result=format.format(time.getTime());
return result;
}
/**
*
* @param date 待转化的时间
* @param direction 偏移方向,toBefore是向前,toAfter是向后
* @param offset 偏移量
* @param directType 偏移类型directTypeOfYear按年偏移,directTypeOfMonth是按月,directTypeOfDay按天偏移
* @return 返回偏移后时间,格式:yyyyMMdd
*/
public static String RedirectDate(String date,int direction,int offset,int directType)
{ //从参数date中截取年,月,日
String result=null;
if(date==null)return null;
int yearOfInt=Integer.parseInt(date.substring(0,4));
int monthOfInt=Integer.parseInt(date.substring(4,6));
monthOfInt--;
int dayOfInt=Integer.parseInt(date.substring(6,8));
if(direction==toBefore)offset=-offset;
//对日期进行处理,并返回处理后的日期
GregorianCalendar worldTour = new GregorianCalendar(yearOfInt,monthOfInt,dayOfInt);
worldTour.add(directType,offset);
SimpleDateFormat time=new SimpleDateFormat("yyyyMMdd");
Date d = worldTour.getTime();
result=time.format(d);
return result;
}
/**
* <P>给定时间的年月,返回该年该月末的日期</P>
* @param year
* @param month
* @return
*/
public static String getLastDayOfMonth(String year,String month)
{
String result=null;
int year_int=Integer.parseInt(year);
int month_int=Integer.parseInt(month)-1;
int day_int=1;
GregorianCalendar worldTour = new GregorianCalendar(year_int,month_int,day_int);
int currentDayOfMonth = worldTour.get(Calendar.DAY_OF_MONTH);/*取得现在的月份*/
int maxDayOfMonth = worldTour.getActualMaximum(Calendar.DATE);/* 取得该月的最大天数*/
worldTour.add(Calendar.DAY_OF_MONTH,maxDayOfMonth-currentDayOfMonth);/*把这月的时间偏移到月末*/
SimpleDateFormat time=new SimpleDateFormat(YYYYMMDD);
result=time.format(worldTour.getTime());
return result;
}
}
System.out.println(date);
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE,1);
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//String date1 = dateFormat.format(date);
String time = dateFormat.format(calendar.getTime());
Date time2 = dateFormat.parse(time);
System.out.println(time);
System.out.println(time2);
calendar.setTime(date); calendar.add(calendar.DAY_OF_YEAR,1)
或者calendar.add(calendar.DAY_OF_MONTH,1)
那个date是你从页面或其他方式得到的date格式的日期
还有很多方法可以参考Calendar 用法。
楼上方法太复杂了!