时间工具类

 public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; /**
* 格式化后的日期字符串,默认使用 yyyy-MM-dd HH:mm:ss
* @param localDateTime
* @return formatDateToString
*/
public static String formatDateToString(LocalDateTime localDateTime){
return localDateTime.format(DateTimeFormatter.ofPattern(DEFAULT_DATETIME_FORMAT));
} /**
* 根据给定的格式把时间转换为字符串
* @param localDateTime 要格式化的日期
* @param format 转换格式
* @return formatDateToString
*/
public static String formatDateToString(LocalDateTime localDateTime , String format){
if (null != localDateTime){
return localDateTime.format(DateTimeFormatter.ofPattern(format));
} else {
return null;
}
} /**
* 字符转换为时间,默认使用yyyy-MM-dd HH:mm:ss格式
* @param date
* @return LocalDateTime
*/
public static LocalDateTime formatStringToDate(String date){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DEFAULT_DATETIME_FORMAT);
return LocalDateTime.parse(date,dateTimeFormatter);
} /**
* 字符转换为时间,默认使用yyyy-MM-dd HH:mm:ss格式
* @param data 日期字符串
* @param format 转换格式
* @return LocalDateTime
*/
public static LocalDateTime formatStringToDate(String data,String format){
if (null != data && ! data.equals("")){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return LocalDateTime.parse(data,dateTimeFormatter);
} else {
return null;
}
} /**
* 将LocalDateTime转为long类型的timestamp
* @param localDateTime
* @return long
*/
public static long getTimestampOfDateTime(LocalDateTime localDateTime){
ZoneId zoneId = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zoneId).toInstant();
return instant.toEpochMilli();
} /**
* 将long类型的timestamp转为LocalDateTime
* @param timestamp
* @return LocalDateTime
*/
public static LocalDateTime getDateTimeOfTimestamp(long timestamp){
Instant instant = Instant.ofEpochMilli(timestamp);
ZoneId zoneId = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant,zoneId);
} /**
* 判断一个日期是否在指定日期之后
* @param data 要比较的日期
* @param afterData 指定的日期
* @param format 格式
* @return boolean
*/
public static boolean isAfter(String data , String afterData , String format){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDateTime datas = LocalDateTime.parse(data,dateTimeFormatter);
LocalDateTime afterDa = LocalDateTime.parse(afterData , dateTimeFormatter);
return datas.isAfter(afterDa);
} /**
* 判断一个日期是否在指定日期之前
* @param data 要比较的日期
* @param before 指定的日期
* @param format 格式
* @return boolean
*/
public static boolean isBefore(String data,String before ,String format){
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDateTime datas = LocalDateTime.parse(data,dateTimeFormatter);
LocalDateTime afterDa = LocalDateTime.parse(before , dateTimeFormatter);
return datas.isBefore(afterDa);
} /**
* 判断是否为闰年
* @param data 日期 格式:yyyy-MM-dd
* @return boolean
*/
public static boolean isLeapYear(String data){
LocalDate yeat = LocalDate.parse(data);
return yeat.isLeapYear();
} /**
* 判断是否为闰年
* @param localDate 日期 格式:yyyy-MM-dd
* @return boolean
*/
public static boolean isLeapYear(LocalDate localDate){
return localDate.isLeapYear();
} /**
* 当前时间加减年
* @param years 年
* @return
*/
public static LocalDateTime plusYears(int years){
LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.plusYears(years);
} /**
* 当前时间加减月
* @param months 月份
* @return
*/
public static LocalDateTime plusMonths(int months){
LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.plusMonths(months);
} /**
* 当前时间加减日
* @param days 日
* @return
*/
public static LocalDateTime plusDays(int days){
LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.plusDays(days);
} /**
* 当前时间加减星期
* @param weeks 星期
* @return
*/
public static LocalDateTime plusWeeks(int weeks){
LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.plusWeeks(weeks);
} /**
* 当前时间加上几个小时,传入负数减去对应的时间
* @param hours 时间:单位小时
* @return LocalDateTime
*/
public static LocalDateTime plusHours(int hours){
LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.plusHours(hours);
} /**
* 当前时间加上几分钟,传入负数减去对应的时间
* @param minutes 时间:单位分钟
* @return LocalDateTime
*/
public static LocalDateTime plusMinutes(int minutes){
LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.plusMinutes(minutes);
} /**
* 当前时间加上几秒钟,传入负数减去对应的时间
* @param seconds 时间:单位秒
* @return LocalDateTime
*/
public static LocalDateTime plusSeconds(int seconds){
LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.plusSeconds(seconds);
} /**
* 修改当前时间的小时
* @param hour 时间:单位小时
* @return LocalDateTime
*/
public static LocalDateTime withHour(int hour){
LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.withHour(hour);
} /**
* 修改当前时间的分钟
* @param minute 时间:单位分钟
* @return LocalDateTime
*/
public static LocalDateTime withMinute(int minute){
LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.withMinute(minute);
} /**
* 修改当前时间的秒
* @param second 时间:单位秒
* @return LocalDateTime
*/
public static LocalDateTime withSecond(int second){
LocalDateTime localDateTime = LocalDateTime.now();
return localDateTime.withSecond(second);
} /**
* 给指定的时间加年
* @param data 指定时间
* @param years 要加上的年份负数为减去
* @return LocalDateTime
*/
public static LocalDateTime addYears(String data , int years){
LocalDateTime localDateTime = formatStringToDate(data);
return localDateTime.plusYears(years);
} /**
* 给指定的时间加月
* @param data 指定时间
* @param months 要加上的月负数为减去
* @return LocalDateTime
*/
public static LocalDateTime addMonths(String data , int months){
LocalDateTime localDateTime = formatStringToDate(data);
return localDateTime.plusMonths(months);
} /**
* 给指定的时间加日
* @param data 指定时间
* @param days 要加上的日负数为减去
* @return LocalDateTime
*/
public static LocalDateTime addDays(String data , int days){
LocalDateTime localDateTime = formatStringToDate(data);
return localDateTime.plusDays(days);
} /**
* 给指定的时间加星期
* @param data 指定时间
* @param weeks 要加上的星期负数为减去
* @return LocalDateTime
*/
public static LocalDateTime addWeeks(String data, int weeks){
LocalDateTime localDateTime = formatStringToDate(data);
return localDateTime.plusWeeks(weeks);
} /**
* 给指定时间加小时
* @param data 指定时间
* @param hour 要加上的小时负数为减去
* @return LocalDateTime
*/
public static LocalDateTime addHours(String data , int hour){
LocalDateTime localDateTime = formatStringToDate(data);
return localDateTime.plusHours(hour);
} /**
* 给指定时间加分钟
* @param data 指定时间
* @param minutes 要加上的分钟负数为减去
* @return LocalDateTime
*/
public static LocalDateTime addMinutes(String data , int minutes){
LocalDateTime localDateTime = formatStringToDate(data);
return localDateTime.plusMinutes(minutes);
} /**
* 给指定时间加秒
* @param data 指定时间
* @param seconds 要加上的秒负数为减去
* @return LocalDateTime
*/
public static LocalDateTime addSeconds(String data , int seconds){
LocalDateTime localDateTime = formatStringToDate(data);
return localDateTime.plusSeconds(seconds);
} /**
* 计算两个时间相差多少年,结果为负数说明前面的时间比后面的时间大
* @param time1 时间一
* @param time2 时间二
* @return long
*/
public static long minusYears(String time1 ,String time2){
LocalDateTime localDateTime = formatStringToDate(time1);
LocalDateTime localDateTime2 = formatStringToDate(time2);
return ChronoUnit.YEARS.between(localDateTime,localDateTime2);
} /**
* 计算两个时间相差多少月,结果为负数说明前面的时间比后面的时间大
* @param time1 时间一
* @param time2 时间二
* @return long
*/
public static long minusMonths(String time1 ,String time2){
LocalDateTime localDateTime = formatStringToDate(time1);
LocalDateTime localDateTime2 = formatStringToDate(time2);
return ChronoUnit.MONTHS.between(localDateTime,localDateTime2);
} /**
* 计算两个时间相差多少天,结果为负数说明前面的时间比后面的时间大
* @param time1 时间一
* @param time2 时间二
* @return long
*/
public static long minusDays(String time1 ,String time2){
LocalDateTime localDateTime = formatStringToDate(time1);
LocalDateTime localDateTime2 = formatStringToDate(time2);
return ChronoUnit.DAYS.between(localDateTime,localDateTime2);
} /**
* 计算两个时间相差多少星期,结果为负数说明前面的时间比后面的时间大
* @param time1 时间一
* @param time2 时间二
* @return long
*/
public static long minusWeeks(String time1 ,String time2){
LocalDateTime localDateTime = formatStringToDate(time1);
LocalDateTime localDateTime2 = formatStringToDate(time2);
return ChronoUnit.WEEKS.between(localDateTime,localDateTime2);
} /**
* 计算两个时间相差多少小时,结果为负数说明前面的时间比后面的时间大
* @param time1 时间一
* @param time2 时间二
* @return long
*/
public static long minusHours(String time1 ,String time2){
LocalDateTime localDateTime = formatStringToDate(time1);
LocalDateTime localDateTime2 = formatStringToDate(time2);
return ChronoUnit.HOURS.between(localDateTime,localDateTime2);
} /**
* 计算两个时间相差多少分钟,结果为负数说明前面的时间比后面的时间大
* @param time1 时间一
* @param time2 时间二
* @return long
*/
public static long minusMinutes(String time1 ,String time2){
LocalDateTime localDateTime = formatStringToDate(time1);
LocalDateTime localDateTime2 = formatStringToDate(time2);
return ChronoUnit.MINUTES.between(localDateTime,localDateTime2);
} /**
* 计算两个时间相差多少秒,结果为负数说明前面的时间比后面的时间大
* @param time1 时间一
* @param time2 时间二
* @return long
*/
public static long minusSeconds(String time1 ,String time2){
LocalDateTime localDateTime = formatStringToDate(time1);
LocalDateTime localDateTime2 = formatStringToDate(time2);
return ChronoUnit.MINUTES.between(localDateTime,localDateTime2);
} /**
* 获取传入时间的年份
* @param localDateTime 时间
* @return
*/
public static int getYear(LocalDateTime localDateTime){
return localDateTime.getYear();
} /**
* 获取传入时间的月份
* @param localDateTime 时间
* @return
*/
public static int getMonth(LocalDateTime localDateTime){
return MyMonth.getMonth(localDateTime.getMonth().toString());
} /**
* 获取传入时间的日
* @param localDateTime 时间
* @return
*/
public static int getDay(LocalDateTime localDateTime){
return localDateTime.getDayOfMonth();
} /**
* 获取传入时间是星期几
* @param localDateTime 时间
* @return Integer
*/
public static Integer getWeek(LocalDateTime localDateTime){
return MyDayOfWeek.getDayWeek(localDateTime.getDayOfWeek().toString());
} /**
* 获取传入时间是一年中的第几天
* @param localDateTime 时间
* @return Integer
*/
public static Integer getDayOfYear(LocalDateTime localDateTime){
return localDateTime.getDayOfYear();
} /**
* 获取传入时间的时
* @param localDateTime 时间
* @return
*/
public static int getHour(LocalDateTime localDateTime){
return localDateTime.getHour();
} /**
* 获取传入时间的分
* @param localDateTime 时间
* @return
*/
public static int getMinutes(LocalDateTime localDateTime){
return localDateTime.getMinute();
} /**
* 获取传入时间的秒
* @param localDateTime 时间
* @return
*/
public static int getSeconds(LocalDateTime localDateTime){
return localDateTime.getSecond();
}

  另外加上两个附带使用到的工具类,在获取月份和星期的时候使用

package com.xy.studyboot.util;

/**
* WJY
*/
public enum MyMonth { JANUARY(1,"JANUARY"),
FEBRUARY(2,"FEBRUARY"),
MARCH(3,"MARCH"),
APRIL(4,"APRIL"),
MAY(5,"MAY"),
JUNE(6,"JUNE"),
JULY(7,"JULY"),
AUGUST(8,"AUGUST"),
SEPTEMBER(9,"SEPTEMBER"),
OCTOBER(10,"OCTOBER"),
NOVEMBER(11,"NOVEMBER"),
DECEMBER(12,"DECEMBER"); private int month; private String value; private MyMonth(int month,String value){
this.month = month;
this.value = value;
} public static Integer getMonth(String value){
for (MyMonth myMonth: MyMonth.values()){
if (myMonth.getValue().equals(value)){
return myMonth.getMonth();
}
}
return null;
} public int getMonth() {
return month;
} public String getValue() {
return value;
}
}

 

package com.xy.studyboot.util;

/**
* WJY
*/
public enum MyDayOfWeek { MONDAY(1,"MONDAY"),
TUESDAY(2,"TUESDAY"),
WEDNESDAY(3,"WEDNESDAY"),
THURSDAY(4,"THURSDAY"),
FRIDAY(5,"FRIDAY"),
SATURDAY(6,"SATURDAY"),
SUNDAY(7,"SUNDAY"); private int day; private String value; private MyDayOfWeek(int day , String value){
this.day = day;
this.value = value;
} public static Integer getDayWeek(String value){
for (MyDayOfWeek dayOfWeek : MyDayOfWeek.values()){
if (dayOfWeek.getValue().equals(value)){
return dayOfWeek.getDay();
}
}
return null;
} public int getDay() {
return day;
} public String getValue() {
return value;
} }

  

jdk1.8 时间工具类,可以满足基本操作的更多相关文章

  1. 代码片段:基于 JDK 8 time包的时间工具类 TimeUtil

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “知识的工作者必须成为自己时间的首席执行官.” 前言 这次泥瓦匠带来的是一个好玩的基于 JDK ...

  2. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  3. 小记Java时间工具类

    小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...

  4. 超详细的Java时间工具类

    package com.td.util; import java.sql.Timestamp; import java.text.ParseException; import java.text.Pa ...

  5. java时间工具类

    在项目中,很多地方需要根据时间获取相应的数据,将时间格式化,或者时间比较等相关操作.一个良好的工具类不仅可以减少代码冗余,还能促进业务处理,加快进度. /** * @author: lxw * @Da ...

  6. 基于Java8的日期时间工具类DateTimeFormatter

    原文:https://blog.csdn.net/qq_36596145/article/details/85331002 import java.time.Instant; import java. ...

  7. Apache Commons Lang之日期时间工具类

    码农不识Apache,码尽一生也枉然. FastDateFormat FastDateFormat是一个快速且线程安全的时间操作类,它完全可以替代SimpleDateFromat.因为是线程安全的,所 ...

  8. Java8 ,LocalDate,LocalDateTime处理日期和时间工具类,

    Java8 ,LocalDate,LocalDateTime处理日期和时间工具类 1.获取今天的日期 2.在Java 8 中获取年.月.日信息 3.在Java 8 中处理特定日期 4.在Java 8 ...

  9. java8时间类API安全问题(赠送新的时间工具类哟)

    LocalDateTime等新出的日期类全是final修饰的类,不能被继承,且对应的日期变量都是final修饰的,也就是不可变类.赋值一次后就不可变,不存在多线程数据问题. simpleDateFor ...

随机推荐

  1. Python学习笔记之 Python设计思想&设计原则

    Python设计思想&设计原则 设计思想 1.封装 数据角度 多种数据合为一种数据 优势:代码可读性高            将数据与行为相关联 例如:电脑(内存,储存空间,...) 行为角度 ...

  2. C#LeetCode刷题之#62-不同路径(Unique Paths)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3680 访问. 一个机器人位于一个 m x ...

  3. C#LeetCode刷题之#27-移除元素(Remove Element)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3624 访问. 给定一个数组 nums 和一个值 val,你需要原 ...

  4. C#LeetCode刷题之#896-单调数列(Monotonic Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3760 访问. 如果数组是单调递增或单调递减的,那么它是单调的. ...

  5. python设计模式之状态模式

    python设计模式之状态模式 面向对象编程着力于在对象交互时改变它们的状态.在很多问题中,有限状态机(通常名为状态机)是一个非常方便的状态转换建模(并在必要时以数学方式形式化)工具.首先,什么是状态 ...

  6. 题解 poj 3304

    题目描述 线段和直线判交板子题 分析题目,如果存在这一条直线,那么过这条直线作垂线,一定有一条垂线穿过所有线段,否则不存在.题目转化为寻找一条直线与所有线段有交点. 直线线段判交方法: 1.先判断线段 ...

  7. vue 父子之间传值

    1:父组件 子组件 子组件利用 props 接收父级传过来的数值.子组件选中的数值返回父亲当中利用 钩子函数 $emit('函数名',传过去的数值)  

  8. Jmeter 常用函数(26)- 详解 __chooseRandom

    如果你想查看更多 Jmeter 常用函数可以在这篇文章找找哦 https://www.cnblogs.com/poloyy/p/13291704.html 作用 从指定的范围里面取值 语法格式 ${_ ...

  9. 求求大厂给个Offer:List面试题

    前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 从今天开始,我,三歪,正式开始写面试系列.我给这 ...

  10. nautilus pg autoscaler PG自动伸缩

    链接地址:https://ceph.io/rados/new-in-nautilus-pg-merging-and-autotuning/ [root@controller ~]# ceph osd ...