目录
2020-06-29T16:00:00.000Z格式转换
判断今天星期几
比较两个时分秒时间的大小
获得当月1号零时零分零秒
获得下月1号
获得当天零时零分零秒
获取当月所有天
获取输入时间当月的 输入时间和下半月剩下的时间
获取输入时间 当月上半个月的时间
获取一年的 当前时间以前的所有月份
获取间隔时间
获取明天零点零分零秒
获取几天后的日期
获取几天前的日期
获取几月前的日期
获取两个时间间隔天数
2020-06-29T16:00:00.000Z格式转换
public static String dealDateFormat(String oldDateStr) {
Date date1 = null;
SimpleDateFormat df2 = null;
try {
oldDateStr = oldDateStr.replace("Z", " UTC");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
Date date = df.parse(oldDateStr);
SimpleDateFormat sdf = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
date1 = sdf.parse(date.toString());
df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} catch (ParseException e) {
e.printStackTrace();
}
return df2.format(date1);
}
判断今天星期几
public static void main(String[] args) {
String weekDate = "";
String str = "2020-12-01";
Calendar calendar = Calendar.getInstance(); // 获取当前时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
calendar.setTime(df.parse(str));
} catch (ParseException e) {
e.printStackTrace();
}
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 获取星期的第几日
switch (week) {
case 0:
weekDate = "星期日";
break;
case 1:
weekDate = "星期一";
break;
case 2:
weekDate = "星期二";
break;
case 3:
weekDate = "星期三";
break;
case 4:
weekDate = "星期四";
break;
case 5:
weekDate = "星期五";
break;
case 6:
weekDate = "星期六";
break;
}
System.out.println("今天是 " + weekDate);
}
public static int getWeek(String str) {
String weekDate = "";
Calendar calendar = Calendar.getInstance(); // 获取当前时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
calendar.setTime(df.parse(str));
} catch (ParseException e) {
e.printStackTrace();
}
int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 获取星期的第几日
// switch (week) {
// case 0:
// weekDate = "星期日";
// break;
// case 1:
// weekDate = "星期一";
// break;
// case 2:
// weekDate = "星期二";
// break;
// case 3:
// weekDate = "星期三";
// break;
// case 4:
// weekDate = "星期四";
// break;
// case 5:
// weekDate = "星期五";
// break;
// case 6:
// weekDate = "星期六";
// break;
// }
if (week == 0) {
week = 7;
}
// System.out.println("今天是 " + weekDate);
return week;
}
比较两个时分秒时间的大小
public static boolean compTime(String s1, String s2) {
try {
if (s1.indexOf(":") < 0 || s1.indexOf(":") < 0) {
System.out.println("格式不正确");
} else {
String[] array1 = s1.split(":");
int total1 = Integer.valueOf(array1[0]) * 3600 + Integer.valueOf(array1[1]) * 60 + Integer.valueOf(array1[2]);
String[] array2 = s2.split(":");
int total2 = Integer.valueOf(array2[0]) * 3600 + Integer.valueOf(array2[1]) * 60 + Integer.valueOf(array2[2]);
return total1 - total2 > 0 ? true : false;
}
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println(compTime("08:35:56", "06:33:33")); // true
}
获得当月1号零时零分零秒
/**
* 获得当月1号零时零分零秒
*/
public static String initDateByMonthBegin(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (date == null) {
date = new Date();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return df.format(calendar.getTime());
}
获得下月1号
/**
* 获得下月1号
*/
public static Date getNextDate1Copy(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
获得当天零时零分零秒
/**
* 获得当天零时零分零秒
*/
public static Date initDateByToDay(Date date) {
if (date == null) {
date = new Date();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
获取当月所有天
/**
* 获取当月所有天
*/
public static List<String> getDayListOfMonth2(Date date) {
List<String> list = new ArrayList<String>();
Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
aCalendar.setTime(date);
int year = aCalendar.get(Calendar.YEAR);//年份
int month = aCalendar.get(Calendar.MONTH) + 1;//月份
int day = aCalendar.getActualMaximum(Calendar.DATE);
for (int i = 1; i <= day; i++) {
String monthStr = String.valueOf(month);
if (month < 10) {
monthStr = "0" + month;
}
String days = String.valueOf(i);
if (i < 10) {
days = "0" + i;
}
String aDate = String.valueOf(year)+"-"+monthStr+"-"+ days;
list.add(aDate);
}
return list;
}
获取输入时间当月的 输入时间和下半月剩下的时间
/**
* 获取当月的 输入时间和往后下半月剩下的时间,包含输入时间
*/
public static List<String> getThisMonthDayListAfterDay(Date date) {
List<String> list = new ArrayList<String>();
Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
aCalendar.setTime(date);
int year = aCalendar.get(Calendar.YEAR);//年份
int month = aCalendar.get(Calendar.MONTH) + 1;//月份
int day = aCalendar.getActualMaximum(Calendar.DATE);
int start = aCalendar.get(Calendar.DATE);
for (int i = start; i <= day; i++) {
String monthStr = String.valueOf(month);
if (month < 10) {
monthStr = "0" + month;
}
String days = String.valueOf(i);
if (i < 10) {
days = "0" + i;
}
String aDate = String.valueOf(year)+"-"+monthStr+"-"+ days;
list.add(aDate);
}
return list;
}
获取输入时间 当月上半个月的时间
/**
* 获取当月的 输入时间之前上半个月的时间,包含输入时间
*/
public static List<String> getThisMonthDayListBeforeDay(Date date) {
List<String> list = new ArrayList<String>();
Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
aCalendar.setTime(date);
int year = aCalendar.get(Calendar.YEAR);//年份
int month = aCalendar.get(Calendar.MONTH) + 1;//月份
int start = aCalendar.get(Calendar.DATE);
for (int i = 1; i < start; i++) {
String monthStr = String.valueOf(month);
if (month < 10) {
monthStr = "0" + month;
}
String days = String.valueOf(i);
if (i < 10) {
days = "0" + i;
}
String aDate = String.valueOf(year)+"-"+monthStr+"-"+ days;
list.add(aDate);
}
return list;
}
获取一年的 当前时间以前的所有月份
/**
* 获取一年的 当前时间以前的所有月份
*
* @param date 日期
* @return List<String> 返回类型
* @Title: getDateMonthTimeList
* @Description: 格式化日期
*/
public static List<String> getDateMonthTimeList(Date date) {
if (date == null) {
date = new Date();
}
List<String> list = new ArrayList<String>();
String yearString = getDateString(date, "yyyy-");
String nowMonthString = getDateString(date, "yyyy-MM");
for (int i = 1; i < 13; i++) {
String time = "";
if (i < 10) {
time += yearString + "0" + i;
} else {
time += yearString + i;
}
list.add(time);
if (StringUtils.equals(time, nowMonthString)) {
break;
}
}
return list;
}
获取间隔时间
/**
* 获取间隔时间
*
* @param one 开始时间
* @param two 结束时间
* @return String 返回结束时间和开始时间的 天 /小时/分/秒
*/
public static String getDistanceTime(Date one, Date two) {
long day = 0;
long hour = 0;
long min = 0;
long sec = 0;
if (one == null || two == null) {
return "";
}
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
day = diff / (24 * 60 * 60 * 1000);
hour = (diff / (60 * 60 * 1000) - day * 24);
min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
StringBuffer res = new StringBuffer();
if (day > 0) {
return res.append(day + "天前").toString();
}
if (day > 0 || hour > 0) {
return res.append(hour + "小时前").toString();
}
if (day > 0 || hour > 0 || min > 0) {
return res.append(min + "分钟前").toString();
}
if (day > 0 || hour > 0 || min > 0 || sec > 0) {
// return res.append(sec + "秒前").toString();
return res.append("刚刚").toString();
}
return res.toString();
}
获取明天零点零分零秒
public static long getZeroPointNextDay() throws ParseException {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.DAY_OF_MONTH, 1);
String format = df.format(calendar.getTime());
long longTime = df.parse(format).getTime();
System.out.println(longTime);
System.out.println(df.format(longTime));
return longTime;
}
获取几天后的日期
public static String getDateAfterDay(String beginDate, int offsetDay) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar currDate = Calendar.getInstance();
String sourceDate = beginDate + " 00:00:00";
currDate.setTime(format2.parse(sourceDate));
currDate.add(Calendar.DATE, offsetDay);
Date d = currDate.getTime();
String beforeDate = format.format(d);
return beforeDate;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
System.out.println(getDateAfterDay("2020-12-05 12:01:25", 2)); // 2020-12-07
}
获取几天前的日期
public static String getDateBeforeDay(int beforeDay) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar currDate = Calendar.getInstance();
currDate.setTime(new Date());
currDate.add(Calendar.DATE, -beforeDay);
Date d = currDate.getTime();
String beforeDate = format.format(d);
return beforeDate;
}
获取几月前的日期
public static void main(String[] args) {
System.out.println(getDateBeforeMonth(8)); // 2020-12-05 -> 2020-04-05
}
public static String getDateBeforeMonth(int beforeMonth) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar currDate = Calendar.getInstance();
currDate.setTime(new Date());
currDate.add(Calendar.MONTH, -beforeMonth);
Date m = currDate.getTime();
String beforeDate = format.format(m);
return beforeDate;
}
获取两个时间间隔天数
/**
* 获取两个时间间隔天数,比较两天是否是第二天
* 开始时间大于结束时间返回 正数,
*/
public static int getDifferentDays2(Date beginDate, Date endDate) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = initDateByToDay(endDate);
// System.out.println("endDate: " + df.format(date));
long endTime = date.getTime();
Date date1 = initDateByToDay(beginDate);
// System.out.println("beginDate: " + df.format(date1));
long beginTime = date1.getTime();
// if (endTime < beginTime) {
// return -1;
// }
Calendar cal1 = Calendar.getInstance();
cal1.setTime(endDate);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(beginDate);
int day1= cal1.get(Calendar.DAY_OF_YEAR);
int day2 = cal2.get(Calendar.DAY_OF_YEAR);
int year1 = cal1.get(Calendar.YEAR);
int year2 = cal2.get(Calendar.YEAR);
if (year1 != year2) { //不同一年
int timeDistance = 0;
for (int i = year1; i < year2; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { //闰年
timeDistance += 366;
} else { //不是闰年
timeDistance += 365;
}
}
return timeDistance + (day2 - day1);
} else { //同一年
// System.out.println("判断day2 - day1 : " + (day2 - day1));
return day2 - day1;
}