-
Java获取各种常用时间方法2
-
- // 获得下周星期一的日期
- public String getNextMonday() {
- weeks++;
- int mondayPlus = this.getMondayPlus();
- GregorianCalendar currentDate = new GregorianCalendar();
- currentDate.add(GregorianCalendar.DATE, mondayPlus + 7);
- Date monday = currentDate.getTime();
- DateFormat df = DateFormat.getDateInstance();
- String preMonday = df.format(monday);
- return preMonday;
- }
-
- // 获得下周星期日的日期
- public String getNextSunday() {
-
- int mondayPlus = this.getMondayPlus();
- GregorianCalendar currentDate = new GregorianCalendar();
- currentDate.add(GregorianCalendar.DATE, mondayPlus + 7+6);
- Date monday = currentDate.getTime();
- DateFormat df = DateFormat.getDateInstance();
- String preMonday = df.format(monday);
- return preMonday;
- }
-
-
-
- private int getMonthPlus(){
- Calendar cd = Calendar.getInstance();
- int monthOfNumber = cd.get(Calendar.DAY_OF_MONTH);
- cd.set(Calendar.DATE, 1);//把日期设置为当月第一天
- cd.roll(Calendar.DATE, -1);//日期回滚一天,也就是最后一天
- MaxDate=cd.get(Calendar.DATE);
- if(monthOfNumber == 1){
- return -MaxDate;
- }else{
- return 1-monthOfNumber;
- }
- }
-
- //获得上月最后一天的日期
- public String getPreviousMonthEnd(){
- String str = "";
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
-
- Calendar lastDate = Calendar.getInstance();
- lastDate.add(Calendar.MONTH,-1);//减一个月
- lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天
- lastDate.roll(Calendar.DATE, -1);//日期回滚一天,也就是本月最后一天
- str=sdf.format(lastDate.getTime());
- return str;
- }
-
- //获得下个月第一天的日期
- public String getNextMonthFirst(){
- String str = "";
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
-
- Calendar lastDate = Calendar.getInstance();
- lastDate.add(Calendar.MONTH,1);//减一个月
- lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天
- str=sdf.format(lastDate.getTime());
- return str;
- }
-
- //获得下个月最后一天的日期
- public String getNextMonthEnd(){
- String str = "";
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
-
- Calendar lastDate = Calendar.getInstance();
- lastDate.add(Calendar.MONTH,1);//加一个月
- lastDate.set(Calendar.DATE, 1);//把日期设置为当月第一天
- lastDate.roll(Calendar.DATE, -1);//日期回滚一天,也就是本月最后一天
- str=sdf.format(lastDate.getTime());
- return str;
- }
-
- //获得明年最后一天的日期
- public String getNextYearEnd(){
- String str = "";
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
-
- Calendar lastDate = Calendar.getInstance();
- lastDate.add(Calendar.YEAR,1);//加一个年
- lastDate.set(Calendar.DAY_OF_YEAR, 1);
- lastDate.roll(Calendar.DAY_OF_YEAR, -1);
- str=sdf.format(lastDate.getTime());
- return str;
- }
-
- //获得明年第一天的日期
- public String getNextYearFirst(){
- String str = "";
- SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
-
- Calendar lastDate = Calendar.getInstance();
- lastDate.add(Calendar.YEAR,1);//加一个年
- lastDate.set(Calendar.DAY_OF_YEAR, 1);
- str=sdf.format(lastDate.getTime());
- return str;
-
- }
-
- //获得本年有多少天
- private int getMaxYear(){
- Calendar cd = Calendar.getInstance();
- cd.set(Calendar.DAY_OF_YEAR,1);//把日期设为当年第一天
- cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滚一天。
- int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
- return MaxYear;
- }
-
- private int getYearPlus(){
- Calendar cd = Calendar.getInstance();
- int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);//获得当天是一年中的第几天
- cd.set(Calendar.DAY_OF_YEAR,1);//把日期设为当年第一天
- cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滚一天。
- int MaxYear = cd.get(Calendar.DAY_OF_YEAR);
- if(yearOfNumber == 1){
- return -MaxYear;
- }else{
- return 1-yearOfNumber;
- }
- }
- //获得本年第一天的日期
- public String getCurrentYearFirst(){
- int yearPlus = this.getYearPlus();
- GregorianCalendar currentDate = new GregorianCalendar();
- currentDate.add(GregorianCalendar.DATE,yearPlus);
- Date yearDay = currentDate.getTime();
- DateFormat df = DateFormat.getDateInstance();
- String preYearDay = df.format(yearDay);
- return preYearDay;
- }
-
-
- //获得本年最后一天的日期 *
- public String getCurrentYearEnd(){
- Date date = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式
- String years = dateFormat.format(date);
- return years+"-12-31";
- }
-
-
- //获得上年第一天的日期 *
- public String getPreviousYearFirst(){
- Date date = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式
- String years = dateFormat.format(date); int years_value = Integer.parseInt(years);
- years_value--;
- return years_value+"-1-1";
- }
-
- //获得上年最后一天的日期
- public String getPreviousYearEnd(){
- weeks--;
- int yearPlus = this.getYearPlus();
- GregorianCalendar currentDate = new GregorianCalendar();
- currentDate.add(GregorianCalendar.DATE,yearPlus+MaxYear*weeks+(MaxYear-1));
- Date yearDay = currentDate.getTime();
- DateFormat df = DateFormat.getDateInstance();
- String preYearDay = df.format(yearDay);
- getThisSeasonTime(11);
- return preYearDay;
- }
-
- //获得本季度
- public String getThisSeasonTime(int month){
- int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
- int season = 1;
- if(month>=1&&month<=3){
- season = 1;
- }
- if(month>=4&&month<=6){
- season = 2;
- }
- if(month>=7&&month<=9){
- season = 3;
- }
- if(month>=10&&month<=12){
- season = 4;
- }
- int start_month = array[season-1][0];
- int end_month = array[season-1][2];
-
- Date date = new Date();
- SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");//可以方便地修改日期格式
- String years = dateFormat.format(date);
- int years_value = Integer.parseInt(years);
-
- int start_days =1;//years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);
- int end_days = getLastDayOfMonth(years_value,end_month);
- String seasonDate = years_value+"-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days;
- return seasonDate;
-
- }
-
- /**
- * 获取某年某月的最后一天
- * @param year 年
- * @param month 月
- * @return 最后一天
- */
- private int getLastDayOfMonth(int year, int month) {
- if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
- || month == 10 || month == 12) {
- return 31;
- }
- if (month == 4 || month == 6 || month == 9 || month == 11) {
- return 30;
- }
- if (month == 2) {
- if (isLeapYear(year)) {
- return 29;
- } else {
- return 28;
- }
- }
- return 0;
- }
- /**
- * 是否闰年
- * @param year 年
- * @return
- */
- public boolean isLeapYear(int year) {
- return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- }
- }
-
相关阅读:
Java基础之抽象类和多态
Java基础之继承重载重写与this和super
Java基础之StringBuffer的使用
Java基础之String的方法与常量池
Spring Boot之JSP开发
Spring Boot之thymeleaf中替换th:each使用
本周进度
问卷调查立题报告(三人行)
本周进度(复习软考)
软件需求十步走之阅读笔记02
-
原文地址:https://www.cnblogs.com/danghuijian/p/4400771.html
Copyright © 2020-2023
润新知