java获取日,月,年开始结束时间
2021/9/18 14:34:43
本文主要是介绍java获取日,月,年开始结束时间,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.Beichen.XHealthView.XRecommendation; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Test { public static void main(String[] args) { SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd"); try { Calendar c = Calendar.getInstance(); Date now = null; c.set(Calendar.DATE, 1); now = shortSdf.parse(shortSdf.format(c.getTime())); System.out.println("月开始:"+shortSdf.format(now)); Calendar cc = Calendar.getInstance(); cc.set(Calendar.DATE, cc.getActualMaximum(Calendar.DAY_OF_MONTH)); System.out.println("月结束:"+shortSdf.format(cc.getTime())); } catch (Exception e) { e.printStackTrace(); } System.out.println("当前小时开始:"+getCurrentHourStartTime().toString()); System.out.println("当前小时结束:"+getCurrentHourEndTime().toString()); System.out.println("当前天开始:"+getCurrentDayStartTime().toString()); System.out.println("当前天时结束:"+getCurrentDayEndTime().toString()); System.out.println("当前周开始:"+getCurrentWeekDayStartTime().toString()); System.out.println("当前周结束:"+getCurrentWeekDayEndTime().toString()); System.out.println("当前月开始:"+getCurrentMonthStartTime().toString()); System.out.println("当前月结束:"+getCurrentMonthEndTime().toString()); System.out.println("当前季度开始:"+getCurrentQuarterStartTime().toString()); System.out.println("当前季度结束:"+getCurrentQuarterEndTime().toString()); System.out.println("当前半年/后半年开始:"+getHalfYearStartTime().toString()); System.out.println("当前半年/后半年结束:"+getHalfYearEndTime().toString()); System.out.println("当前年开始:"+getCurrentYearStartTime().toString()); System.out.println("当前年结束:"+getCurrentYearEndTime().toString()); } /** * 获取 当前年、半年、季度、月、日、小时 开始结束时间 */ private final static SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd"); private final static SimpleDateFormat longHourSdf = new SimpleDateFormat("yyyy-MM-dd HH");; private final static SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");; /** * 获得本周的第一天,周一 * * @return */ public static Date getCurrentWeekDayStartTime() { Calendar c = Calendar.getInstance(); try { int weekday = c.get(Calendar.DAY_OF_WEEK) - 2; c.add(Calendar.DATE, -weekday); c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00")); } catch (Exception e) { e.printStackTrace(); } return c.getTime(); } /** * 获得本周的最后一天,周日 * * @return */ public static Date getCurrentWeekDayEndTime() { Calendar c = Calendar.getInstance(); try { int weekday = c.get(Calendar.DAY_OF_WEEK); c.add(Calendar.DATE, 8 - weekday); c.setTime(longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59")); } catch (Exception e) { e.printStackTrace(); } return c.getTime(); } /** * 获得本天的开始时间 * * @return */ public static Date getCurrentDayStartTime() { Date now = new Date(); try { now = shortSdf.parse(shortSdf.format(now)); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获得本天的结束时间 * * @return */ public static Date getCurrentDayEndTime() { Date now = new Date(); try { now = longSdf.parse(shortSdf.format(now) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获得本小时的开始时间 * * @return */ public static Date getCurrentHourStartTime() { Date now = new Date(); try { now = longHourSdf.parse(longHourSdf.format(now)); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获得本小时的结束时间 * * @return */ public static Date getCurrentHourEndTime() { Date now = new Date(); try { now = longSdf.parse(longHourSdf.format(now) + ":59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获得本月的开始时间 * * @return */ public static Date getCurrentMonthStartTime() { Calendar c = Calendar.getInstance(); Date now = null; try { c.set(Calendar.DATE, 1); now = shortSdf.parse(shortSdf.format(c.getTime())); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 本月的结束时间 * * @return */ public static Date getCurrentMonthEndTime() { Calendar c = Calendar.getInstance(); Date now = null; try { c.set(Calendar.DATE, 1); c.add(Calendar.MONTH, 1); c.add(Calendar.DATE, -1); now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 当前年的开始时间 * * @return */ public static Date getCurrentYearStartTime() { Calendar c = Calendar.getInstance(); Date now = null; try { c.set(Calendar.MONTH, 0); c.set(Calendar.DATE, 1); now = shortSdf.parse(shortSdf.format(c.getTime())); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 当前年的结束时间 * * @return */ public static Date getCurrentYearEndTime() { Calendar c = Calendar.getInstance(); Date now = null; try { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 当前季度的开始时间 * * @return */ public static Date getCurrentQuarterStartTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 3) c.set(Calendar.MONTH, 0); else if (currentMonth >= 4 && currentMonth <= 6) c.set(Calendar.MONTH, 3); else if (currentMonth >= 7 && currentMonth <= 9) c.set(Calendar.MONTH, 4); else if (currentMonth >= 10 && currentMonth <= 12) c.set(Calendar.MONTH, 9); c.set(Calendar.DATE, 1); now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 当前季度的结束时间 * * @return */ public static Date getCurrentQuarterEndTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 3) { c.set(Calendar.MONTH, 2); c.set(Calendar.DATE, 31); } else if (currentMonth >= 4 && currentMonth <= 6) { c.set(Calendar.MONTH, 5); c.set(Calendar.DATE, 30); } else if (currentMonth >= 7 && currentMonth <= 9) { c.set(Calendar.MONTH, 8); c.set(Calendar.DATE, 30); } else if (currentMonth >= 10 && currentMonth <= 12) { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); } now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获取前/后半年的开始时间 * * @return */ public static Date getHalfYearStartTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 6) { c.set(Calendar.MONTH, 0); } else if (currentMonth >= 7 && currentMonth <= 12) { c.set(Calendar.MONTH, 6); } c.set(Calendar.DATE, 1); now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"); } catch (Exception e) { e.printStackTrace(); } return now; } /** * 获取前/后半年的结束时间 * * @return */ public static Date getHalfYearEndTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; Date now = null; try { if (currentMonth >= 1 && currentMonth <= 6) { c.set(Calendar.MONTH, 5); c.set(Calendar.DATE, 30); } else if (currentMonth >= 7 && currentMonth <= 12) { c.set(Calendar.MONTH, 11); c.set(Calendar.DATE, 31); } now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59"); } catch (Exception e) { e.printStackTrace(); } return now; } }
这篇关于java获取日,月,年开始结束时间的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-28知识管理革命:文档软件的新玩法了解一下!
- 2024-11-28低代码应用课程:新手入门全攻略
- 2024-11-28哪些办公软件适合团队协作,且能够清晰记录每个阶段的工作进展?
- 2024-11-28全栈低代码开发课程:零基础入门到初级实战
- 2024-11-28拖动排序课程:轻松掌握课程拖动排序功能
- 2024-11-28如何高效管理数字化转型项目
- 2024-11-28SMART法则好用吗?有哪些项目管理工具辅助实现?
- 2024-11-28深度剖析:6 款办公软件如何构建设计团队项目可视化管理新生态?
- 2024-11-28HTTP缓存课程:新手入门指南
- 2024-11-28实战丨证券 HTAP 混合业务场景的难点问题应对