- 创建spring-boot-dateutils-starter项目
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zcy</groupId>
<artifactId>spring-boot-dateutils-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-dateutils-starter</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
3.DateutilServiceConfiguration.java
package com.zcy.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author zhaochengyang
* @version 1.0
* @date 2020/4/1 18:11
*/
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(DateutilProperties.class)
public class DateutilServiceConfiguration {
@Autowired
DateutilProperties DateutilProperties;
@Bean
public DateutilService DateutilService(){
DateutilService helloService =new DateutilService();
return helloService;
}
}
- DateutilService.java
package com.zcy.starter;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
/**
* @author zhaochengyang
* @version 1.0
* @date 2020/4/1 18:12
*/
public class DateutilService {
private static final Logger log = LoggerFactory.getLogger(DateutilService.class);
public DateutilProperties getDateutilspro() {
return dateutilspro;
}
public void setDateutilspro(DateutilProperties dateutilspro) {
this.dateutilspro = dateutilspro;
}
@Autowired
private DateutilProperties dateutilspro;
private static String[] dateFormat = {
"yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy年MM月dd日HH时mm分ss秒",
"yyyy-MM-dd", "yyyy/MM/dd", "yy-MM-dd", "yy/MM/dd", "yyyy年MM月dd日", "HH:mm:ss", "yyyyMMddHHmmss", "yyyyMMdd",
"yyyy.MM.dd", "yy.MM.dd", "MMyyHHmdd", "yyMMddHH", "yyyy-MM" };
public static Timestamp convUtilCalendarToSqlTimestamp(Calendar date) {
if (date == null) {
return null;
}
return new Timestamp(date.getTimeInMillis());
}
public static Calendar convSqlTimestampToUtilCalendar(Timestamp date) {
if (date == null) {
return null;
}
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis(date.getTime());
return gc;
}
public Calendar parseDate(String dateStr) {
if ((dateStr == null) || (dateStr.trim().length() == 0)) {
return null;
}
Date result = parseDate(dateStr, 0);
Calendar cal = Calendar.getInstance();
cal.setTime(result);
return cal;
}
/**
* Timestamp 转化为 DateStr
*
* @param date
* @param formatindex
* @return
*/
public String TimestampToDateStr(Timestamp date, int formatindex) {
Calendar calendar = DateutilService.convSqlTimestampToUtilCalendar(date);
return DateutilService.toDateStr(calendar, formatindex);
}
/**
*
* @param 参数1 要转换的字符串日期
* @param 参数2 格式化类型 选填 默认为 yyyy-MM-dd
* @return
*/
public Date toDateFromString(String... args) {
String ftype = "yyyy-MM-dd";
Date dateTime = null;
for (int i = 0; i < args.length; i++) {
if (!StringUtils.isEmpty(args[1])) {
ftype = args[1];
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ftype);
try {
dateTime = simpleDateFormat.parse(args[0]);
} catch (ParseException e) {
e.