日期工具类

  作者:记性不好的阁主

1、日期父类封装


DateUtilP.java


package cn.ucmed.merger.util.parent;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;

public class DateUtilP {
public static final int sdff1_type = 1;
public static final int sdff2_type = 2;
public static final int sdff3_type = 3;
public static final int sdff4_type = 4;
public static final int sdff5_type = 5;
public static final int sdff6_type = 6;
private static final Logger LOG = Logger.getLogger(DateUtilP.class);
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
private static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
private static final SimpleDateFormat yyyy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
private static final SimpleDateFormat MM_dd = new SimpleDateFormat("MM-dd");
private static final SimpleDateFormat yyyyMMddHHmm = new SimpleDateFormat("yyyyMMddHHmm");
private static final SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
private static final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat sdf3 = new SimpleDateFormat("HHmm");
private static final SimpleDateFormat sdf5 = new SimpleDateFormat("HH:mm");
private static final SimpleDateFormat sdf4 = new SimpleDateFormat("yy-MM-dd HH:mm");
private static final SimpleDateFormat yyyyMMddhhmmssSSS = new SimpleDateFormat("yyyyMMddhhmmssSSS");
private static final SimpleDateFormat yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
private static final SimpleDateFormat sdff1 = new SimpleDateFormat("MMdd");
private static final SimpleDateFormat sdff2 = new SimpleDateFormat("MMdd HH:mm");
private static final SimpleDateFormat sdff3 = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
private static final SimpleDateFormat sdff4 = new SimpleDateFormat("yyyyMMdd HHmmss");
private static final SimpleDateFormat sdff5 = new SimpleDateFormat("yyyyMMdd E HH:mm:ss");
private static final SimpleDateFormat sdff6 = new SimpleDateFormat("yyyyMMdd E HHmmss");

public DateUtilP() {
}

public static String simpleDate3(Date date) {
return date == null ? "" : yyyyMMddHHmmss.format(date);
}

public static String simpleDate(Date date) {
return date == null ? "" : sdf.format(date);
}

public static String simpleDate1(String date1) {
return date1 == null ? "" : sdf.format(date1);
}

public static String simpleDate2(Date date) {
return yyyyMMddhhmmssSSS == null ? null : yyyyMMddhhmmssSSS.format(date);
}

public static String getCurrentTime() {
Calendar calendar = Calendar.getInstance();
int i = calendar.get(1);
int j = calendar.get(2) + 1;
int k = calendar.get(5);
return "" + i + (j >= 10 ? "" + j : "0" + j) + (k >= 10 ? "" + k : "0" + k);
}

public static Date calculateDate(Date startDay, int days) {
Calendar cal = Calendar.getInstance();
cal.setTime(startDay);
cal.add(6, days);
return cal.getTime();
}

public static Date calculateMonth(Date startDay, int months) {
Calendar cal = Calendar.getInstance();
cal.setTime(startDay);
cal.add(2, months);
return cal.getTime();
}

public static String getCurrentDateTime() {
return dateToString4(getCurrentDate());
}

public static Date getCurrentDate() {
return new Date(System.currentTimeMillis());
}

public static String getToday() {
Calendar calendar = Calendar.getInstance();
return getyyyyMMdd(calendar.getTime());
}

public static String getTomorrow() {
Calendar calendar = Calendar.getInstance();
calendar.add(5, 1);
return getyyyyMMdd(calendar.getTime());
}

public static String getDateByDays(Integer days) {
Calendar calendar = Calendar.getInstance();
calendar.add(6, days);
return getyyyyMMdd(calendar.getTime());
}

public static String getDateByDays2(Integer days) {
Calendar calendar = Calendar.getInstance();
calendar.add(6, days);
return getyyyy_MM_dd(calendar.getTime());
}

/** @deprecated */
@Deprecated
public static String dateToString(Date date, String pattern) {
return date == null ? "" : (new SimpleDateFormat(pattern)).format(date);
}

public static String getyyyyMMdd(Date date) {
return date == null ? "" : yyyyMMdd.format(date);
}

public static String getyyyy_MM_dd(Date date) {
return date == null ? "" : yyyy_MM_dd.format(date);
}

public static String getMM_dd(Date date) {
return date == null ? "" : MM_dd.format(date);
}

public static Date StringToDate(String date) {
try {
return yyyyMMdd.parse(date);
} catch (ParseException var2) {
return new Date();
}
}

public static String format1(Date date) {
if (date == null) {
date = new Date();
}

return sdf1.format(date);
}

public static String dateToString1(Date date) {
try {
return sdf1.format(date);
} catch (Exception var2) {
LOG.debug(var2);
return null;
}
}

public static Date StringToDate1(String date) {
try {
return sdf1.parse(date);
} catch (ParseException var2) {
LOG.debug(var2);
return new Date();
}
}

public static Date StringToDate5(String date) {
try {
return sdf.parse(date);
} catch (ParseException var2) {
LOG.debug(var2);
return new Date();
}
}

public static Date StringToDate6(String date) {
try {
return yyyy_MM_dd.parse(date);
} catch (Exception var2) {
LOG.debug(var2);
return new Date();
}
}

public static Date StringToDate2(String date) {
try {
return yyyyMMddHHmm.parse(date);
} catch (ParseException var2) {
LOG.debug(var2);
return new Date();
}
}

public static Date StringToDate3(String date) {
try {
return yyyyMMdd.parse(date);
} catch (ParseException var2) {
LOG.debug(var2);
return new Date();
}
}

public static Date StringToDate4(String date) {
try {
return sdf2.parse(date);
} catch (ParseException var2) {
LOG.debug(var2);
return new Date();
}
}

public static Date StringToDate7(String date) {
try {
return yyyyMMddHHmmss.parse(date);
} catch (ParseException var2) {
LOG.debug(var2);
return new Date();
}
}

public static String dateToString4(Date date) {
try {
return sdf2.format(date);
} catch (Exception var2) {
LOG.debug(var2);
return null;
}
}

public static String getHHmm(Date date) {
try {
return sdf3.format(date);
} catch (Exception var2) {
LOG.debug(var2);
return null;
}
}

public static String getHHmm1(Date date) {
try {
return sdf5.format(date);
} catch (Exception var2) {
LOG.debug(var2);
return null;
}
}

public static String dateToString2(Date date) {
try {
return sdf4.format(date);
} catch (Exception var2) {
LOG.debug(var2);
return null;
}
}

public static Integer getWeekId(String date) {
Date d = new Date();

try {
d = yyyyMMdd.parse(date);
} catch (ParseException var4) {
LOG.debug(var4);
}

Calendar calendar = Calendar.getInstance();
calendar.setTime(d);
Integer i = calendar.get(7) - 1;
return i;
}

public static String getWeek(String date) {
String week = "";
switch(getWeekId(date)) {
case 0:
week = "周日";
break;
case 1:
week = "周一";
break;
case 2:
week = "周二";
break;
case 3:
week = "周三";
break;
case 4:
week = "周四";
break;
case 5:
week = "周五";
break;
case 6:
week = "周六";
}

return week;
}

public static Date getDate(Date date, int n) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
gc.add(5, n);
return gc.getTime();
}

public static Date getDate(Date date, int field, int n) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
gc.add(field, n);
return gc.getTime();
}

public static String getDate(Date date, Date updateTime) {
String str = "";
Calendar calendar1 = new GregorianCalendar();
calendar1.setTime(date);
Calendar calendar2 = new GregorianCalendar();
calendar2.setTime(updateTime);
Date date1 = minuteAdd(date, -1);
Date date2 = hourAdd(date, -1);
Date date3 = dayAdd(date, -1);
Date date4 = dayAdd(date, -2);
if (updateTime.after(date4)) {
str = "昨天" + getHHmm1(updateTime);
if (updateTime.after(date3)) {
int hour = 24 + calendar1.get(11) - calendar2.get(11);
if (hour > 24) {
hour -= 24;
}

str = hour + "小时前";
if (updateTime.after(date2)) {
int minute = 60 + calendar1.get(12) - calendar2.get(12);
if (minute > 60) {
minute -= 60;
}

str = minute + "分钟前";
if (updateTime.after(date1)) {
int second = 60 + calendar1.get(13) - calendar2.get(13);
if (second > 60) {
second -= 60;
}

str = second + "秒前";
}
}
}
} else {
str = format1(updateTime);
}

return str;
}

public static Date yearAdd(Date date, int n) {
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(1, n);
return cl.getTime();
}

public static Date monthAdd(Date date, int n) {
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(2, n);
return cl.getTime();
}

public static Date dayAdd(Date date, int n) {
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(5, n);
return cl.getTime();
}

public static Date hourAdd(Date date, int n) {
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(11, n);
return cl.getTime();
}

public static Date minuteAdd(Date date, int n) {
Calendar cl = Calendar.getInstance();
cl.setTime(date);
cl.add(12, n);
return cl.getTime();
}

public static int judgeQuestionTime(Date createTime) {
Date nowTime = new Date();
Date yesTime = dayAdd(nowTime, -1);
Date yes14 = StringToDate4((new SimpleDateFormat("yyyy-MM-dd 14:00:00")).format(yesTime));
Date yes20 = StringToDate4((new SimpleDateFormat("yyyy-MM-dd 20:00:00")).format(yesTime));
Date now06 = StringToDate4((new SimpleDateFormat("yyyy-MM-dd 06:00:00")).format(nowTime));
Date now12 = StringToDate4((new SimpleDateFormat("yyyy-MM-dd 12:00:00")).format(nowTime));
Date now20 = StringToDate4((new SimpleDateFormat("yyyy-MM-dd 20:00:00")).format(nowTime));
if (createTime.before(yes14)) {
return 1;
} else {
Long t;
if (createTime.after(yes14) && createTime.before(yes20)) {
t = yes20.getTime() - createTime.getTime();
if (nowTime.before(now06)) {
return 0;
}

if (nowTime.after(now06) && nowTime.before(now12)) {
t = t + nowTime.getTime() - now06.getTime();
if (t > 21600000L) {
return 1;
}

return 0;
}

if (nowTime.after(now12)) {
return 1;
}
} else if (createTime.after(yes20) && createTime.before(now06)) {
if (nowTime.before(now06)) {
return 0;
}

if (nowTime.after(now06) && nowTime.before(now12)) {
t = nowTime.getTime() - now06.getTime();
if (t > 21600000L) {
return 1;
}

return 0;
}

if (nowTime.after(now12)) {
return 1;
}
} else {
if (createTime.after(now06) && createTime.before(now20)) {
t = nowTime.getTime() - createTime.getTime();
if (t > 21600000L) {
return 1;
}

return 0;
}

if (createTime.after(now20)) {
return 0;
}
}

return 1;
}
}

public static boolean isValidDate(String sDate) {
String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
if (sDate != null) {
Pattern pattern = Pattern.compile(datePattern1);
Matcher match = pattern.matcher(sDate);
if (match.matches()) {
pattern = Pattern.compile(datePattern2);
match = pattern.matcher(sDate);
return match.matches();
} else {
return false;
}
} else {
return false;
}
}

public static String foramtDate(String date) {
return date.indexOf(" ") != -1 ? date.split(" ")[0] : date;
}

public static Date parseDate2(String date) {
try {
return sdf4.parse(date);
} catch (ParseException var2) {
LOG.debug(var2);
return new Date();
}
}

public static String getStringDate(int formatType, Date date) {
SimpleDateFormat sdf = null;
if (formatType == 1) {
sdf = sdff1;
} else if (formatType == 2) {
sdf = sdff2;
} else if (formatType == 3) {
sdf = sdff3;
} else if (formatType == 4) {
sdf = sdff4;
} else if (formatType == 5) {
sdf = sdff5;
} else if (formatType == 6) {
sdf = sdff6;
}

return sdf.format(date);
}
}



2、日期工具类


DateUtil.java


package cn.ucmed.merger.util;

import cn.ucmed.merger.util.parent.DateUtilP;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;

/**
* @author Ye Zongxiu
* @ClassName DateUtil
* @Description
* @date 2017091310:44
*/
public class DateUtil extends DateUtilP {

private static final Logger LOG = LoggerFactory.getLogger(DateUtil.class);
private static final String HH_MM = "HH:mm";
private static final String YYYY_MM = "yyyy-MM";
private static final String YYYY_MM_DD = "yyyy-MM-dd";
private static final SimpleDateFormat YYYYMMDD = new SimpleDateFormat("YYYYMMDD");
private static final SimpleDateFormat MMDD = new SimpleDateFormat("MMDD");
private static final SimpleDateFormat yyyy_MM_dd_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

/**
* 锁对象
*/
private static final Object LOCK_OBJECT = new Object();

/**
* 存放不同的日期模板格式的sdfMap
*/
private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<>();


/**
* 返回一个ThreadLocalsdf,每个线程只会new一次sdf
*
* @param pattern 日期格式
* @return SimpleDateFormat对象
*/
private static SimpleDateFormat getSdf(final String pattern) {
ThreadLocal<SimpleDateFormat> tl = sdfMap.get(pattern);
// 此处的双重判断和同步是为了防止sdfMap这个单例被多次put重复的sdf
if (tl == null) {
synchronized (LOCK_OBJECT) {
tl = sdfMap.get(pattern);
if (tl == null) {
// 只有Map中还没有这个patternsdf才会生成新的sdf并放入map
tl = ThreadLocal.withInitial(() -> new SimpleDateFormat(pattern));
sdfMap.put(pattern, tl);
}
}
}

return tl.get();
}

/**
* 获取一周日期
*/
public static JSONArray getWeekdays(int weekCount) {
JSONArray list = new JSONArray();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, 7 * weekCount);
for (int i = 0; i < 7; i++) {
String date = getyyyy_MM_dd(calendar.getTime());
list.add(date);
calendar.add(Calendar.DATE, 1);
}
return list;
}

/**
* String 获取今天日期yyyy-MM-dd
*/
public static String getyyyyMMddToday() {
Calendar calendar = Calendar.getInstance();
return getyyyy_MM_dd(calendar.getTime());
}

public static String getyyyyMMddYesterda(){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE,-1);
return getyyyy_MM_dd(calendar.getTime());
}

/**
* String 获取日期yyyy-MM-dd
*/
public static String getYyyyMmDd(Date date) {
return getSdf(YYYY_MM_DD).format(date);
}

/**
* String 获取日期yyyy-MM
*/
public static String getyyyyMM(Date date) {
return getSdf(YYYY_MM).format(date);
}

/**
* 判断两个HH:mm相差多少分钟
*/
public static Long getMinute(String startTime, String endTime) {
try {
Date start = getSdf("HH:MM").parse(startTime);
Date end = getSdf(HH_MM).parse(endTime);
GregorianCalendar calendar1 = new GregorianCalendar();
GregorianCalendar calendar2 = new GregorianCalendar();
calendar1.setTime(start);
calendar2.setTime(end);
return (calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / (1000 * 60);
} catch (Exception e) {
LOG.error("获取时间相差分钟错误", e);
return 0L;
}
}

/**
* 判断时间是否在两个HH:mm时间之间(minute时间偏移)
*/
public static Boolean checkTime(String startTime, String endTime, Integer minute) {
try {
String today = getyyyyMMddToday() + " ";
Date start = StringToDate1(today + startTime);
Date end = StringToDate1(today + endTime);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MINUTE, minute);
Date date = calendar.getTime();
if (start.getTime() < date.getTime() && date.getTime() <= end.getTime()) {
return true;
}
} catch (Exception e) {
LOG.error("判断是否在预约时间段错误", e);
}
return false;
}

/**
* 判断两个HH:mm大小,时间1大于等于时间2,则返回true
*/
public static Boolean compareTime(String time1, String time2) {
try {
String today = getyyyyMMddToday() + " ";
Date date1 = StringToDate1(today + time1);
Date date2 = StringToDate1(today + time2);
if (date1.getTime() >= date2.getTime()) {
return true;
}
} catch (Exception e) {
LOG.error("判断时间大小错误", e);
}
return false;
}

/**
* 判断两个日期时间大小,时间1大于等于时间2,则返回true
*/
public static Boolean compareDateAndTime(Date time1, Date time2) {
try {
if (time1.getTime() >= time2.getTime()) {
return true;
}
} catch (Exception e) {
LOG.error("判断时间大小错误", e);
}
return false;
}

/**
* 获取前月的第一天
*/
public static Date getBeginOfMonth(Date date) {
Calendar cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.MONTH, 0);
cale.set(Calendar.DAY_OF_MONTH, 1);
return cale.getTime();
}

/**
* 获取前月的最后一天
*/
public static Date getEndOfMonth(Date date) {
Calendar cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.MONTH, 1);
cale.set(Calendar.DAY_OF_MONTH, 0);
return cale.getTime();
}

/**
* 获取前月的第一天
*/
public static Date getBeginOfYear(Date date) {
Calendar cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.YEAR, 0);
cale.set(Calendar.DAY_OF_YEAR, 1);
return cale.getTime();
}

/**
* 获取前月的最后一天
*/
public static Date getEndOfYear(Date date) {
Calendar cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.YEAR, 1);
cale.set(Calendar.DAY_OF_YEAR, 0);
return cale.getTime();
}

/**
* 判断日期是否在今天或者今天之后,yyyy-MM-dd
*
* @param dateStr 日期
* @return the boolean
*/
public static Boolean compareToday(String dateStr) throws ParseException {
Date date = getSdf(YYYY_MM_DD).parse(dateStr);
Date today = getSdf(YYYY_MM_DD).parse(getyyyyMMddToday());
return compareDateAndTime(date, today);
}

/**
* 获取日期指定天数后的日期
*
* @param dateStr 日期
* @param day 天数
* @return 指定天数后的日期
*/
public static String afterDate(String dateStr, Integer day) throws ParseException {
Date date = getSdf(YYYY_MM_DD).parse(dateStr);
Calendar cale = Calendar.getInstance();
cale.setTime(date);
cale.add(Calendar.DAY_OF_MONTH, day);
return getyyyy_MM_dd(cale.getTime());
}

/***
* 将某一日期偏移一定时间.
* @param startDay 日期.
* @param days 偏移的天数,可以为负数.
* @return 偏移后的天数或者null.
*/
public static Date calculateDate(Date startDay, int days) {
if(null==startDay){
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(startDay);
cal.add(Calendar.DAY_OF_YEAR, days);
return cal.getTime();
}

/***
* 将某一日期偏移一定时间.
* @param startDay 日期.
* @param month 偏移的月,可以为负数.
* @return 偏移后的天数或者null.
*/
public static Date calculateDateByMonth(Date startDay, int month) {
if(null==startDay){
return null;
}
Calendar cal = Calendar.getInstance();
cal.setTime(startDay);
cal.add(Calendar.MONTH, month);
return cal.getTime();
}

/**
* 显示时间,如果与当前时间差别小于一天,则自动用**(分,小时)前,如果大于一天则用format规定的格式显示
*
* @param ctime 时间
* @return
* @author yujj
*/
public static String showTime(Date ctime) {
String r = "";
if (ctime == null) {
return r;
}
String format;
long nowtimelong = System.currentTimeMillis();

long ctimelong = ctime.getTime();
long result = Math.abs(nowtimelong - ctimelong);

if (result < 60000) {// 一分钟内
long seconds = result / 1000;
r = seconds + "";
} else if (result >= 60000 && result < 3600000) {// 一小时内
long seconds = result / 60000;
r = seconds + "分钟";
} else if (result >= 3600000 && result < 86400000) {// 一天内
long seconds = result / 3600000;
r = seconds + "小时";
} else if (result >= 86400000 && result < 1702967296) {// 三十天内
long seconds = result / 86400000;
r = seconds + "";
} else {// 日期格式
format = "MM-dd HH:mm";
SimpleDateFormat df = new SimpleDateFormat(format);
r = df.format(ctime);
}
return r;
}

public static String getYYMMDD(Date date) {
if (date == null)
return "";
return YYYYMMDD.format(date);
}

public static String getMMDD(Date date) {
if (date == null)
return "";
return MMDD.format(date);
}

public static String dateToStringyyyyMMddHHmmss(Date date) {
if (date == null)
return "";
return yyyy_MM_dd_HH_mm_ss.format(date);
}

public static Date stringToDateyyyyMMddHHmmss(String date) {
if (date == null)
return null;
try {
return yyyy_MM_dd_HH_mm_ss.parse(date);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}

/**
* 显示时间,如果与当前时间差别小于一天,则自动用**(分,小时)前,如果大于一天则用format规定的格式显示
*
* @param ctime 时间
* @return
* @author yujj
*/
public static String showTimeFromNow(Date ctime) {
String str = "";
if (ctime == null) {
return str;
}
long nowTimeLong = System.currentTimeMillis();
long ctimelong = ctime.getTime();
long time = Math.abs(nowTimeLong - ctimelong);
if (time < 60000) {
str = (time / 1000 + "");
}
if (time >= 86400000) {
long seconds = time / 86400000;
str += (seconds + "");
time %= 86400000;
}
if (time >= 3600000) {
long seconds = time / 3600000;
str += (seconds + "小时");
time %= 3600000;
}
if (time >= 60000) {
long seconds = time / 60000;
str += (seconds + "分钟");
}
return str;
}

public static String getWeekDay(int i) {
if (i == 0) {
return "今天";
}
LocalDate date = LocalDate.now().plusDays(i);
DayOfWeek day = date.getDayOfWeek();
switch (day) {
case MONDAY:
return "周一";
case TUESDAY:
return "周二";
case WEDNESDAY:
return "周三";
case THURSDAY:
return "周四";
case FRIDAY:
return "周五";
case SATURDAY:
return "周六";
case SUNDAY:
return "周日";
default:
return null;
}
}

/**
* 获取date开始n天日期
* @param date 日期
* @param n
* @param format 日期格式
* @return
*/
public static List<String> getDateListAfterDate(Date date, Integer n, String format){
List<String> list = new ArrayList<>();
Calendar cale = Calendar.getInstance();
for(int i = 0 ; i < n ; i ++){
cale.setTime(date);
cale.add(Calendar.DAY_OF_MONTH, i);
list.add(new SimpleDateFormat(format).format(cale.getTime()));
}
return list;
}

/**
* 获取date开始n天星期
* @param date
* @param n
* @param type 1 今天和星期X 2 今日和周X
* @return
*/
public static List<String> getWeekListAfterDate(Date date, Integer n, Integer type) {
List<String> list = new ArrayList<>();
Calendar cale = Calendar.getInstance();
String d1 = dateFormat(new Date(), "yyyy-MM-dd");
for(int i = 0 ; i < n ; i ++){
cale.setTime(date);
cale.add(Calendar.DAY_OF_MONTH, i);
Date da = cale.getTime();
String d2 = dateFormat(da, "yyyy-MM-dd");
SimpleDateFormat dateFm = new SimpleDateFormat("EEEE",Locale.CHINA);
String currSun = dateFm.format(da);
if(type == 1 || type == 2){
if(d1.equals(d2)){
if(type == 1){
currSun = "今天";
}else{
currSun = "今日";
}
}
if(type == 2){
currSun = currSun.replace("星期", "");
}
}
list.add(currSun);
}
return list;
}

/**
* 获取本周一日期
* @return
*/
public static Date getThisWeekMonday(){
Date today = new Date();
Calendar c =Calendar.getInstance();
c.setTime(today);
int weekday = c.get(Calendar.DAY_OF_WEEK);
c.setFirstDayOfWeek(Calendar.MONDAY);
int day = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DATE, c.getFirstDayOfWeek() - day);
return c.getTime();
}
public static String dateFormat(Date date, String sdf) {
if (date == null){
return "";
}
return new SimpleDateFormat(sdf).format(date);
}

public static Date stringToDate(String date, String sdf) {
try {
return new SimpleDateFormat(sdf).parse(date);
} catch (ParseException e) {
}
return null;
}

public static void main(String[] args) throws ParseException {
System.out.println(getyyyyMMddToday());
}


}



相关推荐

评论 抢沙发

表情

分类选择