该时间工具类功能:时间戳格式化至毫秒、时间戳格式化至秒、时间戳格式化至日、时间戳格式化成时和分、Date对象格式化至毫秒、Date对象格式化至秒、Date对象格式化至日、Date对象格式化成时和分、Date对象格式化成年份、时间格式为时和分转换为时间戳、时间格式为年月日转换为时间戳、时间格式为年月日时分秒转换为时间戳、获取当前时间的前一天日期。
package com
.blog
.chen_2890
.utils
;
import java
.text
.ParseException
;
import java
.text
.SimpleDateFormat
;
import java
.util
.Calendar
;
import java
.util
.Date
;
public class TimeUtil {
public static String
formatToMillis(Long millis
){
Long memberTime
= millis
;
Date dt
= new Date(memberTime
);
SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
return dateFormat
.format(dt
);
}
public static String
formatToSecond(Long millis
){
Long memberTime
= millis
;
Date dt
= new Date(memberTime
);
SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat
.format(dt
);
}
public static String
formatToDate(Long millis
){
Long memberTime
= millis
;
Date dt
= new Date(memberTime
);
SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd");
return dateFormat
.format(dt
);
}
public static String
formatToHourMinute(Long millis
){
Long memberTime
= millis
;
Date dt
= new Date(memberTime
);
SimpleDateFormat dateFormat
= new SimpleDateFormat("HH:mm");
return dateFormat
.format(dt
);
}
public static String
formatToMillis(Date date
){
Date dt
= date
;
SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
return dateFormat
.format(dt
);
}
public static String
formatToSecond(Date date
){
Date dt
= date
;
SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat
.format(dt
);
}
public static String
formatToDate(Date date
){
Date dt
= date
;
SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd");
return dateFormat
.format(dt
);
}
public static String
formatToHourMinute(Date date
){
Date dt
= date
;
SimpleDateFormat dateFormat
= new SimpleDateFormat("HH:mm");
return dateFormat
.format(dt
);
}
public static String
formatToYear(Date date
){
Date dt
= date
;
SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy");
return dateFormat
.format(dt
);
}
public static Long
fromHourMinuteToMillis(String format
){
SimpleDateFormat dateFormat
= new SimpleDateFormat("HH:mm");
try {
Date date
= dateFormat
.parse(format
);
return date
.getTime();
} catch (ParseException e
) {
e
.printStackTrace();
}
return null
;
}
public static Long
fromDateToMillis(String format
){
SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd");
try {
Date date
= dateFormat
.parse(format
);
return date
.getTime();
} catch (ParseException e
) {
e
.printStackTrace();
}
return null
;
}
public static Long
fromSecondToMillis(String format
){
SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date
= dateFormat
.parse(format
);
return date
.getTime();
} catch (ParseException e
) {
e
.printStackTrace();
}
return null
;
}
public static String
beforeOneDayDate(){
Calendar calendar
= Calendar
.getInstance();
calendar
.add(Calendar
.DATE
, -1);
SimpleDateFormat dateFormat
= new SimpleDateFormat("yyyy-MM-dd");
try {
return dateFormat
.format(calendar
.getTime());
} catch (Exception e
) {
e
.printStackTrace();
}
return null
;
}
}
注:其实就是简单粗暴的分装了一下重复代码,可以有效的提高日常开发效率。
原来微信打赏还可以备注哦