@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的更多相关文章

  1. Convert Date between LocalDateTime

    http://blog.progs.be/542/date-to-java-time Java8 has new date and time classes to “replace” the old ...

  2. 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 ...

  3. LocalDateTime返回的是Local时间

    LocalDateTime返回的是本地时间,比如 LocalDateTime startDateTime = LocalDateTime.of(2016, 9, 18,00, 00); 返回的时间格式 ...

  4. BeanInstantiationException: Failed to instantiate [java.time.LocalDateTime]

    错误提示: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationExce ...

  5. SpringMvc Json LocalDateTime 互转,form urlencoded @ModelAttribute 转换

    JDK8 的LocalDate 系列日期API ,比Date 或者 Calendar 都好用很多,但是在SpringMvc 自动装配会有点小问题 会导致抛出类似异常 default message [ ...

  6. 在JDBC中使用Java8的日期LocalDate、LocalDateTime

    在实体Entity里面,可以使用java.sql.Date.java.sql.Timestamp.java.util.Date来映射到数据库的date.timestamp.datetime等字段 但是 ...

  7. spring boot添加 LocalDateTime 等 java8 时间类序列化和反序列化的支持

    由于项目将原有的  Date类型的字段改造为 LocalDate,LocalDateTime,LocalTime 类型, 发现  spring  对项目的时间格式无法自动转换,故需手动配置下. 在sp ...

  8. springboot~mybatis里localdatetime序列化问题

    问题起因 主要是使用mybatis作为ORM之后,返回的对象为Map,然后对于数据库的datetime,datestamp类型返回为时间戳而不是标准的时间,这个问题解决方案有两种,大叔分析一下: 在m ...

  9. LocalDate、LocalDateTime、LocalTime开发小结

    在我之前的文章<[整理]Java 8新特性总结 >中有提到Date/Time API (JSR 310)对日期与时间的处理.它将服务端对时间的处理进行了统一,使得对时间的处理更加规范和统一 ...

  10. Java8 LocalDateTime获取时间戳(毫秒/秒)、LocalDateTime与String互转、Date与LocalDateTime互转

    本文目前提供:LocalDateTime获取时间戳(毫秒/秒).LocalDateTime与String互转.Date与LocalDateTime互转 文中都使用的时区都是东8区,也就是北京时间.这是 ...

随机推荐

  1. 如何预防SQL注入

    归纳一下,主要有以下几点: 1.永远不要信任用户的输入.对用户的输入进行校验,可以通过正则表达式,或限制长度:对单引号和 双"-"进行转换等. 2.永远不要使用动态拼装sql,可以 ...

  2. spark累加器、广播变量

    一言以蔽之: 累加器就是只写变量 通常就是做事件统计用的 因为rdd是在不同的excutor去执行的 你在不同excutor中累加的结果 没办法汇总到一起 这个时候就需要累加器来帮忙完成 广播变量是只 ...

  3. solr根据id删除索引

    删除solr索引数据,使用XML有两种写法: 1) <delete><id>1</id></delete> <commit/> 2) < ...

  4. 【HTTP】另类的POST头数据 RFC1867协议格式简析

    http://blog.csdn.net/ai2000ai/article/details/52161979 昨天在实战表单模拟提交的时候,有发现在提交某个表单的时候,页面(discuz!论坛)报错, ...

  5. rsync同步备份

    一.服务器端.备份客户端安装 rsync 服务. 1.环境: CentOS 主 IP:172.16.3.18 备 IP:172.16.3.19 2.安装 rsync 软件 #yum install r ...

  6. bzoj1935: [Shoi2007]Tree 园丁的烦恼lowbit 离散化

    链接 bzoj 最好不要去luogu,数据太水 思路 一个询问转化成四个矩阵,求起点\((0,0)到(x,y)\)的矩阵 离线处理,离散化掉y,x不用离散. 一行一行的求,每次处理完一行之后下一行的贡 ...

  7. C语言中常见的图形打印总结

    直角三角形(靠右直立) 示例实现代码如下: int main(){ int n; int i,j; cin >> n; if(n<= 0){ cout << " ...

  8. spring注解式参数校验列表

    校验注释列表: @AssertFalse Boolean,boolean 验证注解的元素值是false @AssertTrue Boolean,boolean 验证注解的元素值是true @NotNu ...

  9. 07-图5 Saving James Bond - Hard Version (30 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  10. 原创:Kmeans算法实战+改进(java实现)

    kmeans算法的流程:   EM思想很伟大,在处理含有隐式变量的机器学习算法中很有用.聚类算法包括kmeans,高斯混合聚类,快速迭代聚类等等,都离不开EM思想.在了解kmeans算法之前,有必要详 ...