Java时间类(转)
- package com.chinagas.common.utils;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- public class DateUtils {
- public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
- public static final String MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
- public static final String HOUR_PATTERN = "yyyy-MM-dd HH:mm:ss";
- public static final String DATE_PATTERN = "yyyy-MM-dd";
- public static final String MONTH_PATTERN = "yyyy-MM";
- public static final String YEAR_PATTERN = "yyyy";
- public static final String MINUTE_ONLY_PATTERN = "mm";
- public static final String HOUR_ONLY_PATTERN = "HH";
- /**
- * 日期相加减天数
- * @param date 如果为Null,则为当前时间
- * @param days 加减天数
- * @param includeTime 是否包括时分秒,true表示包含
- * @return
- * @throws ParseException
- */
- public static Date dateAdd(Date date, int days, boolean includeTime) throws ParseException{
- if(date == null){
- date = new Date();
- }
- if(!includeTime){
- SimpleDateFormat sdf = new SimpleDateFormat(DateUtils.DATE_PATTERN);
- date = sdf.parse(sdf.format(date));
- }
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- cal.add(Calendar.DATE, days);
- return cal.getTime();
- }
- /**
- * 时间格式化成字符串
- * @param date Date
- * @param pattern StrUtils.DATE_TIME_PATTERN || StrUtils.DATE_PATTERN, 如果为空,则为yyyy-MM-dd
- * @return
- * @throws ParseException
- */
- public static String dateFormat(Date date, String pattern) throws ParseException{
- if(StrUtils.isBlank(pattern)){
- pattern = DateUtils.DATE_PATTERN;
- }
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
- return sdf.format(date);
- }
- /**
- * 字符串解析成时间对象
- * @param dateTimeString String
- * @param pattern StrUtils.DATE_TIME_PATTERN || StrUtils.DATE_PATTERN,如果为空,则为yyyy-MM-dd
- * @return
- * @throws ParseException
- */
- public static Date dateParse(String dateTimeString, String pattern) throws ParseException{
- if(StrUtils.isBlank(pattern)){
- pattern = DateUtils.DATE_PATTERN;
- }
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
- return sdf.parse(dateTimeString);
- }
- /**
- * 将日期时间格式成只有日期的字符串(可以直接使用dateFormat,Pattern为Null进行格式化)
- * @param dateTime Date
- * @return
- * @throws ParseException
- */
- public static String dateTimeToDateString(Date dateTime) throws ParseException{
- String dateTimeString = DateUtils.dateFormat(dateTime, DateUtils.DATE_TIME_PATTERN);
- return dateTimeString.substring(0, 10);
- }
- /**
- * 当时、分、秒为00:00:00时,将日期时间格式成只有日期的字符串,
- * 当时、分、秒不为00:00:00时,直接返回
- * @param dateTime Date
- * @return
- * @throws ParseException
- */
- public static String dateTimeToDateStringIfTimeEndZero(Date dateTime) throws ParseException{
- String dateTimeString = DateUtils.dateFormat(dateTime, DateUtils.DATE_TIME_PATTERN);
- if(dateTimeString.endsWith("00:00:00")){
- return dateTimeString.substring(0, 10);
- }else{
- return dateTimeString;
- }
- }
- /**
- * 将日期时间格式成日期对象,和dateParse互用
- * @param dateTime Date
- * @return Date
- * @throws ParseException
- */
- public static Date dateTimeToDate(Date dateTime) throws ParseException{
- Calendar cal = Calendar.getInstance();
- cal.setTime(dateTime);
- 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();
- }
- /**
- * 时间加减小时
- * @param startDate 要处理的时间,Null则为当前时间
- * @param hours 加减的小时
- * @return Date
- */
- public static Date dateAddHours(Date startDate, int hours) {
- if (startDate == null) {
- startDate = new Date();
- }
- Calendar c = Calendar.getInstance();
- c.setTime(startDate);
- c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours);
- return c.getTime();
- }
- /**
- * 时间加减分钟
- * @param startDate 要处理的时间,Null则为当前时间
- * @param minutes 加减的分钟
- * @return
- */
- public static Date dateAddMinutes(Date startDate, int minutes) {
- if (startDate == null) {
- startDate = new Date();
- }
- Calendar c = Calendar.getInstance();
- c.setTime(startDate);
- c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
- return c.getTime();
- }
- /**
- * 时间加减秒数
- * @param startDate 要处理的时间,Null则为当前时间
- * @param minutes 加减的秒数
- * @return
- */
- public static Date dateAddSeconds(Date startDate, int seconds) {
- if (startDate == null) {
- startDate = new Date();
- }
- Calendar c = Calendar.getInstance();
- c.setTime(startDate);
- c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds);
- return c.getTime();
- }
- /**
- * 时间加减天数
- * @param startDate 要处理的时间,Null则为当前时间
- * @param days 加减的天数
- * @return Date
- */
- public static Date dateAddDays(Date startDate, int days) {
- if (startDate == null) {
- startDate = new Date();
- }
- Calendar c = Calendar.getInstance();
- c.setTime(startDate);
- c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
- return c.getTime();
- }
- /**
- * 时间加减月数
- * @param startDate 要处理的时间,Null则为当前时间
- * @param months 加减的月数
- * @return Date
- */
- public static Date dateAddMonths(Date startDate, int months) {
- if (startDate == null) {
- startDate = new Date();
- }
- Calendar c = Calendar.getInstance();
- c.setTime(startDate);
- c.set(Calendar.MONTH, c.get(Calendar.MONTH) + months);
- return c.getTime();
- }
- /**
- * 时间加减年数
- * @param startDate 要处理的时间,Null则为当前时间
- * @param years 加减的年数
- * @return Date
- */
- public static Date dateAddYears(Date startDate, int years) {
- if (startDate == null) {
- startDate = new Date();
- }
- Calendar c = Calendar.getInstance();
- c.setTime(startDate);
- c.set(Calendar.YEAR, c.get(Calendar.YEAR) + years);
- return c.getTime();
- }
- /**
- * 时间比较(如果myDate>compareDate返回1,<返回-1,相等返回0)
- * @param myDate 时间
- * @param compareDate 要比较的时间
- * @return int
- */
- public static int dateCompare(Date myDate, Date compareDate) {
- Calendar myCal = Calendar.getInstance();
- Calendar compareCal = Calendar.getInstance();
- myCal.setTime(myDate);
- compareCal.setTime(compareDate);
- return myCal.compareTo(compareCal);
- }
- /**
- * 获取两个时间中最小的一个时间
- * @param date
- * @param compareDate
- * @return
- */
- public static Date dateMin(Date date, Date compareDate) {
- if(date == null){
- return compareDate;
- }
- if(compareDate == null){
- return date;
- }
- if(1 == dateCompare(date, compareDate)){
- return compareDate;
- }else if(-1 == dateCompare(date, compareDate)){
- return date;
- }
- return date;
- }
- /**
- * 获取两个时间中最大的一个时间
- * @param date
- * @param compareDate
- * @return
- */
- public static Date dateMax(Date date, Date compareDate) {
- if(date == null){
- return compareDate;
- }
- if(compareDate == null){
- return date;
- }
- if(1 == dateCompare(date, compareDate)){
- return date;
- }else if(-1 == dateCompare(date, compareDate)){
- return compareDate;
- }
- return date;
- }
- /**
- * 获取两个日期(不含时分秒)相差的天数,不包含今天
- * @param startDate
- * @param endDate
- * @return
- * @throws ParseException
- */
- public static int dateBetween(Date startDate, Date endDate) throws ParseException {
- Date dateStart = dateParse(dateFormat(startDate, DATE_PATTERN), DATE_PATTERN);
- Date dateEnd = dateParse(dateFormat(endDate, DATE_PATTERN), DATE_PATTERN);
- return (int) ((dateEnd.getTime() - dateStart.getTime())/1000/60/60/24);
- }
- /**
- * 获取两个日期(不含时分秒)相差的天数,包含今天
- * @param startDate
- * @param endDate
- * @return
- * @throws ParseException
- */
- public static int dateBetweenIncludeToday(Date startDate, Date endDate) throws ParseException {
- return dateBetween(startDate, endDate) + 1;
- }
- /**
- * 获取日期时间的年份,如2017-02-13,返回2017
- * @param date
- * @return
- */
- public static int getYear(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- return cal.get(Calendar.YEAR);
- }
- /**
- * 获取日期时间的月份,如2017年2月13日,返回2
- * @param date
- * @return
- */
- public static int getMonth(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- return cal.get(Calendar.MONTH) + 1;
- }
- /**
- * 获取日期时间的第几天(即返回日期的dd),如2017-02-13,返回13
- * @param date
- * @return
- */
- public static int getDate(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- return cal.get(Calendar.DATE);
- }
- /**
- * 获取日期时间当月的总天数,如2017-02-13,返回28
- * @param date
- * @return
- */
- public static int getDaysOfMonth(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- return cal.getActualMaximum(Calendar.DATE);
- }
- /**
- * 获取日期时间当年的总天数,如2017-02-13,返回2017年的总天数
- * @param date
- * @return
- */
- public static int getDaysOfYear(Date date) {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
- }
- /**
- * 根据时间获取当月最大的日期
- * <li>2017-02-13,返回2017-02-28</li>
- * <li>2016-02-13,返回2016-02-29</li>
- * <li>2016-01-11,返回2016-01-31</li>
- * @param date Date
- * @return
- * @throws Exception
- */
- public static Date maxDateOfMonth(Date date) throws Exception {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int value = cal.getActualMaximum(Calendar.DATE);
- return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null);
- }
- /**
- * 根据时间获取当月最小的日期,也就是返回当月的1号日期对象
- * @param date Date
- * @return
- * @throws Exception
- */
- public static Date minDateOfMonth(Date date) throws Exception {
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- int value = cal.getActualMinimum(Calendar.DATE);
- return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null);
- }
- public static void main(String[] args) throws Exception {
- /*System.out.println(dateTimeToDate(new Date()));
- System.out.println(dateParse("2017-02-04 14:58:20", null));
- System.out.println(dateTimeToDateStringIfTimeEndZero(new Date()));
- System.out.println(dateTimeToDateStringIfTimeEndZero(dateTimeToDate(new Date())));*/
- //System.out.println(dateBetween(dateParse("2017-01-30", null), dateParse("2017-02-01", null)));
- //System.out.println(dateBetweenIncludeToday(dateParse("2017-01-30", null), dateParse("2017-02-01", null)));
- System.out.println(getDate(dateParse("2017-01-17", null)));
- /*
- System.out.println(getDaysOfMonth(dateParse("2017-02-01", null)));
- System.out.println(getDaysOfYear(dateParse("2017-01-30", null)));*/
- //System.out.println(dateFormat(dateAddMonths(dateParse("2017-02-07", StrUtils.MONTH_PATTERN), -12), StrUtils.MONTH_PATTERN));
- /*System.out.println(dateFormat(maxDateOfMonth(dateParse("2016-02", "yyyy-MM")), null));
- System.out.println(dateFormat(minDateOfMonth(dateParse("2016-03-31", null)), null));*/
- }
- }
- 原文链接https://www.cnblogs.com/fanshuyao/p/6365099.html
Java时间类(转)的更多相关文章
- java时间类简单总结
java时间类(Data类) 1.Data类(没有考虑到国际化,好多方法已过时java.util.Data包中) 父类(是类不是接口含有直接子类3个): 日期格式为:年月日时分秒(不包含毫秒部分) ...
- Java时间类从此变得清晰明了
Java时间类 Java时间类分为Date 日期类和Calendar 日历类,相信很多小伙伴在初学时会对这个两个类的用法.区别以及有什么联系会感到疑惑,似乎懂了,但又不能具体说清,今天再带你来清晰的再 ...
- java时间类Date、Calendar及用法
对于时间类,这篇主要说明各种现实情况下如何取值,怎么定向取值,得到自己想要的时间参数.在java中时间类主要有Date.Calendar,暂时只介绍 java.util.*下的时间类,对于java.s ...
- Java时间类总结
java.util.Date 包含有年月日时分秒,精确到毫秒级别. 官方解释: // The class Date represents a specific instant in time, wit ...
- Java 时间类
1.System 类 2.Date 类 3.SimpleDateFormate 类 4.Calendar 类 1.System 类 得到当前的时间值.System 类不能被实例化,需要通过它的静态方法 ...
- Java 时间类-Calendar、Date、LocalDate/LocalTime
1.Date 类 java.util.Date是一个"万能接口",它包含日期.时间,还有毫秒数,如果你只想用java.util.Date存储日期,或者只存储时间,那么,只有你知道哪 ...
- Java 时间类 Date 和 Calendar
在项目中获取一个yyyy-MM-dd HH:mm:ss格式的时间字符串 package org.htsg.kits; import java.text.SimpleDateFormat; import ...
- Java中常见时间类的使用
模拟场景针对于常用的操作API,比如流操作(字符流.字节流),时间操作等,仅仅了解概念性的定义终究是无法了解该类的用途和使用方式:这种情况在使用的时候便一脸茫然,脑海中映射不到对应的知识点.本篇博客将 ...
- Java Calendar类使用总结
平时在浏览一些网站时,有些网站会显示出当前时间,如现在是xx年xx月xx日 xx时xx分xx秒,在实际的开发过程中,也会涉及到日期和时间的计算,Java中提供了一个专门的类Calendar来处理日期与 ...
随机推荐
- error C2065: 'IDD_DIALOG1' : undeclared identifier
添加资源文件 #include "resource.h"
- asp.net DataReader DataTable 使用反射给给实体赋值
asp.net 使用反射给给实体赋值 实体类继承此基类 using System.Reflection; using System.Data.SqlClient; using System.Data; ...
- ActiveMQ实战之 Queue点对点消息
前言:ActiveMQ消息模式点对点编码 运行:先运行消费者在开启消息生产者即可接收到消息 消息生产者 /** * @摘要 测试发送单条数据的类 */ public class ZMQOneSendT ...
- Struts2--HelloWord
Struts2官网 官网指南 官网下载Struts2.5-min-lib.zip解压把lib目录下jar包拷贝到web项目lib目录下 配置web.xml文件 <filter> <f ...
- Ubuntu Server 命令行下的默认语言改为英语en_US.UTF-8
源文链接:http://tonychiu.blog.51cto.com/656605/393131 如果Ubuntu Server在安装过程中,选择的是中文(很多新手都会在安装时选择中文,便于上手), ...
- XCode中常用错误解决
No such file or directory 解决方法(可以依次尝试,总有一种能最终解决问题): 方法1.退出Xcode,然后从finder里面进入~/Library/ ...
- stm32的gpio函数介绍
一.gpio_init函数 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) 调用时的格式一般是例如 RCC ...
- (spfa) Highway Project (zoj 3946 )
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 Highway Project Time Limit: 2 Seco ...
- noip第28课作业
分段数列 [问题描述] 对于给定的一个长度为N的正整数数列A[i],现要将其分成连续的若干段,并且每段和不超过M(可以等于M),问最少能将其分成多少段使得满足要求. 输入格式: 输入第1行包含两个正整 ...
- W-TinyLFU——设计一个现代的缓存
缓存设计是个基础架构领域里的重要话题,本号之前也有谈论过相关话题,点击原文可以看之前的介绍. 近日,HighScalability网站刊登了一篇文章,由前Google工程师发明的W-TinyLFU—— ...