木头人
2017-09-30 20:53:12
java关于日期的一些常用方法总结
public class DateUtil { //泽勒一致性公式计算星期几 public static String getWeekOfDate(int year, int month, int day) { int q = day; int m; String weekday[] = new String[] { "Saturday", "Sunday", "Monday", "Tuesday", "Wednessday", "Thursday", "Friday" }; switch (month) { case 1: m = 13; year = year - 1; break; case 2: m = 14; year = year - 1; break; default: m = month; break; } int j = year / 100; int k = year % 100; int h = (q + 26 * (m + 1) / 10 + k + k / 4 + j / 4 + 5 * j) % 7; return weekday[h]; } //计算两个时间段之间的星期数 public static Object countTwoDayWeek(String startDate, String endDate) { if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) { long between_days=getDaysBetweenDates(startDate, endDate); Double days = Double.parseDouble(String.valueOf(between_days)); if ((days / 7) > 0 && (days / 7) <= 1) { // 不满一周的按一周算 return 1; } else if (days / 7 > 1) { int day = days.intValue(); if (day % 7 > 0) { return day / 7 + 1; } else { return day / 7; } } else if ((days / 7) == 0) { return 0; } else { // 负数返还null return null; } } return null; } //计算两个时间段的天数 public static long getDaysBetweenDates(String startDate, String endDate) { if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) { DateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date start = null; Date end = null; try { start = ft.parse(startDate); end = ft.parse(endDate); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } Calendar cal = Calendar.getInstance(); cal.setTime(start); long time1 = cal.getTimeInMillis(); cal.setTime(end); long time2 = cal.getTimeInMillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return between_days; } return 0; } //计算两个时间段之间的月数 public static int getMonthSpace(String startDate, String endDate) { DateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); try { c1.setTime(ft.parse(startDate)); c2.setTime(ft.parse(endDate)); } catch (ParseException e) { e.printStackTrace(); } if (c1.getTimeInMillis() < c2.getTimeInMillis()) return 0; int year1 = c1.get(Calendar.YEAR); int year2 = c2.get(Calendar.YEAR); int month1 = c1.get(Calendar.MONTH); int month2 = c2.get(Calendar.MONTH); int day1 = c1.get(Calendar.DAY_OF_MONTH); int day2 = c2.get(Calendar.DAY_OF_MONTH); // 获取年的差值 假设 d1 = 2015-8-16 d2 = 2011-9-30 int yearInterval = year1 - year2; // 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数 if (month1 < month2 || month1 == month2 && day1 < day2) yearInterval--; // 获取月数差值 int monthInterval = (month1 + 12) - month2; if (day1 < day2) monthInterval--; monthInterval %= 12; return yearInterval * 12 + monthInterval; } public static void main(String[] args) { System.out.println(DateUtil.getWeekOfDate(2017, 9, 30)); System.out.println(DateUtil.getDaysBetweenDates("2016-09-30 06:30:15", "2018-09-30 20:30:30")); System.out.println(DateUtil.getMonthSpace("2016-09-30 06:30:15", "2015-08-31 05:30:30")); System.out.println(DateUtil.countTwoDayWeek("2017-09-30 06:30:15", "2017-10-15 05:30:30")); } }
猜你喜欢
请下载代码后再发表评论
相关代码
最近下载