博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Calendar时间操作
阅读量:6420 次
发布时间:2019-06-23

本文共 6769 字,大约阅读时间需要 22 分钟。

1、Calendar是Java提供的一个util类,在java.util中。我们在开发过程中经常遇到,需要对时间进行处理的方式。而Java中Calendar给我们提供了很好的控制方式。

2、我这里主要是介绍几种Calendar的操作方式来获取不同时间

public class CalendarUtil {    /**     * 获取当月第一天开始时间和最后一天结束时间     * @return     */    public final static List
getMonthStartAndEnd(){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,0); calendar.set(Calendar.DAY_OF_MONTH,1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); Long firstDay = calendar.getTime().getTime(); calendar.add(Calendar.MONTH,1); Long lastDay = calendar.getTime().getTime()-1; List
times = new ArrayList<>(); times.add(firstDay); times.add(lastDay); return times; } /** * 获取当天的开始时间 * @return */ public final static Long getDayStart(){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,0); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); return calendar.getTime().getTime(); } /** * 获取某天的开始时间 * @param day * @return */ public final static Long getDayStart(Integer day){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,0); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); calendar.set(Calendar.DAY_OF_MONTH,day.intValue()); return calendar.getTime().getTime(); } /** * 获取某天的结束时间 * @param day * @return */ public final static Long getDayEnd(Integer day){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,0); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); calendar.set(Calendar.DAY_OF_MONTH,day.intValue()+1); return calendar.getTime().getTime()-1; } /** * 获取本月或者次月某天的结束时间 * @param month * @param day * @return */ public final static Long getDayEnd(Integer month,Integer day){ Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH,month.intValue()); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); calendar.set(Calendar.DAY_OF_MONTH,day.intValue()+1); return calendar.getTime().getTime()-1; } /** * 获取本季度的开始时间和结束时间 * @return */ public final static List
getQuarterStartAndEnd(){ //获取时间 Calendar calendar = Calendar.getInstance(); //判读当前时间是哪个季度 int month = calendar.get(Calendar.MONTH)+1; Integer quarter = 0; if (month>=1 && month <=3) { quarter = 1; } else if (month>=4 && month <=6) { quarter = 2; } else if (month>=7 && month <=9) { quarter = 3; } else if (month>=10 && month <=12) { quarter = 4; } calendar.set(Calendar.MONTH,3*(quarter.intValue()-1)); calendar.set(Calendar.DAY_OF_MONTH,1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); Long firstDay = calendar.getTime().getTime(); calendar.add(Calendar.MONTH,3); Long lastDay = calendar.getTime().getTime()-1; List
times = new ArrayList<>(); times.add(firstDay); times.add(lastDay); return times; } /** * 获取每个季度第某个月,第某个天数的开始时间 * @return */ public final static List
getQuarterStart(Integer month,Integer day){ List
times = new ArrayList<>(); for (int i = 1; i <= 4; i++) { //获取时间 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH,3*(i-1)-1+month.intValue()); calendar.set(Calendar.DAY_OF_MONTH,day); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); times.add(calendar.getTime().getTime()); } return times; } /** * 获取某个季度某个月,某天的结束时间 * @return */ public final static Long getQuarterEnd(Integer month,Integer day){ //获取时间 Calendar calendar = Calendar.getInstance(); //判读当前时间是哪个季度 int currentMonth = calendar.get(Calendar.MONTH)+1; Integer quarter = 0; if (currentMonth>=1 && currentMonth <=3) { quarter = 1; } else if (currentMonth>=4 && currentMonth <=6) { quarter = 2; } else if (currentMonth>=7 && currentMonth <=9) { quarter = 3; } else if (currentMonth>=10 && currentMonth <=12) { quarter = 4; } calendar.set(Calendar.MONTH,3*(quarter.intValue()-1)+month.intValue()); calendar.set(Calendar.DAY_OF_MONTH,day+1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); return calendar.getTime().getTime()-1; } /** * 获取当年的开始时间和结束时间 * @return */ public final static List
getYearStartAndEnd(){ Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH,0); calendar.set(Calendar.DAY_OF_MONTH,1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); Long firstDay = calendar.getTime().getTime(); calendar.add(Calendar.MONTH,12); Long lastDay = calendar.getTime().getTime()-1; List
times = new ArrayList<>(); times.add(firstDay); times.add(lastDay); return times; } /** * 获取当前年第某个月,第某个天数的开始时间 * @return */ public final static Long getYearStart(Integer month,Integer day){ //获取时间 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH,month.intValue()-1); calendar.set(Calendar.DAY_OF_MONTH,day.intValue()); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); return calendar.getTime().getTime(); } /** * 获取当前年第某个月,第某个天数的结束时间 * @return */ public final static Long getYearEnd(Integer month,Integer day){ //获取时间 Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.MONTH,month.intValue()-1); calendar.set(Calendar.DAY_OF_MONTH,day.intValue()+1); calendar.set(Calendar.HOUR_OF_DAY,0); calendar.set(Calendar.MINUTE,0); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); return calendar.getTime().getTime()-1; }}

 

转载于:https://www.cnblogs.com/ll409546297/p/7692471.html

你可能感兴趣的文章
Oracle-01:基础命令小结
查看>>
创建用户配置文件
查看>>
New Concept English Two 31 85
查看>>
New Concept English three (29)
查看>>
2014年发生的一些事情
查看>>
hdu3709
查看>>
自定义对话框,时间日期对话框
查看>>
windows上apache+php+mysql环境部署
查看>>
cocos2dx——裁剪节点ClippingNode
查看>>
13. Intellij IDEA调试功能使用总结
查看>>
3. Spring Boot Servlet
查看>>
3n+1问题
查看>>
[FPGA] DE0_NANO eeprom i2C控制程式
查看>>
java 反射之获取泛型对象的所有字段与对应的值(包括父类的)
查看>>
js中修改标签的hidden属性
查看>>
2012年流行的安卓手机浏览器
查看>>
树莓派摄像头直播程序,非常希望有贡献者一起玩
查看>>
mysql 用drop和delete方法删除用户的区别(草稿)
查看>>
vs code插件
查看>>
less学习笔记四
查看>>