常用日期工具类👇即插即用)
1. 常用日期格式
- HHmmss
- yyyyMMdd
- yyyyMM
- yyyyMMddHHmmss
2. DateUtils
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class DateUtils {
public static final String weekdayFormat = "EEE";
public static final String timeFormat = "HHmmss";
public static final String dateFormat = "yyyyMMdd";
public static final String monthFormat = "yyyyMM";
public static final String dateTimeFormat = "yyyyMMddHHmmss";
public DateUtils() {
}
public static String getCurrentTimeString(String format, long mills) {
SimpleDateFormat simpleFormat = new SimpleDateFormat(format, Locale.CHINA);
return simpleFormat.format(new Date(mills));
}
public static String getCurrentFormatTime(String format) {
return getCurrentTimeString(format, System.currentTimeMillis());
}
public static String yyyyMM() {
return getCurrentTimeString("yyyyMM", System.currentTimeMillis());
}
public static String yyyyMMddHHmmss() {
return getCurrentTimeString("yyyyMMddHHmmss", System.currentTimeMillis());
}
public static String yyyyMMdd() {
return getCurrentTimeString("yyyyMMdd", System.currentTimeMillis());
}
public static String HHmmss() {
return getCurrentTimeString("HHmmss", System.currentTimeMillis());
}
public static String getFormatedDate(long mills) {
return getCurrentTimeString("yyyyMMdd", mills);
}
public static String getFormatedTime(long mills) {
return getCurrentTimeString("yyyyMMddHHmmss", mills);
}
public static String getYesterDay() {
return getOtherDay(-1);
}
public static String getTomorrow() {
return getOtherDay(1);
}
public static String getOtherDay(int i) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
Calendar c = Calendar.getInstance();
c.add(5, i);
return df.format(c.getTime());
}
public static int getWeekDay() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int weekday = cal.get(7);
if (weekday == 1) {
weekday = 7;
} else {
--weekday;
}
return weekday;
}
public static String format(String yyyy_MM_dd) {
DateFormat formatterSrc = new SimpleDateFormat("yyyy-MM-dd");
DateFormat formatterDes = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
try {
date = formatterSrc.parse(yyyy_MM_dd);
} catch (ParseException var5) {
var5.printStackTrace();
}
return formatterDes.format(date);
}
public static long getDifferernce(String mills) {
if (mills.length() != 14) {
return 0L;
} else {
new SimpleDateFormat("yyyyMMddHHmmss");
Calendar c = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c2.set(Integer.parseInt(mills.substring(0, 4)), Integer.parseInt(mills.substring(4, 6)) - 1, Integer.parseInt(mills.substring(6, 8)), Integer.parseInt(mills.substring(8, 10)), Integer.parseInt(mills.substring(10, 12)), Integer.parseInt(mills.substring(12, 14)));
long ld = c.getTimeInMillis();
long ld2 = c2.getTimeInMillis();
long diff = (ld - ld2) / 60000L;
return diff;
}
}
}