java关于时间的相关操作
/*** 获取当天时间零点
* @return
*/
public Date gettoday(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String day = sdf.format(new Date());
Date today = null;
try {
today = sdf.parse(day);
} catch (ParseException e) {
e.printStackTrace();
}
return today;
}
/*** 时间转字符串(20181117)
* @param time
* @return
*/
public String datetostring(Date time){
//设置要获取到什么样的时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//获取String类型的时间
String createdate = sdf.format(time);
return createdate;
}
/*** 日期增加天数
* @param date
* @param num
* @return
*/
public Date addDay(Date date, int num) {
Calendar startDT = Calendar.getInstance();
startDT.setTime(date);
startDT.add(Calendar.DAY_OF_MONTH, num);
return startDT.getTime();
}
/*** 两日期相差天数
* @param date1
* @param date2
* @return
*/
public int getday(Date date1,Date date2){
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
int day1= cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
return day2-day1;
}
以上是 java关于时间的相关操作 的全部内容, 来源链接: utcz.com/z/392960.html