java中日期怎么转换成月份月份

2023-04-20 16:35 58次浏览 攻略

java日期加减年月日

/**

* 日期相加减

* @param time

* 时间字符串 yyyy-MM-dd HH:mm:ss

* @param num

* 加的数,-num就是减去

* @return

* 减去相应的数量的年的日期

* @throws ParseException

*/

public static Date yearAddNum(Date time, Integer num) {

//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//Date date = (time);

Calendar calendar = Calendar.getInstance();

calendar.setTime(time);

calendar.add, num);

Date newTime = calendar.getTime();

return newTime;

}

/**

*

* @param time

* 时间

* @param num

* 加的数,-num就是减去

* @return

* 减去相应的数量的月份的日期

* @throws ParseException Date

*/

public static Date monthAddNum(Date time, Integer num){

//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//Date date = (time);

Calendar calendar = Calendar.getInstance();

calendar.setTime(time);

calendar.add, num);

Date newTime = calendar.getTime();

return newTime;

}

/**

*

* @param time

* 时间

* @param num

* 加的数,-num就是减去

* @return

* 减去相应的数量的天的日期

* @throws ParseException Date

*/

public static Date dayAddNum(Date time, Integer num){

//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//Date date = (time);

Calendar calendar = Calendar.getInstance();

calendar.setTime(time);

calendar.add, num);

Date newTime = calendar.getTime();

return newTime;

}

/**

* 获取本月第一天时间

*/

public static Date getMonthStartDate(){

Calendar calendar = Calendar.getInstance();

calendar.set,1);

return calendar.getTime();

}

/**

* 获取本月最后一天

*

*/

public static Date getMonthEndDate(){

Calendar calendar = Calendar.getInstance();

calendar.set, calendar.getActualMaximum));

return calendar.getTime();

}

/**

* 获取本周的开始时间

*/

public static Date getBeginWeekDate(){

Calendar cal = Calendar.getInstance();

cal.setTime(new Date());

int dayofweek = cal.ge);

//周日是1 ,周一是 2 ,周二是 3

//所以,当周的第一天 = 当前日期 – 距离周一过了几天(周一过了0天,周二过了1天, 周日过了6天)

// 2 – 周一的(dayofweek:2 )= 0

// 2 – 周二的(dayofweek:3 )= -1

// .

// .

// 2 – 周日的(dayofweek:1) = 1(这个是不符合的需要我们修改)===》2 – 周日的(dayofweek:1 ==》8 ) = -6

if (dayofweek == 1) {

dayofweek += 7;

}

cal.add, 2 – dayofweek);

return cal.getTime();

}

/**

* 本周的结束时间

* 开始时间 + 6天

*/

public static Date getEndWeekDate(){

Calendar cal = Calendar.getInstance();

cal.setTime(new Date());

int dayofweek = cal.ge);

if (dayofweek == 1) {

dayofweek += 7;

}

cal.add, 8 – dayofweek);//2 – dayofweek + 6

return cal.getTime();

}

相关推荐