package com.power.common.util;
import java.sql.Timestamp;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalField;
import java.time.temporal.WeekFields;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* @author sunyu
*/
public class DateTimeUtil {
public static final String DATE_FORMAT_MINITE = "yyyy-MM-dd HH:mm";
public static final String DATE_FORMAT_DAY = "yyyy-MM-dd";
public static final String DATE_FORMAT_SECOND = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_SECOND_12 = "yyyy-MM-dd hh:mm:ss";
public static final String DATE_FORMAT_MILLISECOND = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String LOCAL_TIME = "HH:mm:ss";
public static final String YEAR = "yyyy";
public static final String YEAR_MONTH ="yyyy-MM";
public static final String MONTH_DAY = "MM-dd";
public static final String DATE_FORMAT_CHINESE = "yyyy年MM月dd日";
public static final String DATE_FORMAT_CHINESE_SECONDE = "yyyy年MM月dd日 HH:mm:ss";
public static final String DATE_FORMAT_CHINESE_WEEK_SECONDE = "yyyy年MM月dd日 E HH:mm:ss";
public static final String DATE_FORMAT_ZONED_DATE_TIME = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
public static final String YYYYMMDD = "yyyyMMdd";
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
public static final String DATE_MINUTES_12 = "yyyy-MM-dd hh-mm-ss";
public static final String STANDARD_DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
public static final long DAY_MS = 86400000L;
private static final ConcurrentMap<String, DateTimeFormatter> FORMATTER_CACHE = new ConcurrentHashMap<>();
private static final int PATTERN_CACHE_SIZE = 500;
private final static String[] WEEK_ARR = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
/**
* Formats a java.util.Date using a format string
*
* @param date The date to format
* @param format The format string like yyyy-MM-dd
* @return String
*/
public static String dateToStr(Date date, String format) {
return dateToStr(date, format, null);
}
/**
* now timestamp
*
* @return String
*/
public static String nowStrTime() {
return long2Str(System.currentTimeMillis(), DATE_FORMAT_SECOND);
}
/**
* get str now time
*
* @param pattern pattern like yyyy-MM-dd
* @return String
*/
public static String nowStrTime(String pattern) {
return long2Str(System.currentTimeMillis(), pattern);
}
/**
* 12-hour clock
*
* @return String
*/
public static String nowStrTime12() {
return long2Str(System.currentTimeMillis(), DATE_MINUTES_12);
}
/**
* Obtain now date
*
* @return String
*/
public static String nowStrDate() {
return LocalDate.now().toString();
}
/**
* Format ZonedDateTime to String
*
* @param zonedDateTime ZonedDateTime
* @param format Time format
* @return String
*/
public static String zonedDateTimeToStr(ZonedDateTime zonedDateTime, String format) {
DateTimeFormatter formatter = createCacheFormatter(format);
return formatter.format(zonedDateTime);
}
/**
* Convert Date to String
*
* @param date java.util.Date
* @param pattern pattern like yyyy-MM-dd
* @param locale locale
* @return String
*/
public static String dateToStr(Date date, String pattern, Locale locale) {
return localDateTimeToStr(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()), pattern);
}
/**
* Formats a java.sql.Date using a format string
*
* @param date java.sql.Date
* @param format format like yyyy-MM-dd
* @return String
*/
public static String sqlDateToStr(java.sql.Date date, String format) {
return localDateTimeToStr(LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()), format);
}
/**
* Parses a date using a specified format string
*
* @param sDate String
* @param format String
* @return Date
*/
public static Date strToDate(String sDate, String format) {
LocalDate localDate = parseLocalDate(sDate, format);
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime zdt = localDate.atStartOfDay(zoneId);
return Date.from(zdt.toInstant());
}
/**
* Generation timestamp
*
* @return java.sql.Timestamp
*/
public static Timestamp nowTimeStamp() {
return new Timestamp(System.currentTimeMillis());
}
/**
* format a string time like '2013-02-13' to '2013-02-13 00:00:00.0'
*
* @param date String
* @return java.sql.Timestamp
*/
public static Timestamp strToStamp(String date) {
return strToStamp(date, DATE_FORMAT_SECOND);
}
/**
* Convert String to Timestamp
*
* @param date String of date content
* @param format data format
* @return Timestamp
*/
public static Timestamp strToStamp(String date, String format) {
LocalDateTime localDateTime = parseLocalDateTime(date, format);
return Timestamp.valueOf(localDateTime);
}
/**
* input began time and end time calculate run time
*
* @param beginTime long
* @param endTime long
* @return double
*/
public static double calcRunTime(long beginTime, long endTime) {
return endTime - beginTime;
}
public static void printRunTime(long beganTime, long endTime) {
System.out.println("CostTime->" + (endTime - beganTime) / 1000.00
+ " Seconds");
}
/**
* obtain now time
*
* @return Long
*/
public static Long getNowTime() {
return System.currentTimeMillis();
}
/**
* Get Distance Of ms between and two date
*
* @param before before date
* @param after after date
* @return long
*/
public static long getDistanceOfTwoDate(Date before, Date after) {
long beforeTime = before.getTime();
long afterTime = after.getTime();
return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
}
public static boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
/**
* format TimeStamp to string
*
* @param time java.sql.Timestamp
* @param fmt String
* @return String
*/
public static String timestampToString(Timestamp time, String fmt) {
DateTimeFormatter dateTimeFormatter = createCacheFormatter(fmt);
LocalDateTime dateTime = time.toLocalDateTime();
return dateTimeFormatter.format(dateTime);
}
/**
* set time to next day's 0 hour 0 minute 0 second
*
* @param time java.sql.Timestamp
* @return long
*/
public static long setTimeToNextDay0H0M0S(Timestamp time) {
if (time != null) {
return setTimeToNextDay0H0M0S(time.getTime());
} else {
throw new NullPointerException("Timestamp can not be null");
}
}
/**
* set time to next day's 0 hour 0 minute 0 second
*
* @param millis millisecond
* @return long
*/
public static long setTimeToNextDay0H0M0S(long millis) {
LocalDate localDate = longToLocalDate(millis);
localDate = localDate.plusDays(1);
return localDateToLong(localDate);
}
/**
* if the time is today return current milliseconds else set time to next
* day's 0 hour 0 minute 0 seco
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
ApplicationPower项目生成脚手架设计源码:该项目基于Java开发,包含284个文件,主要使用Java和Shell语言。ApplicationPower是一个快速的项目生成脚手架,旨在帮助开发者快速搭建项目框架,包括创建项目结构、配置文件、开发环境等,提高开发效率。
资源推荐
资源详情
资源评论
收起资源包目录
基于Java的ApplicationPower快速项目生成脚手架设计源码 (278个子文件)
gradlew.bat 3KB
start.bat 2KB
springboot-pom.btl 9KB
RestExceptionHandler.btl 4KB
build.btl 4KB
ServiceImpl.btl 3KB
Controller.btl 3KB
Controller.btl 3KB
log4j2.btl 3KB
jquery-plugin.btl 3KB
ControllerTest.btl 3KB
ShutdownConfig.btl 3KB
MyBatisConfig.btl 3KB
AbstractDataSourceConfig.btl 3KB
DateConverter.btl 2KB
DruidConfig.btl 2KB
application.btl 2KB
TransactionManagerConfig.btl 2KB
ControllerTest2.btl 2KB
Mapper.btl 2KB
Jenkinsfile.btl 2KB
application-multiple-db.btl 1KB
Dao.btl 1KB
SmartDocTest.btl 1KB
Service.btl 1KB
logback.btl 1KB
ServiceTest.btl 1KB
ServiceImpl.btl 992B
ServiceTest.btl 913B
Dockerfile.btl 795B
ErrorCode.btl 725B
model.btl 655B
model.btl 566B
SpringBootMainApplication.btl 515B
gitignore.btl 456B
SpringBootBaseControllerTest.btl 452B
DbAspect.btl 445B
404.btl 347B
400.btl 337B
500.btl 330B
Dao.btl 320B
BaseController.btl 296B
SpringBootServiceBaseTest.btl 292B
Service.btl 283B
Mapper.btl 221B
DataSourceKey.btl 108B
settings.btl 48B
.gitignore 290B
gradlew 8KB
DateTimeUtil.java 29KB
StringUtil.java 19KB
ValidateUtil.java 18KB
OkHttp3Util.java 14KB
PathMatcher.java 13KB
ConfigBuilder.java 13KB
AESUtil.java 12KB
CodeWriter.java 12KB
FileUtil.java 12KB
MapperBuilder.java 11KB
StringEscapeUtil.java 11KB
CustomSqlSessionTemplate.java 10KB
ConstVal.java 8KB
DateFilterTest.java 8KB
RSAUtil.java 8KB
ModelBuilder.java 7KB
RandomUtil.java 7KB
CollectionUtil.java 7KB
EnumUtil.java 6KB
MySqlProvider.java 6KB
XssHttpServletRequestWrapper.java 6KB
GeneratorProperties.java 5KB
PageVo.java 5KB
KubeYmlCodeBuilder.java 5KB
DateTimeUtilTest.java 4KB
JodaParser.java 4KB
SpringBootMybatisCfgBuilder.java 4KB
GeneratorConstant.java 4KB
CommonResult.java 4KB
DockerCodeBuilder.java 4KB
SpringBootProjectConfig.java 4KB
DataSourceAspect.java 4KB
GradleCodeBuilder.java 3KB
CasualISO8601Parser.java 3KB
BaseCodeBuilder.java 3KB
ClassUtil.java 3KB
AssemblyCodeBuilder.java 3KB
UnixEpochParser.java 3KB
Base64Util.java 3KB
SessionFilter.java 3KB
BaseProvider.java 3KB
IpFilter.java 3KB
PomCodeBuilder.java 3KB
DateFilter.java 3KB
DbUtil.java 3KB
ProjectConfig.java 3KB
TAI64NParser.java 2KB
IpUtil.java 2KB
TimestampParserFactory.java 2KB
AESUtilTest.java 2KB
PathUtil.java 2KB
共 278 条
- 1
- 2
- 3
资源评论
沐知全栈开发
- 粉丝: 5701
- 资源: 5216
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功