当前位置:网站首页>时间工具类
时间工具类
2022-07-07 17:29:00 【稻草人0.0】
public class DateUtils {
public static final Date MAX_DATE = new Date(4102243199000L);
private DateUtils() {
throw new UnsupportedOperationException(this.getClass().getName() + " is a tool class, can't be created instance!");
}
public static String convertTimeToString(long longTime) {
return convertTimeToString(longTime, "yyyy-MM-dd HH:mm:ss");
}
public static String convertTimeToString(long longTime, String format) {
try {
Timestamp t = new Timestamp(longTime * 1000L);
SimpleDateFormat sDateFormat = new SimpleDateFormat(format);
return sDateFormat.format(t);
} catch (Exception var5) {
throw new RuntimeException("Can't format the time by format[" + format + "]!");
}
}
public static long convertTimeToLong(String dateTime) {
return convertTimeToLong(dateTime, "yyyy-MM-dd HH:mm:ss");
}
public static long convertTimeToLong(String dateTime, String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
long date = 0L;
try {
Date d = sdf.parse(dateTime);
date = d.getTime() / 1000L;
return date;
} catch (Exception var6) {
throw new RuntimeException("Can't format the time by format[" + format + "]!");
}
}
public static long getCurrentLongTime() {
return System.currentTimeMillis() / 1000L;
}
public static String getCurrentTime() {
return getCurrentTime("yyyy-MM-dd HH:mm:ss");
}
public static String getCurrentTime(String format) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
} catch (Exception var2) {
throw new RuntimeException("Can't format the time by format[" + format + "]!");
}
}
public static long getDayBetweenDateAndDate(String startDate, String endDate) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
long day = 0L;
try {
Date date = myFormatter.parse(endDate);
Date mydate = myFormatter.parse(startDate);
day = (date.getTime() - mydate.getTime()) / 86400000L;
return day;
} catch (Exception var7) {
throw new RuntimeException("Time format[" + startDate + "][" + endDate + "] is error ! format must be 'yyyy-MM-dd'!");
}
}
public static int getWeekday(String dateTime) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = myFormatter.parse(dateTime);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(7) - 1 < 0 ? 0 : cal.get(7) - 1;
} catch (ParseException var4) {
throw new RuntimeException("Time format[" + dateTime + "] is error ! format must be 'yyyy-MM-dd'!");
}
}
public static String getPreWeekFirstDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus - 7);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getPreWeekLastDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus - 1);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getNextWeekFirstDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus + 7);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getNextWeekLastDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus + 7 + 6);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getWeekFirstDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getWeekLastDay() {
Calendar cd = Calendar.getInstance();
int week = cd.get(7) == 1 ? 8 : cd.get(7);
int mondayPlus = week - 1 == 1 ? 0 : 1 - (week - 1);
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.add(5, mondayPlus + 6);
Date monday = currentDate.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(monday);
}
public static String getPreMonthFirstDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(5, 1);
lastDate.add(2, -1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getPreMonthLastDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(2, -1);
lastDate.set(5, 1);
lastDate.roll(5, -1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getMonthFirstDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(5, 1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getMonthLastDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.set(5, 1);
lastDate.add(2, 1);
lastDate.add(5, -1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getNextMonthFirstDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(2, 1);
lastDate.set(5, 1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getNextMonthLastDay() {
String str = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar lastDate = Calendar.getInstance();
lastDate.add(2, 1);
lastDate.set(5, 1);
lastDate.roll(5, -1);
str = sdf.format(lastDate.getTime());
return str;
}
public static String getBeforeAfterDate(String dateTime, int day) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date oldDate = null;
try {
df.setLenient(false);
oldDate = new Date(df.parse(dateTime).getTime());
} catch (ParseException var11) {
throw new RuntimeException("Time format[" + dateTime + "] is error ! format must be 'yyyy-MM-dd'!");
}
Calendar cal = new GregorianCalendar();
cal.setTime(oldDate);
int Year = cal.get(1);
int Month = cal.get(2);
int Day = cal.get(5);
int NewDay = Day + day;
cal.set(1, Year);
cal.set(2, Month);
cal.set(5, NewDay);
Date date = new Date(cal.getTimeInMillis());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
}
边栏推荐
- LeetCode 515(C#)
- Research and practice of super-resolution technology in the field of real-time audio and video
- 【RT-Thread env 工具安装】
- 2022.07.02
- 2022上半年朋友圈都在传的10本书,找到了
- Netease Yunxin participated in the preparation of the standard "real time audio and video service (RTC) basic capability requirements and evaluation methods" issued by the Chinese Academy of Communica
- L1-027 rental (Lua)
- Matplotlib drawing 3D graphics
- Solve the error reporting problem of rosdep
- 杰理之关于 TWS 声道配置【篇】
猜你喜欢
Zhong Xuegao wants to remain innocent in the world
Install mysql8 for Linux X ultra detailed graphic tutorial
The strength index of specialized and new software development enterprises was released, and Kirin Xin'an was honored on the list
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
2022上半年朋友圈都在传的10本书,找到了
2022.07.05
2022.07.02
2022.07.05
Redis master-slave and sentinel master-slave switchover are built step by step
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
随机推荐
Tapdata 的 2.0 版 ,开源的 Live Data Platform 现已发布
AI写首诗
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
testing and SQA_动态白盒測试[通俗易懂]
ES6笔记一
网信办公布《数据出境安全评估办法》,9 月 1 日起施行
实训九 网络服务的基本配置
PV static creation and dynamic creation
2022.07.05
杰理之关于 TWS 交叉配对的配置【篇】
ES6 note 1
MySQL、sqlserver oracle数据库连接方式
Redis master-slave and sentinel master-slave switchover are built step by step
LeetCode 497(C#)
How many are there (Lua)
国内首次!这家中国企业的语言AI实力被公认全球No.2!仅次于谷歌
最长公共前缀(leetcode题14)
R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化分组经验累积密度分布函数曲线、linetype参数指定不同分组曲线的线型
LeetCode 515(C#)