java日期的比较 计算
2015-11-05 11:12
323 查看
做财务 erp之类的系统会经常用到很多时间上的比较和计算,这里我总结了一下我用过的东西
用到 的朋友改改就可以用了public static Date StringToDate2(String str){ if(str==null||str.trim().equals("")) return null; Date date = new Date(); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd"); try { date = sf.parse(str); } catch (ParseException e) { e.printStackTrace(); date = null; } return date; } /* * 获取该周 */ public static Date[] getWeekDays(Date date) { Calendar cale = Calendar.getInstance(); cale.setTime(date); cale.setFirstDayOfWeek(Calendar.SUNDAY); int days = 7; int today = cale.get(Calendar.DAY_OF_WEEK); long millis = cale.getTimeInMillis(); Date dates[] = new Date[days]; for (int i = 1; i <= days; i++) { long sub = (today - i) * 24 * 60 * 60 * 1000L; dates[i - 1] = new Date(millis - sub); } cale = null; return dates; } /* * 获取该月 */ public static Date[] getMonthDays(Date date) { Calendar cale = Calendar.getInstance(); cale.setTime(date); int today = cale.get(Calendar.DAY_OF_MONTH); int days = cale.getActualMaximum(Calendar.DAY_OF_MONTH); long millis = cale.getTimeInMillis(); Date dates[] = new Date[days]; for (int i = 1; i <= days; i++) { long sub = (today - i) * 24 * 60 * 60 * 1000L; dates[i - 1] = new Date(millis - sub); } cale = null; return dates; } public static Date getMonthStartDays() { return getMonthDays(new Date())[0]; } public static Date getMonthEndDays() { return getMonthDays(new Date())[(getMonthDays(new Date()).length)-1]; } /* * 获取该年 */ public static Date[] getYearDays(Date date) { Calendar cale = Calendar.getInstance(); cale.setTime(date); int today = cale.get(Calendar.DAY_OF_YEAR); int days = cale.getActualMaximum(Calendar.DAY_OF_YEAR); long millis = cale.getTimeInMillis(); Date dates[] = new Date[days]; for (int i = 1; i <= days; i++) { long sub = (today - i) * 24 * 60 * 60 * 1000L; dates[i - 1] = new Date(millis - sub); } cale = null; return dates; } /** * 比较两个日期之间的大小 * * @param d1 * @param d2 * @return 前者大于后者返回true 反之false */ public static boolean compareDate(Date d1, Date d2) { Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.setTime(d1); c2.setTime(d2); int result = c1.compareTo(c2); if (result >= 0) return true; else return false; } /** * 返回指定年月的月的最后一天 * * @param year * @param month * @return */ public static Date getLastDayOfMonth(Integer year, Integer month) { Calendar calendar = Calendar.getInstance(); if (year == null) { year = calendar.get(Calendar.YEAR); } if (month == null) { month = calendar.get(Calendar.MONTH); } calendar.set(year, month,-1); calendar.roll(Calendar.DAY_OF_MONTH, -1); return calendar.getTime(); } /** * 返回指定年月的月的第一天 * * @param year * @param month * @return */ public static Date getFirstDayOfMonth(Integer year, Integer month) { Calendar calendar = Calendar.getInstance(); if (year == null) { year = calendar.get(Calendar.YEAR); } if (month == null) { month = calendar.get(Calendar.MONTH); } calendar.set(year, month, 1); return calendar.getTime(); }
相关文章推荐
- 将封装了envi功能的IDL类导出成java类,方便java调用
- jackson java转json hibernate懒加载造成的无限递归问题
- Java代码规范
- java学习4-继承
- JAVA的可变类与不可变类
- java中两种类型变量
- spring mvc 下 word 文档(含图片)导出
- java中怎么判断一个字符串中包含某个字符或字符串
- Swagger与SpringMVC项目整合
- JAVA 集合
- [异常] com.mysql.jdbc.ServerPreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
- 最常用的 Eclipse 快捷键总结
- Java对象序列化
- Spring中的AOP—基于Annotation的配置方式
- Java 编程下的同步代码块
- java.lang.UnsupportedClassVersionError 解决办法
- SpringBoot学习(一)入门
- java体系技术日志
- 创建第一个Android工程-Eclipse创建Android项目
- CXF创建简单的RESTful 风格的webservice