工具类
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; /** * Created by qd on 2015/10/26. */ public class DateUtil { public static String getYesterday() { DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_MONTH, -1); return format.format(calendar.getTime()); } public static List<String> getDateList() { List<String> dateList = new ArrayList<String>(); Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.set(Calendar.DAY_OF_MONTH, 1); dateList.add(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime())); cal.set(Calendar.DAY_OF_MONTH, 2); dateList.add(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime())); cal.roll(Calendar.DAY_OF_MONTH, -2); dateList.add(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime())); cal.roll(Calendar.DAY_OF_MONTH, -1); dateList.add(new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime())); return dateList; } public static Long getStartTime() { Calendar todayStart = Calendar.getInstance(); todayStart.set(Calendar.HOUR_OF_DAY, 0); todayStart.set(Calendar.MINUTE, 0); todayStart.set(Calendar.SECOND, 0); todayStart.set(Calendar.MILLISECOND, 0); return todayStart.getTime().getTime(); } public static Long getEndTime() { Calendar todayEnd = Calendar.getInstance(); todayEnd.set(Calendar.HOUR_OF_DAY, 23); todayEnd.set(Calendar.MINUTE, 59); todayEnd.set(Calendar.SECOND, 59); todayEnd.set(Calendar.MILLISECOND, 999); return todayEnd.getTime().getTime(); } // 获取每月的天�? public static int getDaysByMonth() { Calendar cal = Calendar.getInstance(Locale.CHINA); return cal.getActualMaximum(Calendar.DATE); } // 获取当前的月�? public static int getMonth() { Calendar cal = Calendar.getInstance(Locale.CHINA); int month = cal.get(Calendar.MONTH) + 1; return month; } // 获取当月天数 public static int getDay() { Calendar cal = Calendar.getInstance(Locale.CHINA); int day = cal.get(Calendar.DAY_OF_MONTH); //获取当前天数 return day; } // 获取月份第一�? public static Date getDate(int monthNum,int dayNum) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); // 获取当前月第�?天: Calendar cc = Calendar.getInstance(); cc.add(Calendar.MONTH, monthNum); cc.set(Calendar.DAY_OF_MONTH, dayNum);// 设置�?1�?,当前日期既为本月第一�? String first = format.format(cc.getTime()); String str = first+" 00:00:00"; Date d = null; try { d = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(str); } catch (ParseException e) { e.printStackTrace(); } return d; } public static String timeStampToStr(Long timeStamp){ Date date = new Date(); date.setTime(timeStamp); SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); String str=sdf.format(date); return str; } public static Long getFirstDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month-1); cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DATE)); cal.set(Calendar.HOUR_OF_DAY ,0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.MILLISECOND, 001); return cal.getTime().getTime(); } //�?要注意的是:月份是从0�?始的,比如说如果输入5的话,实际上显示的是4月份的最后一天,千万不要搞错了哦 public static Long getLastDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year); cal.set(Calendar.MONTH, month-1); cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY ,23); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.MILLISECOND, 999); // new SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime()) return cal.getTime().getTime(); } public static String Date2Str(Date date, String format) { if (date == null) return null; SimpleDateFormat sdf = new SimpleDateFormat(format); String str = sdf.format(date); return str; } public static Date parseDate(String sDate, String format) { java.text.DateFormat formatter = new java.text.SimpleDateFormat(format); java.util.Date retDate = null; try { retDate = formatter.parse(sDate); } catch (Exception ex) { } return retDate; } public static void main(String args[]) throws ParseException { // System.out.println(getFirstDayOfMonth(2017,10)); // System.out.println(getFirstDayOfMonth(2017,11)-1000); // System.out.println(getMonthLastDay("2017-2")); System.out.println(1); System.out.println(getDaysByMonth()); System.out.println(1); } }