zoukankan      html  css  js  c++  java
  • JAVA获取某年(当年)的第一天的开始时刻和某年(当年)的最后一天的最后时刻

    package com.date;
    
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class Test {
    
    
        public static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        public static SimpleDateFormat format1 = new SimpleDateFormat(
                "yyyyMMdd HH:mm:ss");
    
        /**
         * 得到指定日期的一天的的最后时刻23:59:59
         *
         * @param date
         * @return
         */
        public static Date getFinallyDate(Date date) {
            String temp = format.format(date);
            temp += " 23:59:59";
    
            try {
                return format1.parse(temp);
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 得到指定日期的一天的开始时刻00:00:00
         *
         * @param date
         * @return
         */
        public static Date getStartDate(Date date) {
            String temp = format.format(date);
            temp += " 00:00:00";
    
            try {
                return format1.parse(temp);
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 获取某年第一天日期开始时刻
         * @param year 年份
         * @return Date
         */
        public static Date getYearFirstDay(int year){
            Calendar cal = Calendar.getInstance();
            cal.clear();
            cal.set(Calendar.YEAR, year);
            Date yearFirstDay = cal.getTime();
            return getStartDate(yearFirstDay);
        }
    
        /**
         * 获取某年最后一天日期最后时刻
         * @param year 年份
         * @return Date
         */
        public static Date getYearLastDay(int year){
            Calendar cal = Calendar.getInstance();
            cal.clear();
            cal.set(Calendar.YEAR, year);
            cal.roll(Calendar.DAY_OF_YEAR, -1);
            Date yearLastDay = cal.getTime();
            return getFinallyDate(yearLastDay);
        }
    
        public static void main(String[] args) {
            Calendar cal = Calendar.getInstance();
            //获取当前年份
            int year = cal.get(Calendar.YEAR);
            System.out.println(year);
    
            System.out.println(getYearFirstDay(year));
            System.out.println(getYearLastDay(year));
    
        }
    }
    -----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------ (蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
  • 相关阅读:
    struts.xml,报错 1 c.opensymphony.xwork2.util.DomHelper
    poi 导入Excle
    Oracle update语句更新值来自另一张表中的数据
    Oracle 《积累章》 根据身份证号码更新当前出生日期
    java 反射得到属性与属性值
    spring mvc 简单的文件上传与下载
    java扫描文件。
    类加载机制
    容器工厂(原型&单例)
    容器工厂(原型)
  • 原文地址:https://www.cnblogs.com/pxblog/p/13946887.html
Copyright © 2011-2022 走看看