当前位置:网站首页>Date processing tool class dateutils (tool class 1)
Date processing tool class dateutils (tool class 1)
2022-07-07 01:56:00 【Novice Zhang~】
/** * Date Toolset */
@UtilityClass
public class DateUtils {
public static final String DATE_FORMAT = "yyyy-MM-dd";
public static final String YYYY_MM_DD = "yyyyMMdd";
public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String TIME_FORMAT = "HH:mm:ss";
/** * Get relative time * * @param date * @param amount * @return */
public static String getRelativeTime(Date date, int amount) {
String addDate = DateUtils.getRelativeTime(date, amount);
return addDate;
}
/** * Convert time format 0000-00-00 00:00:00 —> 0000-00-00 * * @param dateTimeStr Time to switch * @return */
public static String toDateStr(@NonNull String dateTimeStr) {
return dateTimeStr.split(" ")[0];
}
/** * Convert time format Date Time format —> String Time format 0000-00-00 * * @param dateTimeStr Time to switch * @return */
public static String toDateStr(@NonNull Date dateTimeStr) {
return DateUtils.toDateStr(dateTimeStr);
}
/** * Convert time format String Time format 0000-00-00 -> Date Time format * * @param dateTimeStr Time to switch * @return */
public static Date toDate(@NonNull String dateTimeStr) {
Date date = null;
try {
// date =
// DateUtils.toDate(
// dateTimeStr, DateFormatEnum.ISO_8601_EXTENDED_DATE_FORMAT.getPattern());
date = DateUtils.toDate(dateTimeStr);
} catch (Exception e) {
throw new RuntimeException(" Convert time format String Time format 0000-00-00 -> Date Time format abnormal ");
}
return date;
}
/** * Convert time format String Time format 0000-00-00 -> Date Time format * * @param dateTimeStr Time to switch * @return */
public static Date toDateTime(@NonNull String dateTimeStr) {
Date date = null;
try {
// date =
// DateUtils.toDate(
// dateTimeStr, DateFormatEnum.ISO_8601_EXTENDED_DATETIME_FORMAT.getPattern());
date = DateUtils.toDate(dateTimeStr);
} catch (Exception e) {
throw new RuntimeException(" Convert time format String Time format 0000-00-00 -> Date Time format abnormal ");
}
return date;
}
/** * Get all the time from the creation time to yesterday according to the creation time * * @param gmtCreate Creation time * @return All time */
public static List<String> getTimest(String gmtCreate) {
List<String> datesStr = Lists.newArrayList();
Date gmtCreateDate = toDate(gmtCreate);
Date yesterdayDate = toDate(DateUtils.getRelativeTime(new Date(), -1));
while (gmtCreateDate.getTime() <= yesterdayDate.getTime()) {
datesStr.add(toDateStr(gmtCreateDate));
gmtCreateDate = toDate(DateUtils.getRelativeTime(gmtCreateDate, 1));
}
return datesStr;
}
/** * date turn LocalDateTime * * @param date * @return */
public static LocalDateTime dateToLocalDateTime(Date date) {
if (ObjectUtils.isNull(date)) {
return null;
}
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
}
/** * date turn LocalDateTime * * @param localDateTime * @return */
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
if (ObjectUtils.isNull(localDateTime)) {
return null;
}
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
public static final Long SHOPEE_TIMESTAMP_LIMIT = 4294967295L;
/** * solve Shopee Timestamp limitation * * @param timestamp * @return */
public static long resolveShopeeTimestamp(Long timestamp) {
if (timestamp >= SHOPEE_TIMESTAMP_LIMIT) {
return timestamp / 1000L;
}
return timestamp;
}
/** * Date to string Format customization * * @param date * @param f * @return */
public static String dateStr(Date date, String f) {
if (date == null) {
return "";
}
SimpleDateFormat format = new SimpleDateFormat(f);
String str = format.format(date);
return str;
}
/** * Get the remaining time of the day * Returns the second value */
public long getRemainingTime() {
//long milliSecondsLeftToday = 86400000 - org.apache.commons.lang.time.DateUtils.getFragmentInMilliseconds(Calendar.getInstance(), Calendar.DATE);
return 86400 - org.apache.commons.lang.time.DateUtils.getFragmentInSeconds(Calendar.getInstance(), Calendar.DATE);
}
/** * take date Turn into ISODate In order to mongo distinguish * * @param dateStr * @return */
public static Date dateToISODate(Date dateStr) {
Date parse = null;
try {
// Parse string time
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
parse = format.parse(format.format(dateStr));
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
/** * If it is a string, it needs to be converted to date Call again dateToISODate() Method * @param strDate * @return */
public static Long strToDateLong(String strDate,String f) {
Date date = null;
try {
SimpleDateFormat formatter = new SimpleDateFormat(f);
date = formatter.parse(strDate);
return date.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/** * Timestamp to time string * @param tiemsmap Time stamp * @return Converted String */
public static String timestampToStr(Long tiemsmap){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(tiemsmap);
}
/** * Custom date analysis format * @param tiemsmap Time stamp * @param r Converted time format for example : yyyy-MM-dd HH:mm:ss * @return Converted String */
public static String timestampToStr(Long tiemsmap,String r){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(r);
return simpleDateFormat.format(tiemsmap);
}
}
边栏推荐
- Set up [redis in centos7.x]
- 猫猫回收站
- Google released a security update to fix 0 days that have been used in chrome
- Errors made in the development of merging the quantity of data in the set according to attributes
- Appium自动化测试基础 — uiautomatorviewer定位工具
- AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
- Set WordPress pseudo static connection (no pagoda)
- ROS learning (21) robot slam function package -- installation and testing of orbslam
- AcWing 361. 观光奶牛 题解(spfa求正环)
- 蓝桥杯2022年第十三届省赛真题-积木画
猜你喜欢

Box stretch and pull (left-right mode)

mongodb查看表是否导入成功

Make DIY welding smoke extractor with lighting

微服务架构介绍
![[advanced C language] 8 written questions of pointer](/img/d4/c9bb2c8c9fd8f54a36e463e3eb2fe0.png)
[advanced C language] 8 written questions of pointer

shell脚本快速统计项目代码行数

Gin 入门实战

454 Baidu Mianjing 1

Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 2)

Analyze "C language" [advanced] paid knowledge [i]
随机推荐
C language [23] classic interview questions [Part 2]
Compile command line terminal swift
Use nodejs to determine which projects are packaged + released
json学习初体验–第三者jar包实现bean、List、map创json格式
ROS learning (25) rviz plugin
454-百度面经1
Google released a security update to fix 0 days that have been used in chrome
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 2)
uva 1401 dp+Trie
Appium automation test foundation uiautomatorviewer positioning tool
shell脚本快速统计项目代码行数
AcWing 904. 虫洞 题解(spfa求负环)
Let's see how to realize BP neural network in Matlab toolbox
JS Es5 can also create constants?
mysqlbackup 还原特定的表
刨析《C语言》【进阶】付费知识【完结】
Add PDF Title floating window
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
公钥\私人 ssh避password登陆
2022 system integration project management engineer examination knowledge point: Mobile Internet