java环境jdk8
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateUtils {
private static final String[] WEEK_EN = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
private static final String[] WEEK_CN = {"周一", "周二", "周三", "周四", "周五", "周六", "周日"};
private static final String EARLY_MORNING = "00:00:00";
private static final String NOonDAY = "12:00:00";
public static String YYYY = "yyyy";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String DD = "dd";
public static String HH_MM = "HH:mm";
public static String MM_DD_HH_MM = "MM月dd日 HH:mm";
public static String YYYY_MM_DD_HH_MM = "yyyy年MM月dd日 HH:mm";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static String SPACE = " ";
public static Date getNowDate() {
return new Date();
}
public static String format(Date date, String pattern) {
if (date != null) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
return df.format(date);
}
return null;
}
public static Date parseDate(String str) {
if (str == null) {
return null;
}
SimpleDateFormat formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
try {
return formatter.parse(str);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public static String format(Date date) {
return processingTime(date);
}
private static String processingTime(Date date) {
// 处理后的日期
String data;
// 当天中午
Date noon = parseDate(format(getNowDate(), YYYY_MM_DD).concat(SPACE).concat(NOONDAY));
// 处理时间下标索引
int index = 0;
// 得到需要处理的日期
String week = date.toString().substring(0, 3);
for (String s : WEEK_EN) {
if (s.equals(week)) {
break;
}
index++;
}
// 判断是否是同一周
if (date.getTime() >= getMonTime() && date.getTime() <= getSunTime()) {
// 判断是否是同一天
if (format(date, DD).equals(format(noon, DD))) {
// 判断上下午
if (date.before(noon)) {
data = "上午".concat(SPACE).concat(format(date, HH_MM));
} else {
data = "下午".concat(SPACE).concat(format(date, HH_MM));
}
} else {
data = WEEK_CN[index].concat(SPACE).concat(format(date, HH_MM));
}
} else {
// 判断是否是同一年
if (format(date, YYYY).equals(format(noon, YYYY))) {
data = format(date, MM_DD_HH_MM);
} else {
data = format(date, YYYY_MM_DD_HH_MM);
}
}
return data;
}
public static long getMonTime() {
// 当天凌晨
Date earlyMorning = parseDate(format(getNowDate(), YYYY_MM_DD).concat(SPACE).concat(EARLY_MORNING));
// 得到当前日期
String week = earlyMorning.toString().substring(0, 3);
// 得到当天日期下标索引
int index = 0;
for (String s : WEEK_EN) {
if (s.equals(week)) {
break;
}
index++;
}
// 周一00:00:00时间戳
return earlyMorning.getTime() - 86400000L * index;
}
public static long getSunTime() {
return getMonTime() + 86400000L * 7;
}
}



