• 时间转换~


    
         package com.cloud.assessment.until;
         import org.springframework.stereotype.Component;
         import java.text.DateFormat;
         import java.text.ParseException;
         import java.text.SimpleDateFormat;
         import java.time.LocalDate;
         import java.util.Calendar;
         import java.util.Date;
    
           /**
               * @Author: jj
             * @Date: 2019/1/17 2:37 PM
             */
            @Component
           public class TimeUntil {
           /**
            * 获取昨天开始的时间
            *
            * @return
           */
           public Date getYesterDayTime() {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            calendar.set(Calendar.HOUR_OF_DAY, -24);
            return calendar.getTime();
           }
    
        /**
         * 获取明天开始时间
         *
         * @return
         */
        public Date getTomorrowStartTime() {
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());
            cal.add(Calendar.DATE, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);//这里面要选Calendar.HOUR_OF_DAY,不然会有问题 
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            return cal.getTime();
        }
    
        /**
         * 时间转换格式
         *
         * @return
         */
        public Date getDayStringTime(String time) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                return format.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
    
        }
    
        /**
         * 时间转换格式
         *
         * @return
         */
        public Date getDayStringTime2(String time) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            try {
                return format.parse(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
    
        }
    
        /**
         * 今天最后一秒
         *
         * @return
         */
        public Date getDayEndTime(Date date) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date d = null;
            if (date != null) {
                d = date;
            } else {
                d = new Date();
            }
            String str = format.format(d);
            str = str + " 23:59:59";
            return getDayStringTime(str);
        }
    
        /**
         * 今天开始时间
         *
         * @return
         */
        public Date getDayStartTime() {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date d = new Date();
            String str = format.format(d);
            str = str + " 00:00:00";
            return getDayStringTime(str);
        }
    
        /**
         * 是否是周一
         *
         * @return
         */
        public boolean dayIsMONDAY() {
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            if (calendar.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
                return Boolean.TRUE;
            }
            return Boolean.FALSE;
        }
    
        /**
         * 是否是月初第一天
         *
         * @return
         */
        public boolean dayIsMonthFirst() {
            Calendar calendar = Calendar.getInstance();
            Date date = new Date();
            calendar.setTime(date);
            int day = calendar.get(Calendar.DATE);
            if (day == 1) {
                return Boolean.TRUE;
            }
            return Boolean.FALSE;
        }
    
        /**
         * 是否是季度第一天
         *
         * @return
         */
        public boolean dayIsQuarterFirst() {
            Calendar calendar = Calendar.getInstance();
            Date date = new Date();
            calendar.setTime(date);
            int month = calendar.get(Calendar.MONTH) + 1;//获取月份
            int day = calendar.get(Calendar.DATE);
            if (day == 1) {
                if (month == 1 || month == 4 || month == 7 || month == 10) {
    
                    return Boolean.TRUE;
                }
    
            }
            return Boolean.FALSE;
        }
    
        /**
         * 是否是年的第一天
         *
         * @return
         */
        public boolean dayIsYearFirst() {
            Calendar calendar = Calendar.getInstance();
            Date date = new Date();
            calendar.setTime(date);
            int month = calendar.get(Calendar.MONTH) + 1;//获取月份
            int day = calendar.get(Calendar.DATE);
            if (day == 1) {
                if (month == 1) {
                    return Boolean.TRUE;
                }
            }
    
            return Boolean.FALSE;
        }
    
    
        /**
         * 获取这周第一天
         *
         * @return
         */
        public Date getWeekStartTime(Date date) {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }
            if (cal.get(Calendar.DAY_OF_WEEK) == 1) {//周末
                cal.add(Calendar.DATE, -6);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);
                System.out.println(cal.getTime());
            } else {
                cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);
                System.out.println(cal.getTime());
            }
            return cal.getTime();
        }
    
        /**
         * 获取这周最后一天
         *
         * @return
         */
        public Date getWeekEndTime(Date date) {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }
            //1表示周末!!
            if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
                return getDayEndTime(date);
            } else {
                cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                cal.add(Calendar.DATE, 7);
                cal.set(Calendar.HOUR_OF_DAY, 23);
                cal.set(Calendar.MINUTE, 59);
                cal.set(Calendar.SECOND, 59);
                cal.set(Calendar.MILLISECOND, 0);
            }
            return cal.getTime();
        }
    
    
        /**
         * 获取本月第一天
         *
         * @return
         */
        public Date getMonthStartTime(Date date) {
    
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            return cal.getTime();
        }
    
        /**
         * 获取本月最后一天
         *
         * @return
         */
        public Date getMonthEndTime(Date date) {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }
            cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 0);
            return cal.getTime();
        }
    
        /**
         * 获取本季度第一天
         *
         * @return
         */
        public Date getQuarterStartTime(Date date) {
    
            Calendar calendar = Calendar.getInstance();//日历对象
            if (date != null) {
                calendar.setTime(date);//设置当前日期
            }
            String endString = " 00:00:00";
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            int month = calendar.get(Calendar.MONTH) + 1;//获取月份
            String message = null;
            try {
                if (month < 4) {
                    message = calendar.get(Calendar.YEAR) + "-01-01" + endString;
                } else if (month > 3 && month < 7) {
                    message = calendar.get(Calendar.YEAR) + "-04-01" + endString;
    
                } else if (month > 7 && month < 10) {
                    message = calendar.get(Calendar.YEAR) + "-07-01" + endString;
    
                } else {
                    message = calendar.get(Calendar.YEAR) + "-10-01" + endString;
                }
                return format.parse(message);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        /**
         * 获取本季度最后一天
         *
         * @return
         */
        public Date getQuarterEndTime(Date date) {
            Calendar calendar = Calendar.getInstance();//日历对象
            if (date != null) {
                calendar.setTime(date);//设置当前日期
            }
            String endString = " 23:59:59";
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            int month = calendar.get(Calendar.MONTH) + 1;//获取月份
            String message = null;
            try {
                if (month < 4) {
                    message = calendar.get(Calendar.YEAR) + "-3-31" + endString;
                } else if (month > 3 && month < 7) {
                    message = calendar.get(Calendar.YEAR) + "-6-30" + endString;
    
                } else if (month > 7 && month < 10) {
                    message = calendar.get(Calendar.YEAR) + "-9-30" + endString;
    
                } else {
                    message = calendar.get(Calendar.YEAR) + "-12-30" + endString;
                }
                return format.parse(message);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return null;
    
        }
    
        /**
         * 获取本年度第一天
         *
         * @return
         */
        public Date getYearStartTime(Date date) {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }
            cal.set(Calendar.DAY_OF_YEAR, 1);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            return cal.getTime();
        }
    
        /**
         * 获取本年度最后一天
         *
         * @return
         */
        public Date getYearEndTime(Date date) {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }
            cal.set(Calendar.DAY_OF_YEAR, cal.getActualMaximum(Calendar.DAY_OF_YEAR));
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 0);
            return cal.getTime();
        }
    
        /**
         * 获取半年的最后一天
         *
         * @return
         */
        public Date getYearHalfEndTime(Date date) {
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;//获取月份
            String endTime = "";
            if (month <= 6) {
                endTime = year + "-06-30  23:59:59";
            } else {
                endTime = year + "-12-31  23:59:59";
            }
            return getDayStringTime(endTime);
        }
    
        /**
         * 获取半年的第一天
         *
         * @return
         */
        public Date getYearHalfStartTime(Date date) {
    
            Calendar cal = Calendar.getInstance();
            if (date != null) {
                cal.setTime(date);
            }
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH) + 1;//获取月份
            String endTime = "";
            if (month <= 6) {
                endTime = year + "-01-01  00:00:00";
            } else {
                endTime = year + "-07-01  00:00:00";
            }
            return getDayStringTime(endTime);
        }
    
    
    }
  • 相关阅读:
    jPoint开发简介
    使用.NET程序集作为Business Data Connectivity数据源(一)
    SharePoint2010内容类型剖析(一)
    SharePoint Learning Kit核心内容概述(四)
    SharePoint 2010正式版发布
    推荐一份极好的SharePoint 2010开发资料
    使用jQuery为数据视图添加图表显示
    SharePoint Designer 2010的新增功能
    SharePoint Learning Kit核心内容概述(三)
    SharePoint2010内容类型剖析(三)
  • 原文地址:https://www.cnblogs.com/jinjian91/p/10334824.html
Copyright © 2020-2023  润新知