package com.dollearn.student.utils; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * Created by zhouyou on 2016/7/25. * Class desc: 日期工具类 */ public class DateUtils { /** * 通过年份和月份 得到当月的日子 */ public static int getMonthDays(int year, int month) { month++; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31; case 4: case 6: case 9: case 11: return 30; case 2: if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { return 29; } else { return 28; } default: return -1; } } /** * 返回当前月份1号位于周几 * * @param year 年份 * @param month 月份,传入系统获取的,不需要正常的 * @return 日:1 一:2 二:3 三:4 四:5 五:6 六:7 */ public static int getFirstDayWeek(int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month, 1); return calendar.get(Calendar.DAY_OF_WEEK); } /** * 查询当前日期前(后)x天的日期 * * @param date 当前日期 * @param day 天数(如果day数为负数,说明是此日期前的天数) * @return yyyyMMdd */ private static String beforNumberDay(Date date, int day) { Calendar c = Calendar.getInstance(); c.setTime(date); c.add(Calendar.DAY_OF_YEAR, day); return new SimpleDateFormat("yyyyMMdd").format(c.getTime()); } }