import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class DateUtils { public static void main(String[] args) throws ParseException { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); String str = "2012-05-02"; Date date = df.parse(str); Map<String, String> map = getFirstday_Lastday_Month(date); System.out.println("当月第一天:" + getFirstDay());//两种方式getFirstDay(),getFirstDay1() System.out.println("当月最后一天:" + getLastDay());//同 System.out.println("月最后一天:" + getLastDay());//同 System.out.println("一个月前第一天:" + map.get("first")); System.out.println("一个月前最后一天:" + map.get("last")); System.out.println("获取前一个月的第一天:" + getPreMonthFirstDay()); System.out.println("获取前一个月的最后一天:" +getPreMonthLastDay()); Calendar calendar = new GregorianCalendar(); calendar.setTime(date); //放入你的日期 System.out.println("天数为=" + calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); } /** * 计算两个日期相差的月份数 * * @param date1 日期1 * @param date2 日期2 * @param pattern 日期1和日期2的日期格式 * @return 相差的月份数 * @throws ParseException */ public static int countMonths(String date1, String date2, String pattern) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat(pattern); Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.setTime(sdf.parse(date1)); c2.setTime(sdf.parse(date2)); int year = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR); //开始日期若小于结束日期 if (year < 0) { year = -year; return year * 12 + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH); } return year * 12 + c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH) + 1; } //获取当前月第一天:方式一 private static String getFirstDay() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); Date theDate = calendar.getTime(); GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance(); gcLast.setTime(theDate); gcLast.set(Calendar.DAY_OF_MONTH, 1); String day_first = df.format(gcLast.getTime()); StringBuffer str = new StringBuffer().append(day_first).append(" 00:00:00"); return str.toString(); } //获取当前月第一天:方式二 private static String getFirstDay1() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, 0); c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天 String first_Day_Of_Month = df.format(c.getTime()); return first_Day_Of_Month; } //获取当月最后一天:方式一 private static String getLastDay() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); Date theDate = calendar.getTime(); String s = df.format(theDate); StringBuffer str = new StringBuffer().append(s).append(" 23:59:59"); return str.toString(); } //获取当前月最后一天:方式二 private static String getLastDay1() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar ca = Calendar.getInstance(); ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH)); String lastDay = df.format(ca.getTime()); return lastDay; } //获取前月的第一天 private static String getPreMonthFirstDay() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cal_1 = Calendar.getInstance();//获取当前日期 cal_1.add(Calendar.MONTH, -1); cal_1.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天 String firstDay = df.format(cal_1.getTime()); return firstDay; } //获取前月的最后一天 private static String getPreMonthLastDay() { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar cale = Calendar.getInstance(); cale.set(Calendar.DAY_OF_MONTH, 0);//设置为1号,当前日期既为本月第一天 String lastDay = df.format(cale.getTime()); System.out.println("-----2------lastDay:" + lastDay); return lastDay; } /** * 某一个月第一天和最后一天 */ private static Map<String, String> getFirstday_Lastday_Month(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, -1); Date theDate = calendar.getTime(); //上个月第一天 GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance(); gcLast.setTime(theDate); gcLast.set(Calendar.DAY_OF_MONTH, 1); String day_first = df.format(gcLast.getTime()); StringBuffer str = new StringBuffer().append(day_first).append(" 00:00:00"); day_first = str.toString(); //上个月最后一天 calendar.add(Calendar.MONTH, 1); //加一个月 calendar.set(Calendar.DATE, 1); //设置为该月第一天 calendar.add(Calendar.DATE, -1); //再减一天即为上个月最后一天 String day_last = df.format(calendar.getTime()); StringBuffer endStr = new StringBuffer().append(day_last).append(" 23:59:59"); day_last = endStr.toString(); Map<String, String> map = new HashMap<String, String>(); map.put("first", day_first); map.put("last", day_last); return map; } public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException { ArrayList<String> result = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月 Calendar min = Calendar.getInstance(); Calendar max = Calendar.getInstance(); min.setTime(sdf.parse(minDate)); min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1); max.setTime(sdf.parse(maxDate)); max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2); Calendar curr = min; while (curr.before(max)) { result.add(sdf.format(curr.getTime())); curr.add(Calendar.MONTH, 1); } return result; } //获取某一日期的哪一天 public static int getDayOfMonth(String nowDate) { Calendar c = Calendar.getInstance();//获得一个日历的实例 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = sdf.parse(nowDate);//初始日期 } catch (Exception e) { } c.setTime(date);//设置日历时间 return c.get(Calendar.DAY_OF_MONTH); } public static String dateFormatToString(Date date, String formatString) { java.text.SimpleDateFormat formatter = new SimpleDateFormat(formatString); return formatter.format(date);//格式化数据 } public static Date dateFormatToDate(String date) throws ParseException { java.text.SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd "); return formatter.parse(date); } public static int compare_date(String nowDATE, String endDATE) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); try { Date dt1 = df.parse(nowDATE); Date dt2 = df.parse(endDATE); if (dt1.getTime() > dt2.getTime()) { //System.out.println("dt1 在dt2前"); return 1; } else if (dt1.getTime() < dt2.getTime()) { //System.out.println("dt1在dt2后"); return -1; } else { return 0; } } catch (Exception exception) { exception.printStackTrace(); } return 0; } public static int getMonthToDay(String date) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = new GregorianCalendar(); Date date1 = sdf.parse(date); calendar.setTime(date1); //放入你的日期 return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); } //获取当前时间的年和月: public int getCurrentYearAndMonth() throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM"); Date date = new Date(); String currentYearAndMonth = formatter.format(date); int currentDate = Integer.parseInt(currentYearAndMonth); return currentDate; }//======================================比较两个日期相差的天数 ================================================ //(版本1):根据您输入的小时与分不同,返回不同的结果的, // 比如你输入的参数今天中午与昨天晚上的两个时间,它就会告诉你相差0天 public static int getIntervalDays(Date fDate, Date oDate) { if (null == fDate || null == oDate) { return -1; } long intervalMilli = oDate.getTime() - fDate.getTime(); return (int) (intervalMilli / (24 * 60 * 60 * 1000)); } //(版本2) public static int daysBetween(Date smdate,Date bdate) throws ParseException{ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); smdate=sdf.parse(sdf.format(smdate)); bdate=sdf.parse(sdf.format(bdate)); Calendar cal = Calendar.getInstance(); cal.setTime(smdate); long time1 = cal.getTimeInMillis(); cal.setTime(bdate); long time2 = cal.getTimeInMillis(); long between_days=(time2-time1)/(1000*3600*24); return Integer.parseInt(String.valueOf(between_days)); } //(版本3):你输入的参数今天中午与昨天晚上的两个时间,只关心天,因此输出的结果是1 public static int daysOfTwo(Date fDate, Date oDate) { Calendar aCalendar = Calendar.getInstance(); aCalendar.setTime(fDate); int day1 = aCalendar.get(Calendar.DAY_OF_YEAR); aCalendar.setTime(oDate); int day2 = aCalendar.get(Calendar.DAY_OF_YEAR); return day2 - day1; } //======================================================================================}
java处理日期时间的方法
最新推荐文章于 2023-07-12 08:43:41 发布