springboot 自定义starter (日期格式转化starter)

  1. 创建spring-boot-dateutils-starter项目
    在这里插入图片描述
  2. 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;
    }
}

  1. 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.
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值