Java 项目常用的日期方法class

package com.school.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 提供一些跟日期相关的通用方法。
 * @author nimade
 */
public abstract class SchoolDateUtils {

    /**
     * “今日”的开始时间。
     */
    public static Date getTodayStart() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND));

        return cal.getTime();
    }

    /**
     * “今日”的结束时间。
     */
    public static Date getTodayEnd() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

        return cal.getTime();
    }

    /**
     * “昨日”的开始时间。
     */
    public static Date getYesterdayStart() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND));

        // 昨日
        cal.add(Calendar.DAY_OF_MONTH, -1);

        return cal.getTime();
    }

    /**
     * “昨日”的结束时间。
     */
    public static Date getYesterdayEnd() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

        // 昨日
        cal.add(Calendar.DAY_OF_MONTH, -1);

        return cal.getTime();
    }

    /**
     * 最近几天前的时间。
     */
    public static Date getRecentDateStart(Integer day) {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

        // 以前的时间
        cal.add(Calendar.DAY_OF_MONTH, -day);//由当前时间,推算出几天前的时间

        return cal.getTime();
    }

    /**
     * 未來几天前的时间。
     */
    public static Date getFutureDateStart(Integer day) {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

        // 以前的时间
        cal.add(Calendar.DAY_OF_MONTH, +day);//由当前时间,推算出將來的时间

        return cal.getTime();
    }

    /**
     * “前天”的开始时间。
     */
    public static Date getQianTianStart() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND));

        // 前天
        cal.add(Calendar.DAY_OF_MONTH, -2);

        return cal.getTime();
    }

    /**
     * “前天”的结束时间。
     */
    public static Date getQianTianEnd() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH));

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

        // 前天
        cal.add(Calendar.DAY_OF_MONTH, -2);

        return cal.getTime();
    }

    /**
     * “本周”的开始时间。
     */
    public static Date getThisWeekStart() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); // 周日

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND));

        return cal.getTime();
    }

    /**
     * “本周”的结束时间。
     */
    public static Date getThisWeekEnd() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); // 周六

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

        return cal.getTime();
    }

    /**
     * “上周”的开始时间。
     */
    public static Date getLastWeekStart() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); // 周日

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND));

        // 上周
        cal.add(Calendar.DATE, -7);

        return cal.getTime();
    }

    /**
     * “上周”的结束时间。
     */
    public static Date getLastWeekEnd() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY); // 周六

        cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

        // 上周
        cal.add(Calendar.DATE, -7);

        return cal.getTime();
    }

    /**
     * “本月”的开始时间。
     */
    public static Date getThisMonthStart() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));

        cal.set(Calendar.DAY_OF_MONTH, now.getActualMinimum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND));

        return cal.getTime();
    }

    /**
     * “本月”的结束时间。
     */
    public static Date getThisMonthEnd() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));

        cal.set(Calendar.DAY_OF_MONTH, now.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

        return cal.getTime();
    }

    /**
     * “上月”的开始时间。
     */
    public static Date getLastMonthStart() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));

        cal.set(Calendar.DAY_OF_MONTH, now.getActualMinimum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND));

        // 上月
        cal.add(Calendar.MONTH, -1);

        return cal.getTime();
    }

    /**
     * “上月”的结束时间。
     */
    public static Date getLastMonthEnd() {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, now.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, now.get(Calendar.MONTH));

        cal.set(Calendar.DAY_OF_MONTH, now.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

        // 上月
        cal.add(Calendar.MONTH, -1);

        return cal.getTime();
    }

    /****************根据传进来的数字,获取某一年,某一个月的开始日期,与结束日期*********************/

    /**
     * “根据传进来的数字”的开始时间。
     */
    public static Date getNumMonthStart(Integer year,Integer month) {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();
        java.util.Date date;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(year.toString()+"-"+month.toString()+"-"+"01");
            now.setTime(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  

        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month-1);

        cal.set(Calendar.DAY_OF_MONTH, now.getActualMinimum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMinimum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMinimum(Calendar.MILLISECOND));

        return cal.getTime();
    }

    /**
     * "根据传进来的数字"的结束时间。
     */
    public static Date getNumMonthEnd(Integer year,Integer month) {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();

        java.util.Date date;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(year.toString()+"-"+month.toString()+"-"+"01");
            now.setTime(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  

        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month-1);

        cal.set(Calendar.DAY_OF_MONTH, now.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, now.getActualMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

        return cal.getTime();
    }

    /***
     * 获取指定月份的所用日期
     */
    /**
     * "根据传进来的数字"的结束时间。
     */
    public static List<String> getNumMonthAll(Integer year,Integer month) {
        Calendar now = Calendar.getInstance();
        Calendar cal = Calendar.getInstance();

        List<String> monthOfDayList=new ArrayList<String>();

        java.util.Date date;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(year.toString()+"-"+month.toString()+"-"+"01");
            now.setTime(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  

        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month-1);

        cal.set(Calendar.DAY_OF_MONTH, now.getActualMaximum(Calendar.DAY_OF_MONTH));

        int day=now.getActualMaximum(Calendar.DAY_OF_MONTH);//获取指定月份的最大天数

        for(int i=1;i<=day;i++){
            cal.set(Calendar.DAY_OF_MONTH, i);
            cal.set(Calendar.HOUR_OF_DAY, now.getActualMinimum(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, now.getActualMaximum(Calendar.MINUTE));
            cal.set(Calendar.SECOND, now.getActualMaximum(Calendar.SECOND));
            cal.set(Calendar.MILLISECOND, now.getActualMaximum(Calendar.MILLISECOND));

            Date dateC = cal.getTime();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            monthOfDayList.add(sdf.format(dateC));
        }

        return monthOfDayList;
    }
}
时间: 2024-10-30 10:10:42

Java 项目常用的日期方法class的相关文章

【JAVA】 Java 项目常用故障排查

JAVA 项目常用故障排查 常用命令 1.pidstat 查看进程的 cpu.memory.disk 使用情况,已定时轮询的方式 # 安装 $ yum install sysstat $ apt-get install sysstat # 每一秒钟采样一次 一共采样3次 采样进程为13084 # -p 指定进程号 # memory 情况 $ pidstat -r -p 13084 1 3 # cpu 情况 $ pidstat -u -p 13787 1 3 # disk 情况 $ pidstat

JAVA项目常用的异常处理情况总结

周一老师讲解了JAVA中的异常处理机制,我们在进行JAVA项目开发时,经常会面对一些常见的异常处理情况,接下来我会根据课下查阅学习的资料,来进行一些总结. 参考资料:https://www.cnblogs.com/Aming-/p/11772796.html 1.空指针异常(java.lang.nullpointerexception) 发生该情况一般是字符串变量未初始化,数组未初始化,类对象未初始化等.还有一种情况是当该对象为空时你并没有判断是否为空值,这个错误我在之前的web习题上犯过,因此

Java 最常用类(前100名)来自一万个开源项目

大部分的 Java 软件开发都会使用到各种不同的库.近日我们从一万个开源的 Java 项目中进行分析,从中提取出最常用的 Java 类,这些类有来自于 Java 的标准库,也有第三方库.每个类在同一个项目中只计数一次,下面的列表中显示前 100 名的类. 以下列表中按使用最多的类进行排序,第三个数值表示 10000 个项目里使用到该类的项目数,而完整的列表请看 here. java.util.ArrayList(6958) java.io.IOException(6866) java.util.

maven3常用命令、java项目搭建、web项目搭建

------------------------------maven3常用命令--------------------------- 1.常用命令 1)创建一个Project mvn archetype:generate -DarchetypeCatalog=internal -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -Dinteractive

JAVA项目中的常用的异常处理情况

在网上查阅了相关资料得出了以下JAVA项目中的常用的异常处理情况总结: 1.不要捕获 Java 类库中定义的继承自 RuntimeException 的运行时异常类,如:IndexOutOfBoundsException / NullPointerException,这类异常由程序员预检查违法来规避,保证程序健壮性. 2.异常不要用来做流程控制,条件控制,因为异常的处理效率比条件分支低.这个坑大家要注意了. 3.对大段代码进行 try-catch,这是不负责任的表现. catch 时请分清稳定代

JAVA项目中的常用的异常处理情况1

Java项目中常用的异常处理情况 1.java.lang.NullPointerException(空指针异常) 调用了未经初始化的对象或者是不存在的对象.经常出现在创建图片,调用数组这些操作中,比如图片未经初始化,或者图片创建时的路径错误等等.对数组操作中出现空指针, 即把数组的初始化和数组元素的初始化混淆起来了.数组的初始化是对数组分配需要的空间,而初始化后的数组,其中的元素并没有实例化, 依然是空的,所以还需要对每个元素都进行初始化(如果要调用的话). 2. java.lang.Class

java项目中可能会使用到的jar包解释

一.Struts2 用的版本是struts2.3.1.1 一个简单的Struts项目所需的jar包有如下8个 1. struts2-core-2.3.1.1.jar: Struts2的核心类库. 2. xwork-core-2.3.1.1.jar: XWork核心类,XWork是一个标准的command模式实现,并且完全从web层剥离出来.WebWork被构建在Xwork上,而Struts2由Struts1和WebWork两个经典的MVC框架发展而来. 3. ognl-3.0.3.jar: 支持

java 项目--安全框架--Shiro

本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinnianshilongnian.iteye.com/blog/2018936 我并没有全部看完,只是选择了一部分对我来说急需在项目中使用的知识加以学习.并且对于大多数第一次接触Shiro的同学来说,掌握这些也应该足够了. 一.架构 要学习如何使用Shiro必须先从它的架构谈起,作为一款安全框架Shiro的设计相当精妙.Shiro的应用不依赖任何容器,它也可以在JavaSE下使用.但是最常用的环境还是JavaEE.下面以

用Ant实现Java项目的自动构建和部署

Ant是一个Apache基金会下的跨平台的构件工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将Ant应用到Java项目中,让它简化构建和部署操作. 一.             安装与配置 下载地址:http://ant.apache.org/,在本文中下载的是1.7.0版本.解压到某个目录(例如E:\apache-ant-1.7.0),即可使用. 添加系统环境变量:ANT_HOME,该变量指向Ant解压后的根目录,在此为E:\apache-ant-1.7.0. 安装与