Commit f88bff42363fceea3c65d742279a1056786b1c41
Merge branch 'dev2.0'
* dev2.0: chore #升级版本为:2.0.6,并发布到创库。 feat #增加方法:指定保留位数 feat #修改方法名称 feat 返回数据由封装对象修改为基本数据类型 feat #增加浮点数的加减乘除的四则运算 feat #补充日期工具类 feat #添加判断空字符串 feat #添加时间、对象工具类 chore #升级第三方库版本 # fix修改返回结果提示消息 #chore:更新maven私有库配置 #fix:修复在升级至2.0后,用户无法登陆的bug #fix:修复不开启1级缓存时报NullException的bug #feat:将二级缓存默认缓存时间从120秒修改为1800秒 #chore:升级版本为2.0.0
Showing
21 changed files
with
1567 additions
and
125 deletions
pom.xml
| @@ -6,22 +6,21 @@ | @@ -6,22 +6,21 @@ | ||
| 6 | 6 | ||
| 7 | <groupId>com.irrigation</groupId> | 7 | <groupId>com.irrigation</groupId> |
| 8 | <artifactId>common-lib</artifactId> | 8 | <artifactId>common-lib</artifactId> |
| 9 | - <version>1.2.4</version> | 9 | + <version>2.0.6</version> |
| 10 | 10 | ||
| 11 | <properties> | 11 | <properties> |
| 12 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | 12 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
| 13 | <java.version>1.8</java.version> | 13 | <java.version>1.8</java.version> |
| 14 | - <spring-boot.version>1.5.10.RELEASE</spring-boot.version> | 14 | + <spring-boot.version>2.0.5.RELEASE</spring-boot.version> |
| 15 | <swagger.version>2.7.0</swagger.version> | 15 | <swagger.version>2.7.0</swagger.version> |
| 16 | - <lombok.version>1.16.20</lombok.version> | 16 | + <lombok.version>1.18.10</lombok.version> |
| 17 | <commons-codec.version>1.11</commons-codec.version> | 17 | <commons-codec.version>1.11</commons-codec.version> |
| 18 | - <guava.vsersion>20.0</guava.vsersion> | 18 | + <guava.vsersion>28.1-jre</guava.vsersion> |
| 19 | <slf4j.version>1.7.24</slf4j.version> | 19 | <slf4j.version>1.7.24</slf4j.version> |
| 20 | <fastjson.version>1.2.49</fastjson.version> | 20 | <fastjson.version>1.2.49</fastjson.version> |
| 21 | - <spring-web.version>4.3.14.RELEASE</spring-web.version> | ||
| 22 | - <spring-security-core.version>4.2.4.RELEASE</spring-security-core.version> | 21 | + <spring-web.version>5.0.9.RELEASE</spring-web.version> |
| 22 | + <spring-security-core.version>5.0.9.RELEASE</spring-security-core.version> | ||
| 23 | <ehcache.version>2.6.11</ehcache.version> | 23 | <ehcache.version>2.6.11</ehcache.version> |
| 24 | - | ||
| 25 | </properties> | 24 | </properties> |
| 26 | 25 | ||
| 27 | 26 | ||
| @@ -116,6 +115,12 @@ | @@ -116,6 +115,12 @@ | ||
| 116 | <version>${spring-boot.version}</version> | 115 | <version>${spring-boot.version}</version> |
| 117 | <!--<scope>provided</scope>--> | 116 | <!--<scope>provided</scope>--> |
| 118 | </dependency> | 117 | </dependency> |
| 118 | + <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> | ||
| 119 | + <!-- <dependency>--> | ||
| 120 | + <!-- <groupId>org.apache.commons</groupId>--> | ||
| 121 | + <!-- <artifactId>commons-lang3</artifactId>--> | ||
| 122 | + <!-- <version>3.9</version>--> | ||
| 123 | + <!-- </dependency>--> | ||
| 119 | 124 | ||
| 120 | </dependencies> | 125 | </dependencies> |
| 121 | <build> | 126 | <build> |
| @@ -123,6 +128,7 @@ | @@ -123,6 +128,7 @@ | ||
| 123 | <plugin> | 128 | <plugin> |
| 124 | <groupId>org.apache.maven.plugins</groupId> | 129 | <groupId>org.apache.maven.plugins</groupId> |
| 125 | <artifactId>maven-compiler-plugin</artifactId> | 130 | <artifactId>maven-compiler-plugin</artifactId> |
| 131 | + <version>3.7.0</version> | ||
| 126 | <configuration> | 132 | <configuration> |
| 127 | <source>1.8</source> | 133 | <source>1.8</source> |
| 128 | <target>1.8</target> | 134 | <target>1.8</target> |
| @@ -150,12 +156,12 @@ | @@ -150,12 +156,12 @@ | ||
| 150 | <!--上传到私有仓库 --> | 156 | <!--上传到私有仓库 --> |
| 151 | <distributionManagement> | 157 | <distributionManagement> |
| 152 | <repository> | 158 | <repository> |
| 153 | - <id>nexus-releases</id> | ||
| 154 | - <url>http://39.108.110.2:8081/repository/maven-releases/</url> | 159 | + <id>rdc-releases</id> |
| 160 | + <url>https://repo.rdc.aliyun.com/repository/24784-release-oRxGgU/</url> | ||
| 155 | </repository> | 161 | </repository> |
| 156 | <snapshotRepository> | 162 | <snapshotRepository> |
| 157 | - <id>nexus-snapshot</id> | ||
| 158 | - <url>http://39.108.110.2:8081/repository/maven-snapshots/</url> | 163 | + <id>rdc-snapshots</id> |
| 164 | + <url>https://repo.rdc.aliyun.com/repository/24784-snapshot-Dsv8kn/</url> | ||
| 159 | </snapshotRepository> | 165 | </snapshotRepository> |
| 160 | </distributionManagement> | 166 | </distributionManagement> |
| 161 | </project> | 167 | </project> |
| 162 | \ No newline at end of file | 168 | \ No newline at end of file |
src/main/java/com/irrigation/icl/cache/Command.java
| @@ -5,7 +5,8 @@ import lombok.Data; | @@ -5,7 +5,8 @@ import lombok.Data; | ||
| 5 | import java.io.Serializable; | 5 | import java.io.Serializable; |
| 6 | 6 | ||
| 7 | /** | 7 | /** |
| 8 | - * Command | 8 | + * @author |
| 9 | + * @Description 缓存 命令 类 | ||
| 9 | */ | 10 | */ |
| 10 | @Data | 11 | @Data |
| 11 | public class Command implements Serializable { | 12 | public class Command implements Serializable { |
src/main/java/com/irrigation/icl/cache/LayeringCacheAutoFactory.java
| @@ -9,6 +9,8 @@ import org.springframework.cache.CacheManager; | @@ -9,6 +9,8 @@ import org.springframework.cache.CacheManager; | ||
| 9 | import org.springframework.context.annotation.Bean; | 9 | import org.springframework.context.annotation.Bean; |
| 10 | import org.springframework.context.annotation.Conditional; | 10 | import org.springframework.context.annotation.Conditional; |
| 11 | import org.springframework.context.annotation.Configuration; | 11 | import org.springframework.context.annotation.Configuration; |
| 12 | +import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
| 13 | +import org.springframework.data.redis.cache.RedisCacheWriter; | ||
| 12 | import org.springframework.data.redis.connection.Message; | 14 | import org.springframework.data.redis.connection.Message; |
| 13 | import org.springframework.data.redis.connection.MessageListener; | 15 | import org.springframework.data.redis.connection.MessageListener; |
| 14 | import org.springframework.data.redis.connection.RedisConnectionFactory; | 16 | import org.springframework.data.redis.connection.RedisConnectionFactory; |
| @@ -17,6 +19,8 @@ import org.springframework.data.redis.listener.PatternTopic; | @@ -17,6 +19,8 @@ import org.springframework.data.redis.listener.PatternTopic; | ||
| 17 | import org.springframework.data.redis.listener.RedisMessageListenerContainer; | 19 | import org.springframework.data.redis.listener.RedisMessageListenerContainer; |
| 18 | import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; | 20 | import org.springframework.data.redis.listener.adapter.MessageListenerAdapter; |
| 19 | 21 | ||
| 22 | +import java.time.Duration; | ||
| 23 | + | ||
| 20 | /** | 24 | /** |
| 21 | * @Author: boni | 25 | * @Author: boni |
| 22 | * @Date: 2018/8/24-下午2:37 | 26 | * @Date: 2018/8/24-下午2:37 |
| @@ -29,11 +33,19 @@ public class LayeringCacheAutoFactory { | @@ -29,11 +33,19 @@ public class LayeringCacheAutoFactory { | ||
| 29 | @Bean | 33 | @Bean |
| 30 | public LayeringCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate, | 34 | public LayeringCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate, |
| 31 | LayeringCacheProperties layeringCacheProperties) { | 35 | LayeringCacheProperties layeringCacheProperties) { |
| 32 | - LayeringCacheManager cacheManager = new LayeringCacheManager(redisTemplate, layeringCacheProperties); | ||
| 33 | - cacheManager.setUsePrefix(true); | ||
| 34 | - long expire = layeringCacheProperties.getExpire() > 0L ? layeringCacheProperties.getExpire() : 120L; | ||
| 35 | - cacheManager.setExpires(layeringCacheProperties.getExpires()); | ||
| 36 | - cacheManager.setDefaultExpiration(expire); | 36 | + |
| 37 | + RedisCacheWriter redisCacheWriter = RedisCacheWriter | ||
| 38 | + .nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory()); | ||
| 39 | + long expire = layeringCacheProperties.getExpire() > 0L ? layeringCacheProperties.getExpire() : 1800L; | ||
| 40 | + RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration | ||
| 41 | + .defaultCacheConfig() | ||
| 42 | + .entryTtl(Duration.ofSeconds(expire)); | ||
| 43 | + LayeringCacheManager cacheManager = | ||
| 44 | + new LayeringCacheManager( | ||
| 45 | + redisTemplate, | ||
| 46 | + redisCacheWriter, | ||
| 47 | + redisCacheConfiguration, | ||
| 48 | + layeringCacheProperties); | ||
| 37 | return cacheManager; | 49 | return cacheManager; |
| 38 | } | 50 | } |
| 39 | 51 | ||
| @@ -43,8 +55,10 @@ public class LayeringCacheAutoFactory { | @@ -43,8 +55,10 @@ public class LayeringCacheAutoFactory { | ||
| 43 | MessageListenerAdapter listenerAdapter) { | 55 | MessageListenerAdapter listenerAdapter) { |
| 44 | RedisMessageListenerContainer container = new RedisMessageListenerContainer(); | 56 | RedisMessageListenerContainer container = new RedisMessageListenerContainer(); |
| 45 | container.setConnectionFactory(connectionFactory); | 57 | container.setConnectionFactory(connectionFactory); |
| 46 | - container.addMessageListener(listenerAdapter, new PatternTopic(layeringCacheProperties.getCacheL1().getTopic())); | ||
| 47 | - | 58 | + LayeringCacheProperties.L1 l1 = layeringCacheProperties.getCacheL1(); |
| 59 | + if (l1 != null) { | ||
| 60 | + container.addMessageListener(listenerAdapter, new PatternTopic(layeringCacheProperties.getCacheL1().getTopic())); | ||
| 61 | + } | ||
| 48 | return container; | 62 | return container; |
| 49 | } | 63 | } |
| 50 | 64 |
src/main/java/com/irrigation/icl/cache/config/CacheConfig.java
| @@ -25,7 +25,7 @@ public class CacheConfig { | @@ -25,7 +25,7 @@ public class CacheConfig { | ||
| 25 | // om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); | 25 | // om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); |
| 26 | // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); | 26 | // om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); |
| 27 | // jackson2JsonRedisSerializer.setObjectMapper(om); | 27 | // jackson2JsonRedisSerializer.setObjectMapper(om); |
| 28 | -// | 28 | + |
| 29 | template.setValueSerializer(new JdkSerializationRedisSerializer()); | 29 | template.setValueSerializer(new JdkSerializationRedisSerializer()); |
| 30 | //使用StringRedisSerializer来序列化和反序列化redis的key值 | 30 | //使用StringRedisSerializer来序列化和反序列化redis的key值 |
| 31 | template.setKeySerializer(new StringRedisSerializer()); | 31 | template.setKeySerializer(new StringRedisSerializer()); |
src/main/java/com/irrigation/icl/cache/core/LayeringCache.java
| @@ -14,11 +14,13 @@ import net.sf.ehcache.store.MemoryStoreEvictionPolicy; | @@ -14,11 +14,13 @@ import net.sf.ehcache.store.MemoryStoreEvictionPolicy; | ||
| 14 | import org.springframework.cache.Cache; | 14 | import org.springframework.cache.Cache; |
| 15 | import org.springframework.cache.support.SimpleValueWrapper; | 15 | import org.springframework.cache.support.SimpleValueWrapper; |
| 16 | import org.springframework.data.redis.cache.RedisCache; | 16 | import org.springframework.data.redis.cache.RedisCache; |
| 17 | -import org.springframework.data.redis.core.RedisOperations; | 17 | +import org.springframework.data.redis.cache.RedisCacheConfiguration; |
| 18 | +import org.springframework.data.redis.cache.RedisCacheWriter; | ||
| 18 | import org.springframework.util.StopWatch; | 19 | import org.springframework.util.StopWatch; |
| 19 | 20 | ||
| 20 | import java.text.SimpleDateFormat; | 21 | import java.text.SimpleDateFormat; |
| 21 | import java.util.Date; | 22 | import java.util.Date; |
| 23 | +import java.util.Optional; | ||
| 22 | import java.util.concurrent.Callable; | 24 | import java.util.concurrent.Callable; |
| 23 | import java.util.concurrent.ConcurrentHashMap; | 25 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | import java.util.concurrent.Future; | 26 | import java.util.concurrent.Future; |
| @@ -36,17 +38,16 @@ public class LayeringCache extends RedisCache { | @@ -36,17 +38,16 @@ public class LayeringCache extends RedisCache { | ||
| 36 | private CacheManager layeringL1CacheManager; | 38 | private CacheManager layeringL1CacheManager; |
| 37 | private boolean layerL1Enable; | 39 | private boolean layerL1Enable; |
| 38 | private String ID; | 40 | private String ID; |
| 39 | -// private String HOST; | ||
| 40 | -// private String CACHE_STORE; | ||
| 41 | -// private String CACHE_STORE_SYNC; | ||
| 42 | 41 | ||
| 43 | - | ||
| 44 | - public LayeringCache(LayeringCacheManager layeringCacheManager, String name, byte[] prefix, | ||
| 45 | - RedisOperations<? extends Object, ? extends Object> redisOperations, long expiration) { | ||
| 46 | - super(name, prefix, redisOperations, expiration); | 42 | + public LayeringCache(LayeringCacheManager layeringCacheManager, String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfig) { |
| 43 | + super(name, cacheWriter, cacheConfig); | ||
| 47 | this.layeringCacheManager = layeringCacheManager; | 44 | this.layeringCacheManager = layeringCacheManager; |
| 48 | this.layeringCacheProperties = layeringCacheManager.getLayeringCacheProperties(); | 45 | this.layeringCacheProperties = layeringCacheManager.getLayeringCacheProperties(); |
| 49 | - this.layerL1Enable = layeringCacheProperties.getCacheL1().isEnable(); | 46 | + this.layerL1Enable = |
| 47 | + Optional.ofNullable(layeringCacheProperties.getCacheL1()) | ||
| 48 | + .orElse(new LayeringCacheProperties.L1()) | ||
| 49 | + .isEnable(); | ||
| 50 | + | ||
| 50 | initEhcache(); | 51 | initEhcache(); |
| 51 | } | 52 | } |
| 52 | 53 | ||
| @@ -54,6 +55,7 @@ public class LayeringCache extends RedisCache { | @@ -54,6 +55,7 @@ public class LayeringCache extends RedisCache { | ||
| 54 | public Cache.ValueWrapper get(Object key) { | 55 | public Cache.ValueWrapper get(Object key) { |
| 55 | StopWatch stopWatch = new StopWatch(); | 56 | StopWatch stopWatch = new StopWatch(); |
| 56 | stopWatch.start(); | 57 | stopWatch.start(); |
| 58 | + | ||
| 57 | Ehcache ehCache = null; | 59 | Ehcache ehCache = null; |
| 58 | if (layerL1Enable) { | 60 | if (layerL1Enable) { |
| 59 | ehCache = getL1Cache(super.getName()); | 61 | ehCache = getL1Cache(super.getName()); |
| @@ -157,7 +159,7 @@ public class LayeringCache extends RedisCache { | @@ -157,7 +159,7 @@ public class LayeringCache extends RedisCache { | ||
| 157 | Callable<Ehcache> callable = () -> { | 159 | Callable<Ehcache> callable = () -> { |
| 158 | Ehcache cache = layeringL1CacheManager.getCache(name); | 160 | Ehcache cache = layeringL1CacheManager.getCache(name); |
| 159 | if (cache == null) { | 161 | if (cache == null) { |
| 160 | - CacheConfiguration cacheConfiguration = getCacheConfiguration(); | 162 | + CacheConfiguration cacheConfiguration = getEncacheCacheConfiguration(); |
| 161 | cacheConfiguration.setName(name); | 163 | cacheConfiguration.setName(name); |
| 162 | Ehcache ehcache = new net.sf.ehcache.Cache(cacheConfiguration); | 164 | Ehcache ehcache = new net.sf.ehcache.Cache(cacheConfiguration); |
| 163 | layeringL1CacheManager.addCache(ehcache); | 165 | layeringL1CacheManager.addCache(ehcache); |
| @@ -188,7 +190,7 @@ public class LayeringCache extends RedisCache { | @@ -188,7 +190,7 @@ public class LayeringCache extends RedisCache { | ||
| 188 | return simpleDateFormat.format(new Date()); | 190 | return simpleDateFormat.format(new Date()); |
| 189 | } | 191 | } |
| 190 | 192 | ||
| 191 | - private CacheConfiguration getCacheConfiguration() { | 193 | + private CacheConfiguration getEncacheCacheConfiguration() { |
| 192 | // Cache | 194 | // Cache |
| 193 | CacheConfiguration cacheConfiguration = new CacheConfiguration(); | 195 | CacheConfiguration cacheConfiguration = new CacheConfiguration(); |
| 194 | cacheConfiguration.setEternal(false); | 196 | cacheConfiguration.setEternal(false); |
| @@ -205,12 +207,6 @@ public class LayeringCache extends RedisCache { | @@ -205,12 +207,6 @@ public class LayeringCache extends RedisCache { | ||
| 205 | cacheConfiguration.copyOnRead(true); | 207 | cacheConfiguration.copyOnRead(true); |
| 206 | cacheConfiguration.copyOnWrite(true); | 208 | cacheConfiguration.copyOnWrite(true); |
| 207 | 209 | ||
| 208 | -// CopyStrategyConfiguration copyStrategyConfiguration = new CopyStrategyConfiguration(); | ||
| 209 | -// copyStrategyConfiguration.setClass(MyCopyStrategy.class.getName()); | ||
| 210 | -// copyStrategyConfiguration.setCopyStrategyInstance(new MyCopyStrategy()); | ||
| 211 | - | ||
| 212 | -// cacheConfiguration.addCopyStrategy(copyStrategyConfiguration); | ||
| 213 | - | ||
| 214 | cacheConfiguration.setTimeToIdleSeconds(layeringCacheProperties.getCacheL1().getLocalTimeToIdleSeconds()); | 210 | cacheConfiguration.setTimeToIdleSeconds(layeringCacheProperties.getCacheL1().getLocalTimeToIdleSeconds()); |
| 215 | cacheConfiguration.setTimeToLiveSeconds(layeringCacheProperties.getCacheL1().getLocalTimeToLiveSeconds()); | 211 | cacheConfiguration.setTimeToLiveSeconds(layeringCacheProperties.getCacheL1().getLocalTimeToLiveSeconds()); |
| 216 | 212 | ||
| @@ -220,13 +216,6 @@ public class LayeringCache extends RedisCache { | @@ -220,13 +216,6 @@ public class LayeringCache extends RedisCache { | ||
| 220 | private void initEhcache() { | 216 | private void initEhcache() { |
| 221 | if (layerL1Enable) { | 217 | if (layerL1Enable) { |
| 222 | this.ID = layeringCacheProperties.getCacheL1().getKey() + "." + getDate(); | 218 | this.ID = layeringCacheProperties.getCacheL1().getKey() + "." + getDate(); |
| 223 | -// this.HOST = Utils.getLocalHostIP(); | ||
| 224 | -// this.CACHE_STORE = layeringCacheProperties.getCacheL1().getKey() | ||
| 225 | -// + layeringCacheProperties.getCacheL1().getSeparator() | ||
| 226 | -// + "cache" | ||
| 227 | -// + layeringCacheProperties.getCacheL1().getSeparator() | ||
| 228 | -// + "store"; | ||
| 229 | -// this.CACHE_STORE_SYNC = this.CACHE_STORE + layeringCacheProperties.getCacheL1().getSeparator() + "sync"; | ||
| 230 | 219 | ||
| 231 | Configuration configuration = new Configuration(); | 220 | Configuration configuration = new Configuration(); |
| 232 | configuration.setName(this.ID); | 221 | configuration.setName(this.ID); |
| @@ -238,7 +227,7 @@ public class LayeringCache extends RedisCache { | @@ -238,7 +227,7 @@ public class LayeringCache extends RedisCache { | ||
| 238 | dsc.setPath(layeringCacheProperties.getCacheL1().getLocalStoreLocation() + this.ID); | 227 | dsc.setPath(layeringCacheProperties.getCacheL1().getLocalStoreLocation() + this.ID); |
| 239 | configuration.diskStore(dsc); | 228 | configuration.diskStore(dsc); |
| 240 | 229 | ||
| 241 | - CacheConfiguration cacheConfiguration = getCacheConfiguration(); | 230 | + CacheConfiguration cacheConfiguration = getEncacheCacheConfiguration(); |
| 242 | 231 | ||
| 243 | configuration.setDefaultCacheConfiguration(cacheConfiguration); | 232 | configuration.setDefaultCacheConfiguration(cacheConfiguration); |
| 244 | configuration.setDynamicConfig(false); | 233 | configuration.setDynamicConfig(false); |
| @@ -248,25 +237,4 @@ public class LayeringCache extends RedisCache { | @@ -248,25 +237,4 @@ public class LayeringCache extends RedisCache { | ||
| 248 | layeringL1CacheManager = new CacheManager(configuration); | 237 | layeringL1CacheManager = new CacheManager(configuration); |
| 249 | } | 238 | } |
| 250 | } | 239 | } |
| 251 | -// | ||
| 252 | -// class MyCopyStrategy implements ReadWriteCopyStrategy<Element> { | ||
| 253 | -// @Override | ||
| 254 | -// public Element copyForWrite(Element value) { | ||
| 255 | -// if (value != null) { | ||
| 256 | -// Object temp = (Serializable) value.getObjectValue(); | ||
| 257 | -// return new Element(value.getObjectKey(), temp); | ||
| 258 | -// } | ||
| 259 | -// return value; | ||
| 260 | -// } | ||
| 261 | -// | ||
| 262 | -// @Override | ||
| 263 | -// public Element copyForRead(Element storedValue) { | ||
| 264 | -// if (storedValue != null) { | ||
| 265 | -// Object temp = (Serializable) storedValue.getObjectValue(); | ||
| 266 | -// return new Element(storedValue.getObjectKey(), temp); | ||
| 267 | -// } | ||
| 268 | -// return storedValue; | ||
| 269 | -// | ||
| 270 | -// } | ||
| 271 | -// } | ||
| 272 | } | 240 | } |
src/main/java/com/irrigation/icl/cache/core/LayeringCacheLoadCondition.java
| 1 | package com.irrigation.icl.cache.core; | 1 | package com.irrigation.icl.cache.core; |
| 2 | 2 | ||
| 3 | -import org.springframework.boot.bind.RelaxedPropertyResolver; | 3 | +import org.springframework.boot.context.properties.bind.Binder; |
| 4 | import org.springframework.context.annotation.Condition; | 4 | import org.springframework.context.annotation.Condition; |
| 5 | import org.springframework.context.annotation.ConditionContext; | 5 | import org.springframework.context.annotation.ConditionContext; |
| 6 | import org.springframework.core.type.AnnotatedTypeMetadata; | 6 | import org.springframework.core.type.AnnotatedTypeMetadata; |
| @@ -12,13 +12,10 @@ import org.springframework.core.type.AnnotatedTypeMetadata; | @@ -12,13 +12,10 @@ import org.springframework.core.type.AnnotatedTypeMetadata; | ||
| 12 | public class LayeringCacheLoadCondition implements Condition { | 12 | public class LayeringCacheLoadCondition implements Condition { |
| 13 | @Override | 13 | @Override |
| 14 | public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { | 14 | public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { |
| 15 | - RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( | ||
| 16 | - context.getEnvironment(), "springext.cache."); | ||
| 17 | - | ||
| 18 | - Boolean enable = resolver.getProperty("enable",Boolean.class); | ||
| 19 | - if(enable == null){ | ||
| 20 | - return false; | ||
| 21 | - } | 15 | + Boolean enable = Binder |
| 16 | + .get(context.getEnvironment()) | ||
| 17 | + .bind("springext.cache.enable", Boolean.class) | ||
| 18 | + .orElse(false); | ||
| 22 | return enable.booleanValue(); | 19 | return enable.booleanValue(); |
| 23 | 20 | ||
| 24 | } | 21 | } |
src/main/java/com/irrigation/icl/cache/core/LayeringCacheManager.java
| 1 | package com.irrigation.icl.cache.core; | 1 | package com.irrigation.icl.cache.core; |
| 2 | 2 | ||
| 3 | import com.irrigation.icl.cache.Command; | 3 | import com.irrigation.icl.cache.Command; |
| 4 | +import lombok.Getter; | ||
| 4 | import org.springframework.data.redis.cache.RedisCache; | 5 | import org.springframework.data.redis.cache.RedisCache; |
| 6 | +import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
| 5 | import org.springframework.data.redis.cache.RedisCacheManager; | 7 | import org.springframework.data.redis.cache.RedisCacheManager; |
| 6 | -import org.springframework.data.redis.core.RedisOperations; | 8 | +import org.springframework.data.redis.cache.RedisCacheWriter; |
| 9 | +import org.springframework.data.redis.core.RedisTemplate; | ||
| 7 | 10 | ||
| 8 | /** | 11 | /** |
| 9 | * @Author: boni | 12 | * @Author: boni |
| 10 | * @Date: 2018/8/24-下午2:29 | 13 | * @Date: 2018/8/24-下午2:29 |
| 11 | */ | 14 | */ |
| 15 | +@Getter | ||
| 12 | public class LayeringCacheManager extends RedisCacheManager { | 16 | public class LayeringCacheManager extends RedisCacheManager { |
| 13 | 17 | ||
| 18 | + final RedisCacheWriter cacheWriter; | ||
| 19 | + final RedisCacheConfiguration redisCacheConfiguration; | ||
| 14 | private LayeringCacheProperties layeringCacheProperties; | 20 | private LayeringCacheProperties layeringCacheProperties; |
| 15 | - | ||
| 16 | - public LayeringCacheProperties getLayeringCacheProperties() { | ||
| 17 | - return layeringCacheProperties; | ||
| 18 | - } | ||
| 19 | - | ||
| 20 | - public void setLayeringCacheProperties(LayeringCacheProperties layeringCacheProperties) { | 21 | + private RedisTemplate redisTemplate; |
| 22 | + | ||
| 23 | + public LayeringCacheManager(RedisTemplate redisTemplate, RedisCacheWriter cacheWriter, RedisCacheConfiguration redisCacheConfiguration, | ||
| 24 | + LayeringCacheProperties layeringCacheProperties) { | ||
| 25 | + super(cacheWriter, redisCacheConfiguration); | ||
| 26 | + this.cacheWriter = cacheWriter; | ||
| 27 | + this.redisCacheConfiguration = redisCacheConfiguration; | ||
| 28 | + this.redisTemplate = redisTemplate; | ||
| 21 | this.layeringCacheProperties = layeringCacheProperties; | 29 | this.layeringCacheProperties = layeringCacheProperties; |
| 22 | } | 30 | } |
| 23 | 31 | ||
| 24 | - public LayeringCacheManager(RedisOperations redisOperations, LayeringCacheProperties layeringCacheProperties) { | ||
| 25 | - super(redisOperations); | ||
| 26 | - this.layeringCacheProperties = layeringCacheProperties; | ||
| 27 | - } | ||
| 28 | - | ||
| 29 | - @SuppressWarnings("unchecked") | ||
| 30 | @Override | 32 | @Override |
| 31 | - protected RedisCache createCache(String cacheName) { | ||
| 32 | - long expiration = computeExpiration(cacheName); | ||
| 33 | - return new LayeringCache( | ||
| 34 | - this, | ||
| 35 | - cacheName, | ||
| 36 | - (this.isUsePrefix() ? this.getCachePrefix().prefix(cacheName) : null), | ||
| 37 | - this.getRedisOperations(), | ||
| 38 | - expiration); | 33 | + protected RedisCache getMissingCache(String name) { |
| 34 | + return new LayeringCache(this, name, this.cacheWriter, this.redisCacheConfiguration); | ||
| 39 | } | 35 | } |
| 40 | 36 | ||
| 41 | - | ||
| 42 | //notify other redis clent to update L1 | 37 | //notify other redis clent to update L1 |
| 43 | public void publishMessage(Command command) { | 38 | public void publishMessage(Command command) { |
| 44 | - this.getRedisOperations().convertAndSend(this.getLayeringCacheProperties().getCacheL1().getTopic(), command); | 39 | + this.getRedisTemplate().convertAndSend(this.getLayeringCacheProperties().getCacheL1().getTopic(), command); |
| 45 | } | 40 | } |
| 46 | 41 | ||
| 47 | 42 |
src/main/java/com/irrigation/icl/cache/core/LayeringCacheProperties.java
| @@ -73,10 +73,10 @@ public class LayeringCacheProperties { | @@ -73,10 +73,10 @@ public class LayeringCacheProperties { | ||
| 73 | /** | 73 | /** |
| 74 | * 本地缓存过期时间,单位秒,默认10分钟 | 74 | * 本地缓存过期时间,单位秒,默认10分钟 |
| 75 | */ | 75 | */ |
| 76 | - private int localTimeToLiveSeconds = 1 * 60; | ||
| 77 | - private int localTimeToIdleSeconds = 0; | 76 | + private int localTimeToLiveSeconds = 10 * 60; |
| 77 | + private int localTimeToIdleSeconds = 30; | ||
| 78 | /** | 78 | /** |
| 79 | - * 本地缓存清理周期,单位秒,默认3分钟 | 79 | + * 本地缓存清理周期,单位秒,默认90秒 |
| 80 | */ | 80 | */ |
| 81 | private int localDiskExpiryThreadIntervalSeconds = 90; | 81 | private int localDiskExpiryThreadIntervalSeconds = 90; |
| 82 | } | 82 | } |
src/main/java/com/irrigation/icl/entity/PageList.java
| @@ -10,8 +10,10 @@ import lombok.NoArgsConstructor; | @@ -10,8 +10,10 @@ import lombok.NoArgsConstructor; | ||
| 10 | import java.util.List; | 10 | import java.util.List; |
| 11 | 11 | ||
| 12 | /** | 12 | /** |
| 13 | - * 分页返回类 | ||
| 14 | * @param <E> | 13 | * @param <E> |
| 14 | + * @author zhangchuanxi | ||
| 15 | + * 分页返回类 | ||
| 16 | + * @version 1.0.0 | ||
| 15 | */ | 17 | */ |
| 16 | @Data | 18 | @Data |
| 17 | @NoArgsConstructor | 19 | @NoArgsConstructor |
src/main/java/com/irrigation/icl/entity/PageParam.java
src/main/java/com/irrigation/icl/entity/RestResult.java
| @@ -11,25 +11,29 @@ import lombok.Data; | @@ -11,25 +11,29 @@ import lombok.Data; | ||
| 11 | import lombok.NoArgsConstructor; | 11 | import lombok.NoArgsConstructor; |
| 12 | 12 | ||
| 13 | /** | 13 | /** |
| 14 | - * 公共返回类 | ||
| 15 | * @param <T> | 14 | * @param <T> |
| 15 | + * @author zhangchuanxi | ||
| 16 | + * 公共返回类 | ||
| 17 | + * @version 1.0.0 | ||
| 16 | */ | 18 | */ |
| 17 | @Data | 19 | @Data |
| 18 | @AllArgsConstructor | 20 | @AllArgsConstructor |
| 19 | @NoArgsConstructor | 21 | @NoArgsConstructor |
| 20 | @ApiModel(value = "RestResult") | 22 | @ApiModel(value = "RestResult") |
| 21 | public class RestResult<T> { | 23 | public class RestResult<T> { |
| 22 | - @ApiModelProperty(value = "返回编码类型",example = "200",allowableValues = "200,400") | 24 | + @ApiModelProperty(value = "返回编码类型", example = "200", allowableValues = "200,400") |
| 23 | private int code; | 25 | private int code; |
| 24 | - @ApiModelProperty(value = "返回提示消息",example = "成功") | 26 | + @ApiModelProperty(value = "返回提示消息", example = "成功") |
| 25 | private String message; | 27 | private String message; |
| 26 | @ApiModelProperty(value = "返回请求数据对象") | 28 | @ApiModelProperty(value = "返回请求数据对象") |
| 27 | private T data; | 29 | private T data; |
| 28 | 30 | ||
| 29 | public String getMessage() { | 31 | public String getMessage() { |
| 30 | - return message != null?message: ResultEnum.getCode(code).getMessage(); | 32 | + return message != null ? message : ResultEnum.getCode(code).getMessage(); |
| 31 | } | 33 | } |
| 32 | - public String toString(){ | 34 | + |
| 35 | + @Override | ||
| 36 | + public String toString() { | ||
| 33 | return JSON.toJSONString(this); | 37 | return JSON.toJSONString(this); |
| 34 | } | 38 | } |
| 35 | } | 39 | } |
src/main/java/com/irrigation/icl/enums/ResultEnum.java
| @@ -3,11 +3,18 @@ package com.irrigation.icl.enums; | @@ -3,11 +3,18 @@ package com.irrigation.icl.enums; | ||
| 3 | import java.util.Arrays; | 3 | import java.util.Arrays; |
| 4 | import java.util.Optional; | 4 | import java.util.Optional; |
| 5 | 5 | ||
| 6 | +/** | ||
| 7 | + * @author zhangchuanxi | ||
| 8 | + * @Description api 返回结果code 类型 | ||
| 9 | + */ | ||
| 10 | + | ||
| 6 | public enum ResultEnum { | 11 | public enum ResultEnum { |
| 7 | 12 | ||
| 8 | - SUCCESS(200, "success"), | ||
| 9 | - SUCCESS_NO_DATA(202,"查询成功,无数据"), | ||
| 10 | - ERROR(400, "failed"), | 13 | + SUCCESS(200, "操作成功"), |
| 14 | + | ||
| 15 | + SUCCESS_NO_DATA(202, "查询成功,无数据"), | ||
| 16 | + | ||
| 17 | + ERROR(400, "操作失败"), | ||
| 11 | 18 | ||
| 12 | PARAMETER_MISMATCH(402, "参数不匹配!"), | 19 | PARAMETER_MISMATCH(402, "参数不匹配!"), |
| 13 | 20 | ||
| @@ -19,14 +26,14 @@ public enum ResultEnum { | @@ -19,14 +26,14 @@ public enum ResultEnum { | ||
| 19 | 26 | ||
| 20 | DATA_OPT(502, "数据库操作异常!"), | 27 | DATA_OPT(502, "数据库操作异常!"), |
| 21 | 28 | ||
| 22 | - REMOTE_CALL_FAILED(503,"远程调用失败"), | 29 | + REMOTE_CALL_FAILED(503, "远程调用失败"), |
| 23 | 30 | ||
| 24 | SESSION_EXPIRE(401, "会话失效,请重新登录!"); | 31 | SESSION_EXPIRE(401, "会话失效,请重新登录!"); |
| 25 | 32 | ||
| 26 | private final int code; | 33 | private final int code; |
| 27 | private final String message; | 34 | private final String message; |
| 28 | 35 | ||
| 29 | - ResultEnum(int code, String message) { | 36 | + ResultEnum(int code, String message) { |
| 30 | this.code = code; | 37 | this.code = code; |
| 31 | this.message = message; | 38 | this.message = message; |
| 32 | } | 39 | } |
src/main/java/com/irrigation/icl/exception/AbstractException.java
| @@ -9,14 +9,19 @@ | @@ -9,14 +9,19 @@ | ||
| 9 | package com.irrigation.icl.exception; | 9 | package com.irrigation.icl.exception; |
| 10 | 10 | ||
| 11 | 11 | ||
| 12 | - | ||
| 13 | import com.irrigation.icl.exception.code.ErrorCode; | 12 | import com.irrigation.icl.exception.code.ErrorCode; |
| 14 | import org.springframework.http.HttpStatus; | 13 | import org.springframework.http.HttpStatus; |
| 15 | 14 | ||
| 16 | import java.io.PrintWriter; | 15 | import java.io.PrintWriter; |
| 17 | import java.io.StringWriter; | 16 | import java.io.StringWriter; |
| 18 | 17 | ||
| 18 | + | ||
| 19 | +/** | ||
| 20 | + * @author zhangchuanxi | ||
| 21 | + * @Description 自定义抽象异常类 | ||
| 22 | + */ | ||
| 19 | public abstract class AbstractException extends RuntimeException { | 23 | public abstract class AbstractException extends RuntimeException { |
| 24 | + | ||
| 20 | private static final long serialVersionUID = 1L; | 25 | private static final long serialVersionUID = 1L; |
| 21 | 26 | ||
| 22 | private final HttpStatus status; | 27 | private final HttpStatus status; |
src/main/java/com/irrigation/icl/exception/ContextRuntimeException.java
| 1 | package com.irrigation.icl.exception; | 1 | package com.irrigation.icl.exception; |
| 2 | 2 | ||
| 3 | +/** | ||
| 4 | + * @author zhangchuanxi | ||
| 5 | + * @Description 自定义异常类 | ||
| 6 | + */ | ||
| 7 | + | ||
| 3 | public class ContextRuntimeException extends RuntimeException { | 8 | public class ContextRuntimeException extends RuntimeException { |
| 4 | 9 | ||
| 5 | public ContextRuntimeException(String message) { | 10 | public ContextRuntimeException(String message) { |
src/main/java/com/irrigation/icl/utils/DateFormatUtils.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | ||
| 2 | + | ||
| 3 | +import java.text.SimpleDateFormat; | ||
| 4 | +import java.util.Locale; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * @author zhangchuanxi | ||
| 8 | + * SimpleDateFormat 工具类 | ||
| 9 | + * @version 1.0.1 | ||
| 10 | + */ | ||
| 11 | +public class DateFormatUtils { | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + public static SimpleDateFormat getInstance(String format) { | ||
| 15 | + return new SimpleDateFormat(format); | ||
| 16 | + } | ||
| 17 | + | ||
| 18 | + public static SimpleDateFormat getInstance(String format, Locale locale) { | ||
| 19 | + return new SimpleDateFormat(format, locale); | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 标准日期格式 yyyy-MM-dd | ||
| 24 | + */ | ||
| 25 | + public final static SimpleDateFormat NORM_DATE_FORMAT = new SimpleDateFormat(DatePatternUtils.NORM_DATE_PATTERN); | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * 标准时间格式 :HH:mm:ss | ||
| 29 | + */ | ||
| 30 | + public final static SimpleDateFormat NORM_TIME_FORMAT = new SimpleDateFormat(DatePatternUtils.NORM_TIME_PATTERN); | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * 标准日期时间格式,精确到分 :yyyy-MM-dd HH:mm | ||
| 34 | + */ | ||
| 35 | + public final static SimpleDateFormat NORM_DATETIME_MINUTE_FORMAT = new SimpleDateFormat(DatePatternUtils.NORM_DATETIME_MINUTE_PATTERN); | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss | ||
| 39 | + */ | ||
| 40 | + public final static String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; | ||
| 41 | + /** | ||
| 42 | + * 标准日期时间格式,精确到秒 :yyyy-MM-dd HH:mm:ss | ||
| 43 | + */ | ||
| 44 | + public final static SimpleDateFormat NORM_DATETIME_FORMAT = new SimpleDateFormat(DatePatternUtils.NORM_DATETIME_PATTERN); | ||
| 45 | + | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * 标准日期时间格式,精确到毫秒 :yyyy-MM-dd HH:mm:ss.SSS | ||
| 49 | + */ | ||
| 50 | + public final static SimpleDateFormat NORM_DATETIME_MS_FORMAT = new SimpleDateFormat(DatePatternUtils.NORM_DATETIME_MS_PATTERN); | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * 标准日期格式 :yyyy年MM月dd日 | ||
| 54 | + */ | ||
| 55 | + public final static SimpleDateFormat CHINESE_DATE_FORMAT = new SimpleDateFormat(DatePatternUtils.CHINESE_DATE_PATTERN); | ||
| 56 | + | ||
| 57 | + /** | ||
| 58 | + * 标准日期格式 :yyyyMMdd | ||
| 59 | + */ | ||
| 60 | + public final static SimpleDateFormat PURE_DATE_FORMAT = new SimpleDateFormat(DatePatternUtils.PURE_DATE_PATTERN); | ||
| 61 | + | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * 标准日期格式 :HHmmss | ||
| 65 | + */ | ||
| 66 | + public final static SimpleDateFormat PURE_TIME_FORMAT = new SimpleDateFormat(DatePatternUtils.PURE_TIME_PATTERN); | ||
| 67 | + | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * 标准日期格式 :yyyyMMddHHmmss | ||
| 71 | + */ | ||
| 72 | + public final static SimpleDateFormat PURE_DATETIME_FORMAT = new SimpleDateFormat(DatePatternUtils.PURE_DATETIME_PATTERN); | ||
| 73 | + | ||
| 74 | + | ||
| 75 | + /** | ||
| 76 | + * 标准日期格式 :yyyyMMddHHmmssSSS | ||
| 77 | + */ | ||
| 78 | + public final static SimpleDateFormat PURE_DATETIME_MS_FORMAT = new SimpleDateFormat(DatePatternUtils.PURE_DATETIME_MS_PATTERN); | ||
| 79 | + | ||
| 80 | + | ||
| 81 | +} |
src/main/java/com/irrigation/icl/utils/DatePatternUtils.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +/** | ||
| 5 | + * @author zhangchuanxi | ||
| 6 | + * 日期格式化样式 | ||
| 7 | + * @version 1.0.1 | ||
| 8 | + * <p> | ||
| 9 | + * 格式描述: | ||
| 10 | + * 年yyyy | ||
| 11 | + * 月MM | ||
| 12 | + * 日dd | ||
| 13 | + * 时HH | ||
| 14 | + * 分mm | ||
| 15 | + * 秒ss | ||
| 16 | + * 毫秒SS | ||
| 17 | + */ | ||
| 18 | +public class DatePatternUtils { | ||
| 19 | + | ||
| 20 | + private DatePatternUtils() { | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + | ||
| 24 | + //-------------------------------------------------------------------------------------------------------------------------------- Normal | ||
| 25 | + | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * 标准日期格式:yyyy-MM-dd | ||
| 29 | + */ | ||
| 30 | + public final static String NORM_YM_PATTERN = "yyyy-MM"; | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * 标准日期格式:yyyy-MM-dd | ||
| 34 | + */ | ||
| 35 | + public final static String NORM_DATE_PATTERN = "yyyy-MM-dd"; | ||
| 36 | + /** | ||
| 37 | + * 标准时间格式:HH:mm:ss | ||
| 38 | + */ | ||
| 39 | + public final static String NORM_TIME_PATTERN = "HH:mm:ss"; | ||
| 40 | + | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * 标准日期时间格式,精确到分:yyyy-MM-dd HH:mm | ||
| 44 | + */ | ||
| 45 | + public final static String NORM_DATETIME_MINUTE_PATTERN = "yyyy-MM-dd HH:mm"; | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * 标准日期时间格式,精确到秒:yyyy-MM-dd HH:mm:ss | ||
| 49 | + */ | ||
| 50 | + public final static String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; | ||
| 51 | + | ||
| 52 | + | ||
| 53 | + /** | ||
| 54 | + * 标准日期时间格式,精确到毫秒:yyyy-MM-dd HH:mm:ss.SSS | ||
| 55 | + */ | ||
| 56 | + public final static String NORM_DATETIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS"; | ||
| 57 | + | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * 标准日期格式:yyyy年MM月dd日 | ||
| 61 | + */ | ||
| 62 | + public final static String CHINESE_DATE_PATTERN = "yyyy年MM月dd日"; | ||
| 63 | + | ||
| 64 | + | ||
| 65 | + //-------------------------------------------------------------------------------------------------------------------------------- Pure | ||
| 66 | + /** | ||
| 67 | + * 标准日期格式:yyyyMMdd | ||
| 68 | + */ | ||
| 69 | + public final static String PURE_DATE_PATTERN = "yyyyMMdd"; | ||
| 70 | + | ||
| 71 | + | ||
| 72 | + /** | ||
| 73 | + * 标准日期格式:HHmmss | ||
| 74 | + */ | ||
| 75 | + public final static String PURE_TIME_PATTERN = "HHmmss"; | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + /** | ||
| 79 | + * 标准日期格式:yyyyMMddHHmmss | ||
| 80 | + */ | ||
| 81 | + public final static String PURE_DATETIME_PATTERN = "yyyyMMddHHmmss"; | ||
| 82 | + | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * 标准日期格式:yyyyMMddHHmmssSSS | ||
| 86 | + */ | ||
| 87 | + public final static String PURE_DATETIME_MS_PATTERN = "yyyyMMddHHmmssSSS"; | ||
| 88 | + | ||
| 89 | + | ||
| 90 | + /** | ||
| 91 | + * yyyy/MM/dd" | ||
| 92 | + */ | ||
| 93 | + public final static String EXTRA_DATE_PATTERN = "yyyy/MM/dd"; | ||
| 94 | + | ||
| 95 | + /** | ||
| 96 | + * yyyy/MM/dd HH:mm | ||
| 97 | + */ | ||
| 98 | + public final static String EXTRA_DATETIME_MINUTE_PATTERN = "yyyy/MM/dd HH:mm"; | ||
| 99 | + | ||
| 100 | + /** | ||
| 101 | + * yyyy/MM/dd HH:mm:ss | ||
| 102 | + */ | ||
| 103 | + public final static String EXTRA_DATETIME_PATTERN = "yyyy/MM/dd HH:mm:ss"; | ||
| 104 | + | ||
| 105 | + | ||
| 106 | + // //-------------------------------------------------------------------------------------------------------------------------------- Others | ||
| 107 | +// /** | ||
| 108 | +// * HTTP头中日期时间格式:EEE, dd MMM yyyy HH:mm:ss z | ||
| 109 | +// */ | ||
| 110 | +// public final static String HTTP_DATETIME_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z"; | ||
| 111 | +// | ||
| 112 | +// | ||
| 113 | +// /** | ||
| 114 | +// * JDK中日期时间格式:EEE MMM dd HH:mm:ss zzz yyyy | ||
| 115 | +// */ | ||
| 116 | +// public final static String JDK_DATETIME_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy"; | ||
| 117 | +// | ||
| 118 | +// | ||
| 119 | +// /** | ||
| 120 | +// * UTC时间:yyyy-MM-dd'T'HH:mm:ss'Z' | ||
| 121 | +// */ | ||
| 122 | +// public final static String UTC_PATTERN = "yyyy-MM-dd'T'HH:mm:ss'Z'"; | ||
| 123 | +// | ||
| 124 | +// | ||
| 125 | +// /** | ||
| 126 | +// * UTC时间:yyyy-MM-dd'T'HH:mm:ssZ | ||
| 127 | +// */ | ||
| 128 | +// public final static String UTC_WITH_ZONE_OFFSET_PATTERN = "yyyy-MM-dd'T'HH:mm:ssZ"; | ||
| 129 | +// | ||
| 130 | +// | ||
| 131 | +// /** | ||
| 132 | +// * UTC时间:yyyy-MM-dd'T'HH:mm:ss.SSS'Z' | ||
| 133 | +// */ | ||
| 134 | +// public final static String UTC_MS_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; | ||
| 135 | +// | ||
| 136 | +// /** | ||
| 137 | +// * UTC时间:yyyy-MM-dd'T'HH:mm:ssZ | ||
| 138 | +// */ | ||
| 139 | +// public final static String UTC_MS_WITH_ZONE_OFFSET_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"; | ||
| 140 | + | ||
| 141 | +} | ||
| 0 | \ No newline at end of file | 142 | \ No newline at end of file |
src/main/java/com/irrigation/icl/utils/DateUtils.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | ||
| 2 | + | ||
| 3 | +import java.text.ParseException; | ||
| 4 | +import java.time.*; | ||
| 5 | +import java.time.format.DateTimeFormatter; | ||
| 6 | +import java.time.temporal.ChronoUnit; | ||
| 7 | +import java.util.Calendar; | ||
| 8 | +import java.util.Date; | ||
| 9 | +import java.util.Locale; | ||
| 10 | +import java.util.TimeZone; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * 日期工具类 | ||
| 14 | + * | ||
| 15 | + * @version 1.0.1 | ||
| 16 | + */ | ||
| 17 | +public class DateUtils { | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * 当前时间,转换为{@link Date}对象 | ||
| 22 | + * | ||
| 23 | + * @return 当前时间 | ||
| 24 | + */ | ||
| 25 | + public static Date date() { | ||
| 26 | + return date(currentTimeMillis()); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * 获取时间 | ||
| 32 | + * | ||
| 33 | + * @param date 时间戳 | ||
| 34 | + * @return | ||
| 35 | + */ | ||
| 36 | + public static Date date(Long date) { | ||
| 37 | + return new Date(date); | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * 获取指定某一天的开始时间戳 | ||
| 42 | + * | ||
| 43 | + * @param date | ||
| 44 | + * @return | ||
| 45 | + */ | ||
| 46 | + public static Date getDailyStartTime(Date date) { | ||
| 47 | + Calendar calendar = calendar(date); | ||
| 48 | + calendar.set(Calendar.HOUR_OF_DAY, 0); | ||
| 49 | + calendar.set(Calendar.SECOND, 0); | ||
| 50 | + calendar.set(Calendar.MINUTE, 0); | ||
| 51 | + calendar.set(Calendar.MILLISECOND, 0); | ||
| 52 | + return calendar.getTime(); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * 获取指定某一天的结束时间戳 | ||
| 57 | + * | ||
| 58 | + * @param date | ||
| 59 | + * @return | ||
| 60 | + */ | ||
| 61 | + public static Date getDailyEndTime(Date date) { | ||
| 62 | + Calendar calendar = calendar(date); | ||
| 63 | + calendar.set(Calendar.HOUR_OF_DAY, 23); | ||
| 64 | + calendar.set(Calendar.MINUTE, 59); | ||
| 65 | + calendar.set(Calendar.SECOND, 59); | ||
| 66 | + calendar.set(Calendar.MILLISECOND, 999); | ||
| 67 | + return calendar.getTime(); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + /** | ||
| 71 | + * 日期加减 | ||
| 72 | + * | ||
| 73 | + * @param date | ||
| 74 | + * @param field 对应 Calendar 常量 | ||
| 75 | + * @param amount | ||
| 76 | + * @return | ||
| 77 | + */ | ||
| 78 | + public static Date dateFieldAdd(Date date, int field, int amount) { | ||
| 79 | + Calendar calendar = calendar(date); | ||
| 80 | + calendar.add(field, amount); | ||
| 81 | + return calendar.getTime(); | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + | ||
| 85 | + /** | ||
| 86 | + * 创建Calendar对象,时间为默认时区的当前时间 | ||
| 87 | + * | ||
| 88 | + * @return Calendar对象 | ||
| 89 | + * @since 4.6.6 | ||
| 90 | + */ | ||
| 91 | + public static Calendar calendar() { | ||
| 92 | + return Calendar.getInstance(); | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + /** | ||
| 96 | + * 转换为Calendar对象 | ||
| 97 | + * | ||
| 98 | + * @param date 日期对象 | ||
| 99 | + * @return Calendar对象 | ||
| 100 | + */ | ||
| 101 | + public static Calendar calendar(Date date) { | ||
| 102 | + return calendar(date.getTime()); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + /** | ||
| 106 | + * 转换为Calendar对象 | ||
| 107 | + * | ||
| 108 | + * @param millis 时间戳 | ||
| 109 | + * @return Calendar对象 | ||
| 110 | + */ | ||
| 111 | + public static Calendar calendar(long millis) { | ||
| 112 | + final Calendar cal = calendar(); | ||
| 113 | + cal.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); | ||
| 114 | + cal.setTimeInMillis(millis); | ||
| 115 | + return cal; | ||
| 116 | + } | ||
| 117 | + | ||
| 118 | + private static Calendar toCalendar(Date date) { | ||
| 119 | + return toCalendar(TimeZone.getDefault(), Locale.getDefault(Locale.Category.FORMAT), date); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + | ||
| 123 | + private static Calendar toCalendar(TimeZone zone, Locale locale, Date date) { | ||
| 124 | + if (null == locale) { | ||
| 125 | + locale = Locale.getDefault(Locale.Category.FORMAT); | ||
| 126 | + } | ||
| 127 | + final Calendar cal = (null != zone) ? Calendar.getInstance(zone, locale) : Calendar.getInstance(locale); | ||
| 128 | + cal.setFirstDayOfWeek(Calendar.MONDAY); | ||
| 129 | + cal.setTime(date); | ||
| 130 | + return cal; | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + /** | ||
| 134 | + * 当前时间的时间戳 | ||
| 135 | + * | ||
| 136 | + * @return 时间 | ||
| 137 | + */ | ||
| 138 | + public static long currentTimeMillis() { | ||
| 139 | + return System.currentTimeMillis(); | ||
| 140 | + } | ||
| 141 | + | ||
| 142 | + | ||
| 143 | + /** | ||
| 144 | + * 当前时间的时间戳(秒) | ||
| 145 | + * | ||
| 146 | + * @return 当前时间秒数 | ||
| 147 | + */ | ||
| 148 | + public static long currentSeconds() { | ||
| 149 | + return System.currentTimeMillis() / 1000; | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + /** | ||
| 153 | + * 获得年的部分 | ||
| 154 | + * | ||
| 155 | + * @param date 日期 | ||
| 156 | + * @return 年的部分 | ||
| 157 | + */ | ||
| 158 | + public static int year(Date date) { | ||
| 159 | + return toCalendar(date).get(Calendar.YEAR); | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + | ||
| 163 | + /** | ||
| 164 | + * 获得指定日期所属季度,从1开始计数 | ||
| 165 | + * | ||
| 166 | + * @param date 日期 | ||
| 167 | + * @return 第几个季度 | ||
| 168 | + * @since 4.1.0 | ||
| 169 | + */ | ||
| 170 | + public static int quarter(Date date) { | ||
| 171 | + return month(date) / 3 + 1; | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + | ||
| 175 | + /** | ||
| 176 | + * 获得月份,从0开始计数 | ||
| 177 | + * | ||
| 178 | + * @param date 日期 | ||
| 179 | + * @return 月份,从0开始计数 | ||
| 180 | + */ | ||
| 181 | + public static int month(Date date) { | ||
| 182 | + return toCalendar(date).get(Calendar.MONTH); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + | ||
| 186 | + /** | ||
| 187 | + * 获得指定日期是所在年份的第几周<br> | ||
| 188 | + * | ||
| 189 | + * @param date 日期 | ||
| 190 | + * @return 周 | ||
| 191 | + */ | ||
| 192 | + public static int weekOfYear(Date date) { | ||
| 193 | + return toCalendar(date).get(Calendar.WEEK_OF_YEAR); | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + /** | ||
| 197 | + * 获得指定日期是所在月份的第几周<br> | ||
| 198 | + * | ||
| 199 | + * @param date 日期 | ||
| 200 | + * @return 周 | ||
| 201 | + */ | ||
| 202 | + public static int weekOfMonth(Date date) { | ||
| 203 | + return toCalendar(date).get(Calendar.WEEK_OF_MONTH); | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | + /** | ||
| 207 | + * 获得指定日期是这个日期所在月份的第几天<br> | ||
| 208 | + * | ||
| 209 | + * @param date 日期 | ||
| 210 | + * @return 天 | ||
| 211 | + */ | ||
| 212 | + public static int dayOfMonth(Date date) { | ||
| 213 | + return toCalendar(date).get(Calendar.DAY_OF_MONTH); | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + /** | ||
| 217 | + * 获得指定日期是星期几,1表示周日,2表示周一 | ||
| 218 | + * | ||
| 219 | + * @param date 日期 | ||
| 220 | + * @return 天 | ||
| 221 | + */ | ||
| 222 | + public static int dayOfWeek(Date date) { | ||
| 223 | + return toCalendar(date).get(Calendar.DAY_OF_WEEK); | ||
| 224 | + | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + /** | ||
| 228 | + * 获得指定日期的小时数部分<br> | ||
| 229 | + * | ||
| 230 | + * @param date 日期 | ||
| 231 | + * @return 小时数 | ||
| 232 | + */ | ||
| 233 | + public static int hour(Date date) { | ||
| 234 | + return toCalendar(date).get(Calendar.HOUR_OF_DAY); | ||
| 235 | + } | ||
| 236 | + | ||
| 237 | + /** | ||
| 238 | + * 获得指定日期的分钟数部分<br> | ||
| 239 | + * 例如:10:04:15.250 =》 4 | ||
| 240 | + * | ||
| 241 | + * @param date 日期 | ||
| 242 | + * @return 分钟数 | ||
| 243 | + */ | ||
| 244 | + public static int minute(Date date) { | ||
| 245 | + return toCalendar(date).get(Calendar.MINUTE); | ||
| 246 | + | ||
| 247 | + } | ||
| 248 | + | ||
| 249 | + /** | ||
| 250 | + * 获得指定日期的秒数部分<br> | ||
| 251 | + * | ||
| 252 | + * @param date 日期 | ||
| 253 | + * @return 秒数 | ||
| 254 | + */ | ||
| 255 | + public static int second(Date date) { | ||
| 256 | + return toCalendar(date).get(Calendar.SECOND); | ||
| 257 | + } | ||
| 258 | + | ||
| 259 | + /** | ||
| 260 | + * 获得指定日期的毫秒数部分<br> | ||
| 261 | + * | ||
| 262 | + * @param date 日期 | ||
| 263 | + * @return 毫秒数 | ||
| 264 | + */ | ||
| 265 | + public static int millisecond(Date date) { | ||
| 266 | + return toCalendar(date).get(Calendar.MILLISECOND); | ||
| 267 | + | ||
| 268 | + } | ||
| 269 | + | ||
| 270 | + /** | ||
| 271 | + * 当前时间,格式 yyyy-MM-dd HH:mm:ss | ||
| 272 | + * | ||
| 273 | + * @return 当前时间的标准形式字符串 | ||
| 274 | + */ | ||
| 275 | + public static String now() { | ||
| 276 | + return formatDateTime(new Date()); | ||
| 277 | + } | ||
| 278 | + | ||
| 279 | + /** | ||
| 280 | + * 当前日期,格式 yyyy-MM-dd | ||
| 281 | + * | ||
| 282 | + * @return 当前日期的标准形式字符串 | ||
| 283 | + */ | ||
| 284 | + public static String today() { | ||
| 285 | + return formatDate(new Date()); | ||
| 286 | + } | ||
| 287 | + | ||
| 288 | + // ------------------------------------ Format start ---------------------------------------------- | ||
| 289 | + | ||
| 290 | + | ||
| 291 | + /** | ||
| 292 | + * 根据特定格式格式化日期 | ||
| 293 | + * | ||
| 294 | + * @param date 被格式化的日期 | ||
| 295 | + * @param pattern 日期格式,常用格式见: | ||
| 296 | + * @return 格式化后的字符串 | ||
| 297 | + * @see DatePatternUtils | ||
| 298 | + */ | ||
| 299 | + public static String format(Date date, String pattern) { | ||
| 300 | + if (null == date || ObjectUtils.stringIsEmpty(pattern)) { | ||
| 301 | + return null; | ||
| 302 | + } | ||
| 303 | + return DateFormatUtils.getInstance(pattern).format(date); | ||
| 304 | + } | ||
| 305 | + | ||
| 306 | + /** | ||
| 307 | + * 格式化日期时间<br> | ||
| 308 | + * 格式 yyyy-MM-dd HH:mm:ss | ||
| 309 | + * | ||
| 310 | + * @param date 被格式化的日期 | ||
| 311 | + * @return 格式化后的日期 | ||
| 312 | + */ | ||
| 313 | + public static String formatDateTime(Date date) { | ||
| 314 | + if (null == date) { | ||
| 315 | + return null; | ||
| 316 | + } | ||
| 317 | + return DateFormatUtils.NORM_DATETIME_FORMAT.format(date); | ||
| 318 | + } | ||
| 319 | + | ||
| 320 | + /** | ||
| 321 | + * 格式化日期部分(不包括时间)<br> | ||
| 322 | + * 格式 yyyy-MM-dd | ||
| 323 | + * | ||
| 324 | + * @param date 被格式化的日期 | ||
| 325 | + * @return 格式化后的字符串 | ||
| 326 | + */ | ||
| 327 | + public static String formatDate(Date date) { | ||
| 328 | + if (null == date) { | ||
| 329 | + return null; | ||
| 330 | + } | ||
| 331 | + return DateFormatUtils.NORM_DATE_FORMAT.format(date); | ||
| 332 | + } | ||
| 333 | + | ||
| 334 | + /** | ||
| 335 | + * 格式化时间<br> | ||
| 336 | + * 格式 HH:mm:ss | ||
| 337 | + * | ||
| 338 | + * @param date 被格式化的日期 | ||
| 339 | + * @return 格式化后的字符串 | ||
| 340 | + * @since 3.0.1 | ||
| 341 | + */ | ||
| 342 | + public static String formatTime(Date date) { | ||
| 343 | + if (null == date) { | ||
| 344 | + return null; | ||
| 345 | + } | ||
| 346 | + return DateFormatUtils.NORM_TIME_FORMAT.format(date); | ||
| 347 | + } | ||
| 348 | + | ||
| 349 | + /** | ||
| 350 | + * 格式化给定样式 | ||
| 351 | + * | ||
| 352 | + * @param timestamp | ||
| 353 | + * @param pattern | ||
| 354 | + * @return | ||
| 355 | + * @see DatePatternUtils | ||
| 356 | + */ | ||
| 357 | + public static String format(Long timestamp, String pattern) { | ||
| 358 | + if (ObjectUtils.isNull(timestamp) || ObjectUtils.isNull(pattern)) { | ||
| 359 | + return null; | ||
| 360 | + } | ||
| 361 | + return DateFormatUtils.getInstance(pattern).format(date(timestamp)); | ||
| 362 | + } | ||
| 363 | + | ||
| 364 | + // ------------------------------------ Format end ---------------------------------------------- | ||
| 365 | + | ||
| 366 | + // ------------------------------------ Parse start ---------------------------------------------- | ||
| 367 | + | ||
| 368 | + | ||
| 369 | + /** | ||
| 370 | + * 构建DateTime对象 | ||
| 371 | + * | ||
| 372 | + * @param dateStr Date字符串 | ||
| 373 | + * @param pattern 格式 | ||
| 374 | + * @return DateTime对象 | ||
| 375 | + * @see DatePatternUtils | ||
| 376 | + */ | ||
| 377 | + public static Date parse(String dateStr, String pattern) { | ||
| 378 | + try { | ||
| 379 | + return DateFormatUtils.getInstance(pattern).parse(dateStr); | ||
| 380 | + } catch (ParseException e) { | ||
| 381 | + e.printStackTrace(); | ||
| 382 | + } | ||
| 383 | + return null; | ||
| 384 | + } | ||
| 385 | + | ||
| 386 | + /** | ||
| 387 | + * 将特定格式的日期转换为Date对象 | ||
| 388 | + * | ||
| 389 | + * @param dateStr 特定格式的日期 | ||
| 390 | + * @param pattern 格式,例如yyyy-MM-dd | ||
| 391 | + * @param locale 区域信息 | ||
| 392 | + * @return 日期对象 | ||
| 393 | + * @see DatePatternUtils | ||
| 394 | + * @since 4.5.18 | ||
| 395 | + */ | ||
| 396 | + public static Date parse(String dateStr, String pattern, Locale locale) { | ||
| 397 | + try { | ||
| 398 | + return DateFormatUtils.getInstance(pattern, locale).parse(dateStr); | ||
| 399 | + } catch (ParseException e) { | ||
| 400 | + e.printStackTrace(); | ||
| 401 | + } | ||
| 402 | + return null; | ||
| 403 | + } | ||
| 404 | + | ||
| 405 | + /** | ||
| 406 | + * 格式yyyy-MM-dd HH:mm:ss | ||
| 407 | + * | ||
| 408 | + * @param dateString 标准形式的时间字符串 | ||
| 409 | + * @return 日期对象 | ||
| 410 | + */ | ||
| 411 | + public static Date parseDateTime(String dateString) { | ||
| 412 | + return parse(dateString, DatePatternUtils.NORM_DATETIME_PATTERN); | ||
| 413 | + } | ||
| 414 | + | ||
| 415 | + /** | ||
| 416 | + * 解析格式为yyyy-MM-dd的日期,忽略时分秒 | ||
| 417 | + * | ||
| 418 | + * @param dateString 标准形式的日期字符串 | ||
| 419 | + * @return 日期对象 | ||
| 420 | + */ | ||
| 421 | + public static Date parseDate(String dateString) { | ||
| 422 | + return parse(dateString, DatePatternUtils.NORM_DATE_PATTERN); | ||
| 423 | + } | ||
| 424 | + | ||
| 425 | + /** | ||
| 426 | + * 解析时间,格式HH:mm:ss,日期部分默认为1970-01-01 | ||
| 427 | + * | ||
| 428 | + * @param timeString 标准形式的日期字符串 | ||
| 429 | + * @return 日期对象 | ||
| 430 | + */ | ||
| 431 | + public static Date parseTime(String timeString) { | ||
| 432 | + return parse(timeString, DatePatternUtils.NORM_TIME_PATTERN); | ||
| 433 | + } | ||
| 434 | + | ||
| 435 | + // ------------------------------------ Parse end ---------------------------------------------- | ||
| 436 | + | ||
| 437 | + /** | ||
| 438 | + * date 转换为 long | ||
| 439 | + * | ||
| 440 | + * @param date | ||
| 441 | + * @return | ||
| 442 | + */ | ||
| 443 | + public static long dateToLong(Date date) { | ||
| 444 | + return ObjectUtils.isNull(date) ? 0L : date.getTime(); | ||
| 445 | + } | ||
| 446 | + | ||
| 447 | + | ||
| 448 | + /** | ||
| 449 | + * 时间字符串转换long | ||
| 450 | + * | ||
| 451 | + * @param dateStr | ||
| 452 | + * @param pattern | ||
| 453 | + * @return | ||
| 454 | + * @see DatePatternUtils | ||
| 455 | + */ | ||
| 456 | + public static long dateToLong(String dateStr, String pattern) { | ||
| 457 | + if (ObjectUtils.isNull(dateStr) || ObjectUtils.isNull(pattern)) { | ||
| 458 | + return 0L; | ||
| 459 | + } | ||
| 460 | + return parse(dateStr, pattern).getTime(); | ||
| 461 | + } | ||
| 462 | + | ||
| 463 | +//---------------------new api | ||
| 464 | + | ||
| 465 | + | ||
| 466 | + /** | ||
| 467 | + * 获取指定天的北京时间戳为00:00:00的日期 | ||
| 468 | + * | ||
| 469 | + * @param year | ||
| 470 | + * @param month | ||
| 471 | + * @param day | ||
| 472 | + * @return | ||
| 473 | + */ | ||
| 474 | + public static Date getStartTimeOfDay(int year, int month, int day) { | ||
| 475 | + LocalDate localDate = LocalDate.of(year, month, day); | ||
| 476 | + Instant instant = localDate.atStartOfDay(ZoneId.of("Asia/Shanghai")).toInstant(); | ||
| 477 | + return Date.from(instant); | ||
| 478 | + } | ||
| 479 | + | ||
| 480 | + /** | ||
| 481 | + * 获取某月中第一天北京时间戳为00:00:00的日期 | ||
| 482 | + * | ||
| 483 | + * @param year | ||
| 484 | + * @param month | ||
| 485 | + * @return | ||
| 486 | + */ | ||
| 487 | + public static Date getStartDateOfMonth(int year, int month) { | ||
| 488 | + return getStartTimeOfDay(year, month, 1); | ||
| 489 | + } | ||
| 490 | + | ||
| 491 | + /** | ||
| 492 | + * 获取某月中最后一天北京时间为23:59:59.999的日期 | ||
| 493 | + * | ||
| 494 | + * @param year | ||
| 495 | + * @param month | ||
| 496 | + * @return | ||
| 497 | + */ | ||
| 498 | + public static Date getEndDateOfMonth(int year, int month) { | ||
| 499 | + YearMonth yearMonth = YearMonth.of(year, month); | ||
| 500 | + LocalDate endOfMonth = yearMonth.atEndOfMonth(); | ||
| 501 | + LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999); | ||
| 502 | + ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai")); | ||
| 503 | + return Date.from(zonedDateTime.toInstant()); | ||
| 504 | + } | ||
| 505 | + | ||
| 506 | + /** | ||
| 507 | + * 获得指定年北京时间0点的开始时间戳 | ||
| 508 | + * | ||
| 509 | + * @param year | ||
| 510 | + * @return | ||
| 511 | + */ | ||
| 512 | + public static long getYearStartMilliseconds(Integer year) { | ||
| 513 | + return LocalDateTime.of(year, 1, 1, 0, 0, 0, 0).toInstant(ZoneOffset.of("+8")).toEpochMilli(); | ||
| 514 | + } | ||
| 515 | + | ||
| 516 | + /** | ||
| 517 | + * 获得指定年北京时间最后一毫秒的时间戳 | ||
| 518 | + * | ||
| 519 | + * @param year | ||
| 520 | + * @return | ||
| 521 | + */ | ||
| 522 | + public static long getYearEndMilliseconds(Integer year) { | ||
| 523 | + return getYearStartMilliseconds(year + 1) - 1; | ||
| 524 | + } | ||
| 525 | + | ||
| 526 | + /** | ||
| 527 | + * 获得指定日期北京时间0点的时间戳 | ||
| 528 | + * | ||
| 529 | + * @param year | ||
| 530 | + * @param month | ||
| 531 | + * @param day | ||
| 532 | + * @return | ||
| 533 | + */ | ||
| 534 | + public static long getDayStartMilliseconds(Integer year, Integer month, Integer day) { | ||
| 535 | + return LocalDateTime.of(year, month, day, 0, 0, 0, 0).toInstant(ZoneOffset.of("+8")).toEpochMilli(); | ||
| 536 | + } | ||
| 537 | + | ||
| 538 | + /** | ||
| 539 | + * 获取指定日期北京时间23:59:59.999的时间戳 | ||
| 540 | + * | ||
| 541 | + * @param year | ||
| 542 | + * @param month | ||
| 543 | + * @param day | ||
| 544 | + * @return | ||
| 545 | + */ | ||
| 546 | + public static long getDayEndMilliseconds(Integer year, Integer month, Integer day) { | ||
| 547 | + return LocalDateTime.of(year, month, day, 23, 59, 59, 999).toInstant(ZoneOffset.of("+8")).toEpochMilli(); | ||
| 548 | + } | ||
| 549 | + | ||
| 550 | + /** | ||
| 551 | + * 获取传入时间戳所指向日期北京时间0点的时间戳 | ||
| 552 | + * | ||
| 553 | + * @param timestamp | ||
| 554 | + * @return | ||
| 555 | + */ | ||
| 556 | + public static long getDayStartTimeStamp(Long timestamp) { | ||
| 557 | + LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8)); | ||
| 558 | + LocalDate localDate = time2.toLocalDate(); | ||
| 559 | + return localDate.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli(); | ||
| 560 | + } | ||
| 561 | + | ||
| 562 | + /** | ||
| 563 | + * 获取传入时间戳所指向日期北京时间23:59:59.999的时间戳 | ||
| 564 | + * | ||
| 565 | + * @param timestamp | ||
| 566 | + * @return | ||
| 567 | + */ | ||
| 568 | + public static long getDayEndTimeStamp(Long timestamp) { | ||
| 569 | + LocalDateTime time2 = LocalDateTime.ofEpochSecond(timestamp / 1000, 0, ZoneOffset.ofHours(8)).plusDays(1L); | ||
| 570 | + LocalDate localDate = time2.toLocalDate(); | ||
| 571 | + return localDate.atStartOfDay().toInstant(ZoneOffset.of("+8")).toEpochMilli() - 1L; | ||
| 572 | + } | ||
| 573 | + | ||
| 574 | + | ||
| 575 | + /** | ||
| 576 | + * 获取指定月北京时间1月1日0点的时间戳 | ||
| 577 | + * | ||
| 578 | + * @param month | ||
| 579 | + * @return | ||
| 580 | + */ | ||
| 581 | + public static long getStartTimestampOfMonth(int month) { | ||
| 582 | + YearMonth yearMonth = YearMonth.now(ZoneId.of("Asia/Shanghai")); | ||
| 583 | + return getDayStartMilliseconds(yearMonth.getYear(), month, 1); | ||
| 584 | + } | ||
| 585 | + | ||
| 586 | + /** | ||
| 587 | + * 获取当年一月的格式化字符串 yyyy-MM | ||
| 588 | + * | ||
| 589 | + * @return | ||
| 590 | + */ | ||
| 591 | + public static String getJanuary() { | ||
| 592 | + YearMonth yearMonth = YearMonth.now().withMonth(1); | ||
| 593 | + return yearMonth.format(DateTimeFormatter.ofPattern(DatePatternUtils.NORM_YM_PATTERN)); | ||
| 594 | + } | ||
| 595 | + | ||
| 596 | + /** | ||
| 597 | + * 获取当月格式化字符串 yyyy-MM | ||
| 598 | + * | ||
| 599 | + * @return | ||
| 600 | + */ | ||
| 601 | + public static String getCurrentMonth() { | ||
| 602 | + LocalDate now = LocalDate.now(ZoneId.of("Asia/Shanghai")); | ||
| 603 | + return now.format(DateTimeFormatter.ofPattern(DatePatternUtils.NORM_YM_PATTERN)); | ||
| 604 | + } | ||
| 605 | + | ||
| 606 | + /** | ||
| 607 | + * 通过时间戳获取前一日北京日期 | ||
| 608 | + * | ||
| 609 | + * @param timestamp | ||
| 610 | + * @return | ||
| 611 | + */ | ||
| 612 | + public static Date yesterday(Long timestamp) { | ||
| 613 | + LocalDateTime time2 = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.of("Asia/Shanghai")).minusDays(1L); | ||
| 614 | + return Date.from(time2.toInstant(ZoneOffset.ofHours(8))); | ||
| 615 | + } | ||
| 616 | + | ||
| 617 | + /** | ||
| 618 | + * 将时间戳转换为北京日期 | ||
| 619 | + * | ||
| 620 | + * @param timestamp | ||
| 621 | + * @return | ||
| 622 | + */ | ||
| 623 | + public static Date convertTimestampToDate(Long timestamp) { | ||
| 624 | + Instant instant = Instant.ofEpochMilli(timestamp).atOffset(ZoneOffset.ofHours(8)).toInstant(); | ||
| 625 | + return Date.from(instant); | ||
| 626 | + | ||
| 627 | + } | ||
| 628 | + | ||
| 629 | + /** | ||
| 630 | + * 获取timestamp+8小时的日期,专用于MongoDB查询 | ||
| 631 | + * | ||
| 632 | + * @param timestamp | ||
| 633 | + * @return | ||
| 634 | + */ | ||
| 635 | + public static Date getYesterdayUtcDate(Long timestamp) { | ||
| 636 | + Instant instant = Instant.ofEpochMilli(timestamp).plus(8L, ChronoUnit.HOURS).minus(1L, ChronoUnit.DAYS); | ||
| 637 | + return Date.from(instant); | ||
| 638 | + } | ||
| 639 | + | ||
| 640 | + | ||
| 641 | + /** | ||
| 642 | + * 获取+8小时时间戳的日期,专用于MongoDB查询 | ||
| 643 | + * | ||
| 644 | + * @param timestamp | ||
| 645 | + * @return | ||
| 646 | + */ | ||
| 647 | + public static Date convertTimestampToUtcDate(Long timestamp) { | ||
| 648 | + Instant instant = Instant.ofEpochMilli(timestamp).plus(8, ChronoUnit.HOURS); | ||
| 649 | + return Date.from(instant); | ||
| 650 | + } | ||
| 651 | + | ||
| 652 | + /** | ||
| 653 | + * 获取日期 | ||
| 654 | + * | ||
| 655 | + * @return 如:2014-12-10 | ||
| 656 | + */ | ||
| 657 | + public static LocalDate getLocalDateNow() { | ||
| 658 | + return LocalDate.now(); | ||
| 659 | + } | ||
| 660 | + | ||
| 661 | + /** | ||
| 662 | + * 获取日期 | ||
| 663 | + * | ||
| 664 | + * @param year | ||
| 665 | + * @param month | ||
| 666 | + * @param dayOfMont | ||
| 667 | + * @return 如:2014-12-10 | ||
| 668 | + */ | ||
| 669 | + public static LocalDate getLocalDate(int year, int month, int dayOfMont) { | ||
| 670 | + return LocalDate.of(year, month, dayOfMont); | ||
| 671 | + } | ||
| 672 | + | ||
| 673 | + /** | ||
| 674 | + * 获取时间 | ||
| 675 | + * | ||
| 676 | + * @return 如:14:29:40.558 | ||
| 677 | + */ | ||
| 678 | + public static LocalTime getLocalTimeNow() { | ||
| 679 | + return LocalTime.now(); | ||
| 680 | + } | ||
| 681 | + | ||
| 682 | + /** | ||
| 683 | + * 获取时间 | ||
| 684 | + * | ||
| 685 | + * @return 14:29:40 | ||
| 686 | + */ | ||
| 687 | + public static LocalTime getLocalTimeNowOfSecond() { | ||
| 688 | + return getLocalTimeNow().withNano(0); | ||
| 689 | + } | ||
| 690 | + | ||
| 691 | + /** | ||
| 692 | + * 获取指定时间 | ||
| 693 | + * | ||
| 694 | + * @param hour | ||
| 695 | + * @param minute | ||
| 696 | + * @param second | ||
| 697 | + * @param nanoOfSecond | ||
| 698 | + * @return | ||
| 699 | + */ | ||
| 700 | + public static LocalTime getLocalTime(int hour, int minute, int second, int nanoOfSecond) { | ||
| 701 | + return LocalTime.of(hour, minute, second, nanoOfSecond); | ||
| 702 | + } | ||
| 703 | + | ||
| 704 | + /** | ||
| 705 | + * 获取时间和日期 | ||
| 706 | + * | ||
| 707 | + * @return 如:2016-11-06T15:20:27.996 | ||
| 708 | + */ | ||
| 709 | + public static LocalDateTime getLocalDateTimeNow() { | ||
| 710 | + return LocalDateTime.now(); | ||
| 711 | + } | ||
| 712 | + | ||
| 713 | + /** | ||
| 714 | + * 获取时间和日期 | ||
| 715 | + * | ||
| 716 | + * @param year | ||
| 717 | + * @param month | ||
| 718 | + * @param dayOfMonth | ||
| 719 | + * @param hour | ||
| 720 | + * @param minute | ||
| 721 | + * @param second | ||
| 722 | + * @param nanoOfSecond | ||
| 723 | + * @return | ||
| 724 | + */ | ||
| 725 | + public static LocalDateTime getLocalDateTime(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) { | ||
| 726 | + return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond); | ||
| 727 | + | ||
| 728 | + | ||
| 729 | + } | ||
| 730 | + | ||
| 731 | + /** | ||
| 732 | + * LocalDateTime 格式化 | ||
| 733 | + * | ||
| 734 | + * @param localDateTime | ||
| 735 | + * @param pattern | ||
| 736 | + * @return | ||
| 737 | + * @see DatePatternUtils | ||
| 738 | + */ | ||
| 739 | + public static String format(LocalDateTime localDateTime, String pattern) { | ||
| 740 | + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); | ||
| 741 | + return dateTimeFormatter.format(localDateTime); | ||
| 742 | + } | ||
| 743 | + | ||
| 744 | + /** | ||
| 745 | + * LocalDate 格式化 | ||
| 746 | + * | ||
| 747 | + * @param localDate | ||
| 748 | + * @param pattern | ||
| 749 | + * @return | ||
| 750 | + */ | ||
| 751 | + public static String format(LocalDate localDate, String pattern) { | ||
| 752 | + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern); | ||
| 753 | + return dateTimeFormatter.format(localDate); | ||
| 754 | + } | ||
| 755 | + | ||
| 756 | + /** | ||
| 757 | + * 获取当前时间字符串 :2019-09-01 | ||
| 758 | + * @return | ||
| 759 | + */ | ||
| 760 | + public static String formatDateNow() { | ||
| 761 | + DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DatePatternUtils.NORM_DATE_PATTERN); | ||
| 762 | + return dateTimeFormatter.format(getLocalDateNow()); | ||
| 763 | + } | ||
| 764 | + | ||
| 765 | + | ||
| 766 | +} | ||
| 0 | \ No newline at end of file | 767 | \ No newline at end of file |
src/main/java/com/irrigation/icl/utils/DoubleValueUtils.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | ||
| 2 | + | ||
| 3 | +import com.irrigation.icl.exception.ContextRuntimeException; | ||
| 4 | + | ||
| 5 | +import javax.annotation.Nonnull; | ||
| 6 | +import java.math.BigDecimal; | ||
| 7 | +import java.util.Objects; | ||
| 8 | +import java.util.Optional; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * @description: 浮点数操作工具类 | ||
| 12 | + * @Author: yangLang | ||
| 13 | + * @CreateDate: 2020/4/29 16:09 | ||
| 14 | + */ | ||
| 15 | +public final class DoubleValueUtils { | ||
| 16 | + | ||
| 17 | + /** | ||
| 18 | + * 默认保留位数 | ||
| 19 | + */ | ||
| 20 | + private final static int SCALE = 4; | ||
| 21 | + | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * Double 0.0 | ||
| 25 | + */ | ||
| 26 | + public final static Double DOUBLE_ZERO = Double.valueOf(0.0d); | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * double 0.0 | ||
| 30 | + */ | ||
| 31 | + public final static double ZERO = 0.0d; | ||
| 32 | + | ||
| 33 | + | ||
| 34 | + private DoubleValueUtils() { | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * 计算两个浮点数之和s | ||
| 39 | + * | ||
| 40 | + * @param value 加数 | ||
| 41 | + * @param addValue 被加数 | ||
| 42 | + * @return 返回结果四舍五入,保留4位小数 | ||
| 43 | + * @Author: yangLang | ||
| 44 | + */ | ||
| 45 | + public static double doubleAdd(Double value, Double addValue) { | ||
| 46 | + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); | ||
| 47 | + addValue = Optional.ofNullable(addValue).orElse(DOUBLE_ZERO); | ||
| 48 | + return new BigDecimal(value.toString()).add(new BigDecimal(addValue.toString())) | ||
| 49 | + .setScale(SCALE, BigDecimal.ROUND_HALF_UP) | ||
| 50 | + .doubleValue(); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + /** | ||
| 54 | + * 计算两个浮点数之和 | ||
| 55 | + * | ||
| 56 | + * @param value 加数 | ||
| 57 | + * @param addValue 被加数 | ||
| 58 | + * @param scale 保留位数 | ||
| 59 | + * @return 返回结果四舍五入,保留4位小数 | ||
| 60 | + * @Author: yangLang | ||
| 61 | + */ | ||
| 62 | + public static double doubleAdd(Double value, Double addValue, int scale) { | ||
| 63 | + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); | ||
| 64 | + addValue = Optional.ofNullable(addValue).orElse(DOUBLE_ZERO); | ||
| 65 | + return new BigDecimal(value.toString()).add(new BigDecimal(addValue.toString())) | ||
| 66 | + .setScale(scale, BigDecimal.ROUND_HALF_UP) | ||
| 67 | + .doubleValue(); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + /** | ||
| 71 | + * 计算两个浮点数之差 | ||
| 72 | + * | ||
| 73 | + * @param value 减数 | ||
| 74 | + * @param subValue 被减数 | ||
| 75 | + * @return 返回结果四舍五入,保留4位小数 | ||
| 76 | + * @author: yangLang | ||
| 77 | + */ | ||
| 78 | + public static double doubleSub(Double value, Double subValue) { | ||
| 79 | + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); | ||
| 80 | + subValue = Optional.ofNullable(subValue).orElse(DOUBLE_ZERO); | ||
| 81 | + return new BigDecimal(value.toString()).subtract(new BigDecimal(subValue.toString())) | ||
| 82 | + .setScale(SCALE, BigDecimal.ROUND_HALF_UP) | ||
| 83 | + .doubleValue(); | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + /** | ||
| 87 | + * 计算两个浮点数之差 | ||
| 88 | + * | ||
| 89 | + * @param value 减数 | ||
| 90 | + * @param subValue 被减数 | ||
| 91 | + * @param scale 保留位数 | ||
| 92 | + * @return 返回结果四舍五入,保留4位小数 | ||
| 93 | + * @author: yangLang | ||
| 94 | + */ | ||
| 95 | + public static double doubleSub(Double value, Double subValue, int scale) { | ||
| 96 | + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); | ||
| 97 | + subValue = Optional.ofNullable(subValue).orElse(DOUBLE_ZERO); | ||
| 98 | + return new BigDecimal(value.toString()).subtract(new BigDecimal(subValue.toString())) | ||
| 99 | + .setScale(scale, BigDecimal.ROUND_HALF_UP) | ||
| 100 | + .doubleValue(); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + /** | ||
| 104 | + * 计算两个浮点数之积 | ||
| 105 | + * | ||
| 106 | + * @param value 乘数 | ||
| 107 | + * @param mutValue 被乘数 | ||
| 108 | + * @return 返回结果四舍五入,保留4位小数 | ||
| 109 | + * @author: yangLang | ||
| 110 | + */ | ||
| 111 | + public static double doubleMulti(Double value, Double mutValue) { | ||
| 112 | + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); | ||
| 113 | + mutValue = Optional.ofNullable(mutValue).orElse(DOUBLE_ZERO); | ||
| 114 | + return new BigDecimal(value.toString()).multiply(new BigDecimal(mutValue.toString())) | ||
| 115 | + .setScale(SCALE, BigDecimal.ROUND_HALF_UP) | ||
| 116 | + .doubleValue(); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + /** | ||
| 120 | + * 计算两个浮点数之积 | ||
| 121 | + * | ||
| 122 | + * @param value 乘数 | ||
| 123 | + * @param mutValue 被乘数 | ||
| 124 | + * @param scale 保留位数 | ||
| 125 | + * @return 返回结果四舍五入,保留4位小数 | ||
| 126 | + * @author: yangLang | ||
| 127 | + */ | ||
| 128 | + public static double doubleMulti(Double value, Double mutValue, int scale) { | ||
| 129 | + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); | ||
| 130 | + mutValue = Optional.ofNullable(mutValue).orElse(DOUBLE_ZERO); | ||
| 131 | + return new BigDecimal(value.toString()).multiply(new BigDecimal(mutValue.toString())) | ||
| 132 | + .setScale(scale, BigDecimal.ROUND_HALF_UP) | ||
| 133 | + .doubleValue(); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + /** | ||
| 137 | + * 计算两个浮点数之商 | ||
| 138 | + * | ||
| 139 | + * @param value 除数 | ||
| 140 | + * @param divValue 被除数 | ||
| 141 | + * @return 返回结果四舍五入,保留4位小数 | ||
| 142 | + * @author: yangLang | ||
| 143 | + */ | ||
| 144 | + public static double doubleDiv(Double value, @Nonnull Double divValue) { | ||
| 145 | + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); | ||
| 146 | + validateDivValue(divValue); | ||
| 147 | + return new BigDecimal(value.toString()).divide(new BigDecimal(divValue.toString())) | ||
| 148 | + .setScale(SCALE, BigDecimal.ROUND_HALF_UP) | ||
| 149 | + .doubleValue(); | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + /** | ||
| 153 | + * 计算两个浮点数之商 | ||
| 154 | + * | ||
| 155 | + * @param value 除数 | ||
| 156 | + * @param divValue 被除数 | ||
| 157 | + * @param scale 保留位数 | ||
| 158 | + * @return 返回结果四舍五入,保留4位小数 | ||
| 159 | + * @author: yangLang | ||
| 160 | + */ | ||
| 161 | + public static double doubleDiv(Double value, @Nonnull Double divValue, int scale) { | ||
| 162 | + value = Optional.ofNullable(value).orElse(DOUBLE_ZERO); | ||
| 163 | + validateDivValue(divValue); | ||
| 164 | + return new BigDecimal(value.toString()).divide(new BigDecimal(divValue.toString())) | ||
| 165 | + .setScale(scale, BigDecimal.ROUND_HALF_UP) | ||
| 166 | + .doubleValue(); | ||
| 167 | + } | ||
| 168 | + | ||
| 169 | + /** | ||
| 170 | + * 检查除数是否为0 | ||
| 171 | + * | ||
| 172 | + * @param divValue | ||
| 173 | + * @return | ||
| 174 | + * @author: yangLang | ||
| 175 | + */ | ||
| 176 | + private static Double validateDivValue(@Nonnull Double divValue) { | ||
| 177 | + Objects.requireNonNull(divValue, "被除数不能为空"); | ||
| 178 | + if (ZERO == divValue.doubleValue()) { | ||
| 179 | + throw new ContextRuntimeException("被除数不能为0"); | ||
| 180 | + } | ||
| 181 | + return divValue; | ||
| 182 | + } | ||
| 183 | + | ||
| 184 | +} |
src/main/java/com/irrigation/icl/utils/ObjectUtils.java
0 → 100644
| 1 | +package com.irrigation.icl.utils; | ||
| 2 | + | ||
| 3 | +import com.google.common.collect.Lists; | ||
| 4 | +import com.irrigation.icl.exception.ContextRuntimeException; | ||
| 5 | +import org.springframework.beans.BeanUtils; | ||
| 6 | + | ||
| 7 | +import java.util.*; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * @author zhangchuanxi | ||
| 11 | + * 对象工具类 | ||
| 12 | + * @version 1.0.1 | ||
| 13 | + */ | ||
| 14 | +public class ObjectUtils { | ||
| 15 | + | ||
| 16 | + /** | ||
| 17 | + * 复制对象错误提示 | ||
| 18 | + */ | ||
| 19 | + private final static String COPY_OBJECT_FAIL = "复制对象失败"; | ||
| 20 | + | ||
| 21 | + private ObjectUtils() { | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * 判断对象司是否是空 | ||
| 27 | + * | ||
| 28 | + * @param obj | ||
| 29 | + * @return | ||
| 30 | + */ | ||
| 31 | + public static boolean isNull(Object obj) { | ||
| 32 | + return obj == null; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * 判断对象司是否不是空 | ||
| 37 | + * | ||
| 38 | + * @param obj | ||
| 39 | + * @return | ||
| 40 | + */ | ||
| 41 | + public static boolean nonNull(Object obj) { | ||
| 42 | + return obj != null; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * 判断两个对象是否相等 | ||
| 47 | + * | ||
| 48 | + * @param a | ||
| 49 | + * @param b | ||
| 50 | + * @return | ||
| 51 | + */ | ||
| 52 | + public static boolean equals(Object a, Object b) { | ||
| 53 | + return a != null && a.equals(b); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + /** | ||
| 57 | + * 判断两个对象是否不相等 | ||
| 58 | + * | ||
| 59 | + * @param a | ||
| 60 | + * @param b | ||
| 61 | + * @return | ||
| 62 | + */ | ||
| 63 | + public static boolean unequal(Object a, Object b) { | ||
| 64 | + return (a != null && !a.equals(b)) || (b != null && !b.equals(a)); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * 判断集合是否是空 | ||
| 69 | + * | ||
| 70 | + * @param collection | ||
| 71 | + * @return | ||
| 72 | + */ | ||
| 73 | + public static boolean isEmpty(Collection<?> collection) { | ||
| 74 | + return collection == null || collection.isEmpty(); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + /** | ||
| 78 | + * 判断集合是否不是空 | ||
| 79 | + * | ||
| 80 | + * @param collection | ||
| 81 | + * @return | ||
| 82 | + */ | ||
| 83 | + public static boolean nonEmpty(Collection<?> collection) { | ||
| 84 | + return !isEmpty(collection); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + | ||
| 88 | + /** | ||
| 89 | + * 判断map 是否是空 | ||
| 90 | + * | ||
| 91 | + * @param map | ||
| 92 | + * @return | ||
| 93 | + */ | ||
| 94 | + public static boolean isEmpty(Map<?, ?> map) { | ||
| 95 | + return map == null || map.isEmpty(); | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + /** | ||
| 99 | + * 判断map 是否不是空 | ||
| 100 | + * | ||
| 101 | + * @param map | ||
| 102 | + * @return | ||
| 103 | + */ | ||
| 104 | + public static boolean nonEmpty(Map<?, ?> map) { | ||
| 105 | + return !isEmpty(map); | ||
| 106 | + } | ||
| 107 | + | ||
| 108 | + | ||
| 109 | + /** | ||
| 110 | + * 判断字符串 是否是 null 或 length=0 | ||
| 111 | + * | ||
| 112 | + * @param cs | ||
| 113 | + * @return | ||
| 114 | + */ | ||
| 115 | + public static boolean stringIsEmpty(CharSequence cs) { | ||
| 116 | + return cs == null || cs.length() == 0; | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + | ||
| 120 | + /** | ||
| 121 | + * 判断字符串 是否不是 null 并且 length!=0 | ||
| 122 | + * | ||
| 123 | + * @param cs | ||
| 124 | + * @return | ||
| 125 | + */ | ||
| 126 | + public static boolean stringNonEmpty(CharSequence cs) { | ||
| 127 | + return !stringIsEmpty(cs); | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + /** | ||
| 131 | + * 判断字符是否是空字符 | ||
| 132 | + * | ||
| 133 | + * @param cs | ||
| 134 | + * @return | ||
| 135 | + */ | ||
| 136 | + public static boolean stringIsBlank(CharSequence cs) { | ||
| 137 | + int strLen; | ||
| 138 | + if (cs == null || (strLen = cs.length()) == 0) { | ||
| 139 | + return true; | ||
| 140 | + } | ||
| 141 | + for (int i = 0; i < strLen; i++) { | ||
| 142 | + if (!Character.isWhitespace(cs.charAt(i))) { | ||
| 143 | + return false; | ||
| 144 | + } | ||
| 145 | + } | ||
| 146 | + return true; | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + /** | ||
| 150 | + * 判断字符是否是空字符 | ||
| 151 | + * | ||
| 152 | + * @param cs | ||
| 153 | + * @return | ||
| 154 | + */ | ||
| 155 | + public static boolean stringNonBlankB(CharSequence cs) { | ||
| 156 | + return !stringIsBlank(cs); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + | ||
| 160 | + /** | ||
| 161 | + * 去掉字符串前后空白字符 | ||
| 162 | + * | ||
| 163 | + * @param str | ||
| 164 | + * @return | ||
| 165 | + */ | ||
| 166 | + public static String stringTrim(final String str) { | ||
| 167 | + return str == null ? null : str.trim(); | ||
| 168 | + } | ||
| 169 | + | ||
| 170 | + /** | ||
| 171 | + * 单个对象复制 | ||
| 172 | + * | ||
| 173 | + * @param source | ||
| 174 | + * @param targetClass | ||
| 175 | + * @param <T> | ||
| 176 | + * @return | ||
| 177 | + */ | ||
| 178 | + | ||
| 179 | + public static <T> T copyObject(Object source, Class<T> targetClass) { | ||
| 180 | + if (isNull(source)) { | ||
| 181 | + return null; | ||
| 182 | + } | ||
| 183 | + try { | ||
| 184 | + T target = targetClass.newInstance(); | ||
| 185 | + BeanUtils.copyProperties(source, target); | ||
| 186 | + return target; | ||
| 187 | + } catch (Exception e) { | ||
| 188 | + throw new ContextRuntimeException(COPY_OBJECT_FAIL, e); | ||
| 189 | + } | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + /** | ||
| 193 | + * 集合复制 | ||
| 194 | + * | ||
| 195 | + * @param sourceList | ||
| 196 | + * @param targetClass | ||
| 197 | + * @param <T> | ||
| 198 | + * @return | ||
| 199 | + */ | ||
| 200 | + public static <S, T> List<T> copyObjectList(List<S> sourceList, Class<T> targetClass) { | ||
| 201 | + List<T> targetList = Lists.newArrayList(); | ||
| 202 | + if (nonEmpty(sourceList)) { | ||
| 203 | + for (S source : sourceList) { | ||
| 204 | + targetList.add(copyObject(source, targetClass)); | ||
| 205 | + } | ||
| 206 | + } | ||
| 207 | + return targetList; | ||
| 208 | + } | ||
| 209 | + | ||
| 210 | + /** | ||
| 211 | + * 集合通过连接符号返回字符串 | ||
| 212 | + * | ||
| 213 | + * @param iterable | ||
| 214 | + * @param separator | ||
| 215 | + * @return | ||
| 216 | + */ | ||
| 217 | + public static String stringJoin(Iterable<?> iterable, String separator) { | ||
| 218 | + return iterable == null ? null : stringJoin(iterable.iterator(), separator); | ||
| 219 | + } | ||
| 220 | + | ||
| 221 | + private static String stringJoin(Iterator<?> iterator, String separator) { | ||
| 222 | + if (iterator == null) { | ||
| 223 | + return null; | ||
| 224 | + } else if (!iterator.hasNext()) { | ||
| 225 | + return ""; | ||
| 226 | + } else { | ||
| 227 | + Object first = iterator.next(); | ||
| 228 | + if (!iterator.hasNext()) { | ||
| 229 | + return Objects.toString(first, ""); | ||
| 230 | + } else { | ||
| 231 | + StringBuilder buf = new StringBuilder(256); | ||
| 232 | + if (first != null) { | ||
| 233 | + buf.append(first); | ||
| 234 | + } | ||
| 235 | + | ||
| 236 | + while (iterator.hasNext()) { | ||
| 237 | + if (separator != null) { | ||
| 238 | + buf.append(separator); | ||
| 239 | + } | ||
| 240 | + | ||
| 241 | + Object obj = iterator.next(); | ||
| 242 | + if (obj != null) { | ||
| 243 | + buf.append(obj); | ||
| 244 | + } | ||
| 245 | + } | ||
| 246 | + return buf.toString(); | ||
| 247 | + } | ||
| 248 | + } | ||
| 249 | + } | ||
| 250 | + | ||
| 251 | + | ||
| 252 | +} |
src/main/java/com/irrigation/icl/utils/PasswordEncoderUtil.java
| 1 | package com.irrigation.icl.utils; | 1 | package com.irrigation.icl.utils; |
| 2 | 2 | ||
| 3 | -import org.springframework.security.authentication.encoding.BasePasswordEncoder; | ||
| 4 | -import org.springframework.security.authentication.encoding.Md5PasswordEncoder; | ||
| 5 | 3 | ||
| 4 | +import org.springframework.security.crypto.password.MessageDigestPasswordEncoder; | ||
| 5 | +import org.springframework.security.crypto.password.PasswordEncoder; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * @author zhangchuanxi | ||
| 9 | + * md5加密工具类 | ||
| 10 | + * @version 1.0.0 | ||
| 11 | + */ | ||
| 6 | public class PasswordEncoderUtil { | 12 | public class PasswordEncoderUtil { |
| 7 | 13 | ||
| 8 | - private final static BasePasswordEncoder encoder = new Md5PasswordEncoder(); | 14 | + public static final PasswordEncoder ENCODER = new MessageDigestPasswordEncoder("MD5"); |
| 9 | 15 | ||
| 10 | public static String encodePassword(String password) { | 16 | public static String encodePassword(String password) { |
| 11 | - return encoder.encodePassword(password, null); | 17 | + return ENCODER.encode(password); |
| 12 | } | 18 | } |
| 13 | 19 | ||
| 14 | public static boolean isPasswordValid(String rawPassword, String encodedPassword) { | 20 | public static boolean isPasswordValid(String rawPassword, String encodedPassword) { |
| 15 | - return encoder.isPasswordValid(encodedPassword, rawPassword, null); | 21 | + return ENCODER.matches(rawPassword, encodedPassword); |
| 16 | } | 22 | } |
| 17 | 23 | ||
| 18 | } | 24 | } |
src/main/java/com/irrigation/icl/utils/RestResultGeneratorUtil.java
| @@ -4,33 +4,39 @@ package com.irrigation.icl.utils; | @@ -4,33 +4,39 @@ package com.irrigation.icl.utils; | ||
| 4 | import com.irrigation.icl.entity.RestResult; | 4 | import com.irrigation.icl.entity.RestResult; |
| 5 | import com.irrigation.icl.enums.ResultEnum; | 5 | import com.irrigation.icl.enums.ResultEnum; |
| 6 | 6 | ||
| 7 | -public class RestResultGeneratorUtil { | 7 | +/** |
| 8 | + * @author zhangchuanxi | ||
| 9 | + * @version 1.1.0 | ||
| 10 | + * @Description api 公共返回类的工具类 | ||
| 11 | + */ | ||
| 12 | + | ||
| 13 | +public final class RestResultGeneratorUtil { | ||
| 14 | + | ||
| 15 | + private RestResultGeneratorUtil() { | ||
| 16 | + } | ||
| 8 | 17 | ||
| 9 | public static <T> RestResult<T> getResult(int code, T data, String message) { | 18 | public static <T> RestResult<T> getResult(int code, T data, String message) { |
| 10 | RestResult<T> result = new RestResult<>(code, message, data); | 19 | RestResult<T> result = new RestResult<>(code, message, data); |
| 11 | return result; | 20 | return result; |
| 12 | } | 21 | } |
| 13 | 22 | ||
| 14 | - public static <T> RestResult<T> getSuccessResult() { | 23 | + public static <T> RestResult<T> getSuccessResult() { |
| 15 | return getResult(ResultEnum.SUCCESS.getCode(), null, ResultEnum.SUCCESS.getMessage()); | 24 | return getResult(ResultEnum.SUCCESS.getCode(), null, ResultEnum.SUCCESS.getMessage()); |
| 16 | } | 25 | } |
| 26 | + | ||
| 17 | public static <T> RestResult<T> getSuccessResult(T data) { | 27 | public static <T> RestResult<T> getSuccessResult(T data) { |
| 18 | return getResult(ResultEnum.SUCCESS.getCode(), data, ResultEnum.SUCCESS.getMessage()); | 28 | return getResult(ResultEnum.SUCCESS.getCode(), data, ResultEnum.SUCCESS.getMessage()); |
| 19 | } | 29 | } |
| 20 | 30 | ||
| 21 | - public static <T> RestResult<T> getSuccessResult(T data,String message) { | 31 | + public static <T> RestResult<T> getSuccessResult(T data, String message) { |
| 22 | return getResult(ResultEnum.SUCCESS.getCode(), data, message); | 32 | return getResult(ResultEnum.SUCCESS.getCode(), data, message); |
| 23 | } | 33 | } |
| 24 | 34 | ||
| 25 | - public static <T> RestResult<T> getSuccessResultMsg(String message) { | 35 | + public static <T> RestResult<T> getSuccessResultMsg(String message) { |
| 26 | return getResult(ResultEnum.SUCCESS.getCode(), null, message); | 36 | return getResult(ResultEnum.SUCCESS.getCode(), null, message); |
| 27 | } | 37 | } |
| 28 | 38 | ||
| 29 | 39 | ||
| 30 | -// public static RestResult<String> getErrorResult(String message) { | ||
| 31 | -// return getResult(ResultEnum.ERROR.getCode(), null, message); | ||
| 32 | -// } | ||
| 33 | - | ||
| 34 | public static <T> RestResult<T> getErrorResult(String message) { | 40 | public static <T> RestResult<T> getErrorResult(String message) { |
| 35 | return getResult(ResultEnum.ERROR.getCode(), null, message); | 41 | return getResult(ResultEnum.ERROR.getCode(), null, message); |
| 36 | } | 42 | } |