LocalDateTime
@Component
public class DateUtil { public final static String EMPTY_SRING = "";
public final static String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
public final static long DATE_TIME = * * * ;
public final static long HOUR_TIME = * * ;
public final static long MINUTE_TIME = * ;
public LocalDateTime parseDate(String timeStr) {
// return new LocalDateTimeStringConverter().fromString(timeStr);
DateTimeFormatter pattern =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); //严格按照ISO yyyy-MM-dd验证,03写成3都不行
return LocalDateTime.parse(timeStr, pattern);
} public String currentDateDay() {
LocalDate ldt = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return ldt.format(formatter);
} public String currentDateTime() {
LocalDateTime ldt = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
return ldt.format(formatter);
} public String afterCurrentDate(LocalDateTime dt, int mount, int unit) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.now();
if (dt != null) {
ldt = dt;
}
if (unit == ConstUtil.UNIT_SECOND) {
return ldt.plusSeconds(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_MINUTE) {
return ldt.plusMinutes(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_HOUR) {
return ldt.plusHours(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_DAY) {
return ldt.plusDays(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_WEEK) {
return ldt.plusWeeks(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_MONTH) {
return ldt.plusMonths(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_YEAR) {
return ldt.plusYears(mount).format(formatter);
}
return ldt.format(formatter);
} public String beforeCurrentDate(LocalDateTime dt, int mount, int unit) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.now();
if (dt != null) {
ldt = dt;
}
if (unit == ConstUtil.UNIT_SECOND) {
return ldt.minusSeconds(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_MINUTE) {
return ldt.minusMinutes(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_HOUR) {
return ldt.minusHours(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_DAY) {
return ldt.minusDays(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_WEEK) {
return ldt.minusWeeks(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_MONTH) {
return ldt.minusMonths(mount).format(formatter);
} else if (unit == ConstUtil.UNIT_YEAR) {
return ldt.minusYears(mount).format(formatter);
}
return ldt.format(formatter);
} /**
* @param date
* @return Date
*/
public static Date convertStringToDate(String date) {
try {
return new SimpleDateFormat(DEFAULT_PATTERN).parse(date);
} catch (ParseException e) {
return null;
}
} /**
* 把日期转换成yyyy-MM-dd HH:mm:ss格式
*
* @param date
* @return String
*/
public static String convertDate(Date date) {
if (date == null) {
return EMPTY_SRING;
}
return new SimpleDateFormat(DEFAULT_PATTERN).format(date);
}
/**
* 把时间加上day天后返回,如果传负数代表减day天
*
* @param date
* @param day
* @return Date
*/
public static Date dateAdd(Date date, int day) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + day);
return calendar.getTime();
}
}
LocalDateTime的更多相关文章
- Convert Date between LocalDateTime
http://blog.progs.be/542/date-to-java-time Java8 has new date and time classes to “replace” the old ...
- Java 8 Date Time API Example Tutorial – LocalDate, Instant, LocalDateTime, Parse and Format
参考 Java 8 Date and Time API is one of the most sought after change for developers. Java has been mis ...
- LocalDateTime返回的是Local时间
LocalDateTime返回的是本地时间,比如 LocalDateTime startDateTime = LocalDateTime.of(2016, 9, 18,00, 00); 返回的时间格式 ...
- BeanInstantiationException: Failed to instantiate [java.time.LocalDateTime]
错误提示: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationExce ...
- SpringMvc Json LocalDateTime 互转,form urlencoded @ModelAttribute 转换
JDK8 的LocalDate 系列日期API ,比Date 或者 Calendar 都好用很多,但是在SpringMvc 自动装配会有点小问题 会导致抛出类似异常 default message [ ...
- 在JDBC中使用Java8的日期LocalDate、LocalDateTime
在实体Entity里面,可以使用java.sql.Date.java.sql.Timestamp.java.util.Date来映射到数据库的date.timestamp.datetime等字段 但是 ...
- spring boot添加 LocalDateTime 等 java8 时间类序列化和反序列化的支持
由于项目将原有的 Date类型的字段改造为 LocalDate,LocalDateTime,LocalTime 类型, 发现 spring 对项目的时间格式无法自动转换,故需手动配置下. 在sp ...
- springboot~mybatis里localdatetime序列化问题
问题起因 主要是使用mybatis作为ORM之后,返回的对象为Map,然后对于数据库的datetime,datestamp类型返回为时间戳而不是标准的时间,这个问题解决方案有两种,大叔分析一下: 在m ...
- LocalDate、LocalDateTime、LocalTime开发小结
在我之前的文章<[整理]Java 8新特性总结 >中有提到Date/Time API (JSR 310)对日期与时间的处理.它将服务端对时间的处理进行了统一,使得对时间的处理更加规范和统一 ...
- Java8 LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互转、Date与LocalDateTime互转
本文目前提供:LocalDateTime获取时间戳(毫秒/秒).LocalDateTime与String互转.Date与LocalDateTime互转 文中都使用的时区都是东8区,也就是北京时间.这是 ...
随机推荐
- 搜索和浏览离线 Wikipedia 维基百科(中/英)数据工具
为什么使用离线维基百科?一是因为最近英文维基百科被封,无法访问:二是不受网络限制,使用方便,缺点是不能及时更新,可能会有不影响阅读的乱码. 目前,主要有两种工具用来搜索和浏览离线维基百科数据:Kiwi ...
- Shell 编程 条件语句
本篇主要写一些shell脚本条件语句的使用. 条件测试 test 条件表达式 [ 条件表达式 ] 文件测试 -d:测试是否为目录(Directory). -e:测试文件或目录是否存在(Exist). ...
- Git回滚代码
回滚命令: 1.回退到上个版本 $ git reset --hard HEAD^ 2.回退到前2次提交之前,以此类推,回退到n次提交之前 $ git reset --hard HEAD~2 3.退到/ ...
- 带你快速上手前端三剑客之css
CSS介绍 CSS(Cascading Style Sheet , 层叠样式表)定义如何显示HTML元素.当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染) 组成 每个CS ...
- linux cgroups简介(下)Cgroups 与 Systemd
Cgroups 是 linux 内核提供的一种机制,如果你还不了解 cgroups,请参考前文<Linux cgroups 简介>先了解 cgroups.当 Linux 的 init 系统 ...
- K-th Path CodeForces - 1196F
题目链接:https://vjudge.net/problem/CodeForces-1196F 题意:从图中找出第K短的最短路,最短路:从一个点到另一个的最短距离. 思路:题目说了,每两个点之间的边 ...
- js图片转为base64的格式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Gym-101242B:Branch Assignment(最短路,四边形不等式优化DP)
题意:要完成一个由s个子项目组成的项目,给b(b>=s)个部门分配,从而把b个部门分成s个组.分组完成后,每一组的任 意两个点之间都要传递信息.假设在(i,j)两个点间传送信息,要先把信息加密, ...
- MySQL中经典的too many connection怎么破
文章来源:云栖社区,经同意授权转载 链接:https://yq.aliyun.com/articles/226984?spm=5176.8091938.0.0.nCksaV 错误解决记录:java d ...
- 17-Flutter移动电商实战-首页_楼层区域的编写
1.楼层标题组件 该组件非常简单,只接收一个图片地址,然后显示即可: class FloorTitle extends StatelessWidget { final String picture_ ...