来,楼主。分享一个我平时总结的日期常用的方法
- package date;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- /**
- * @author 小媛 <br>
- * ①月份所填的值 要比实际值小1. 所以参数-1<br>
- * ②星期 要比实际值大1. 所以参数+1<br>
- */
- public class Demo_Date {
- public static void main(String[] args) {
- //需要什么方法从下面调取 验证
- }
- /**
- * 获取两个时间之间 的 间隔天数yi
- *
- * @param start
- * @param end
- * @return
- */
- public static int getIntervalTimes(Date start, Date end) {
- if (start.after(end)) {
- Date date = start;
- start = end;
- end = date;
- }
- long startTimes = start.getTime();
- long endTimes = end.getTime();
- long intervalTimes = startTimes - endTimes;
- return (int) (intervalTimes / (1000 * 60 * 60 * 24));
- }
- /**
- * 获取两个时间之间 的 间隔天数
- *
- * @param start
- * @param end
- * @return
- */
- public static int getIntervalTimes(Calendar start, Calendar end) {
- if (start.after(end)) {
- Calendar temp = start;
- start = end;
- end = temp;
- }
- long startTimes = start.getTimeInMillis();
- long endTimes = start.getTimeInMillis();
- long intervalTimes = startTimes - endTimes;
- return (int) (intervalTimes / (1000 * 60 * 60 * 24));
- }
- /**
- * 获取某年某月某日,在添加了×天之后,又回到本月的第几天
- *
- * @param year
- * @param mouth
- * @param date
- * @param addDate
- */
- private static String getDateWithAddAtMouth(int year, int mouth, int date, int addDate) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.YEAR, year);
- calendar.set(Calendar.MONTH, mouth - 1);
- calendar.set(Calendar.DAY_OF_MONTH, date);
- calendar.roll(Calendar.DATE, addDate);
- Date time = calendar.getTime();
- return simpleDateFormat.format(time);
- }
- /**
- * 计算某年某月某日 的基础上 添加×天
- *
- * @param year
- * @param mouth
- * @param day
- * @param addDay
- */
- private static String getDateWithAdd(int year, int mouth, int day, int addDay) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.YEAR, year);
- calendar.set(Calendar.MONTH, mouth - 1);
- calendar.set(Calendar.DAY_OF_MONTH, day);
- calendar.add(Calendar.DATE, addDay);
- Date time = calendar.getTime();
- return simpleDateFormat.format(time);
- }
- /**
- * 获取某年的第几个星期几,是×年×月×日
- *
- * @param year
- * @param noWeekOfYear
- * @param noDayOfWeek
- */
- private static String getNoWeekIsNoDay(int year, int noWeekOfYear, int noDayOfWeek) {
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.YEAR, year);
- calendar.set(Calendar.WEEK_OF_YEAR, noWeekOfYear);
- calendar.set(Calendar.DAY_OF_WEEK, noDayOfWeek);
- return simpleDateFormat.format(calendar.getTime());
- }
- /**
- * 获取某年某月某日,是一年中的第几个星期
- *
- * @param year
- * @param mouth
- * @param day
- */
- private static int getDayAtYearByNoWeek(int year, int mouth, int day) {
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.YEAR, year);
- calendar.set(Calendar.MONTH, mouth - 1);
- calendar.set(Calendar.DAY_OF_MONTH, day);
- int weekNo = calendar.get(Calendar.WEEK_OF_YEAR);
- return weekNo;
- }
- /**
- * 格式化输出 转换日期格式
- */
- private static String Date4Format() {
- Date date = new Date();
- SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
- return fm.format(date);
- }
- /**
- * Date转换成 Calendar格式
- */
- private static void Date4Calendar() {
- Date date = new Date();
- Calendar time = Calendar.getInstance();
- time.setTime(date);
- System.out.println(date);
- }
- /**
- * 获取一个月的最大天数
- */
- private static void getMonthMaxDay() {
- Calendar time = Calendar.getInstance();
- time.clear();
- time.set(Calendar.YEAR, 2015);
- time.set(Calendar.MONTH, 7 - 1);
- int day = time.getActualMaximum(Calendar.DAY_OF_MONTH);
- System.out.println(day);
- }
- }
复制代码 |