Java时间日期格式转换Date转String和String转Date

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; public class DateUtil { private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); /*
获取昨天的日期
*/
public static Date getYesterday(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE,-1);
Date yesterday = cal.getTime();
return yesterday;
} /*
获取传入时间的日份
*/
public static int getDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DATE);
} /**
* 获取传入时间的月份
*/
public static int getMonth(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH) + 1;
} /**
* 获取传入时间的季度
*/
public static int getQuarter(Date date) {
int month = getMonth(date);
if (month <= 3)
return 1;
else if (month <= 6)
return 2;
else if (month <= 9)
return 3;
else
return 4;
} /**
* 获取传入时间的年份
*/
public static int getYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
} /**
* 获取传入时间月份天数
*/
public static int getCurrentMonthDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, 1);
cal.roll(Calendar.DATE, -1);
return cal.get(Calendar.DATE);
} /**
* 获得传入时间为是星期几
*/
public static int getWeekDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int weekDay = c.get(Calendar.DAY_OF_WEEK);
return weekDay - 1;
} /*
获取所在周指定星期几的日期Date
*/
public static Date getWeekDayDate(Date date, int num) {
Calendar c = Calendar.getInstance();
c.setTime(date);
switch (num) {
case 1:
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
break;
case 2:
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
break;
case 3:
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
break;
case 4:
c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
break;
case 5:
c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
break;
case 6:
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
break;
case 7:
// c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
int weekday = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DATE, 8 - weekday);
break;
}
return c.getTime();
} /*
获取传入时间所在周第一天,周一
*/
public static Date getFirstWeekDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
try {
int weekday = c.get(Calendar.DAY_OF_WEEK) - 2;
c.add(Calendar.DATE, -weekday);
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
} /*
获取传入时间所在周最后一天,周日
*/
public static Date getLastWeekDay(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
try {
int weekday = c.get(Calendar.DAY_OF_WEEK);
c.add(Calendar.DATE, 8 - weekday);
} catch (Exception e) {
e.printStackTrace();
}
return c.getTime();
} /**
* 根据日期获取月初第一天
*/
public static Date getFirstMonthDay(Date date) {
Calendar cal = Calendar.getInstance();// 获取当前日期
cal.setTime(date);
cal.add(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
} /**
* 根据日期获取月末最后一天
*/
public static Date getLastMonthDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
return cal.getTime();
} /*
根据日期获取所在季度的第一天
*/
public static Date getFirstQuarterDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int currentMonth = cal.get(Calendar.MONTH) + 1;
try {
if (currentMonth >= 1 && currentMonth <= 3)
cal.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
cal.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
cal.set(Calendar.MONTH, 6);
else if (currentMonth >= 10 && currentMonth <= 12)
cal.set(Calendar.MONTH, 9);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
} catch (Exception e) {
e.printStackTrace();
}
return cal.getTime();
} /*
根据日期获取所在季度的最后一天
*/
public static Date getLastQuarterDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int currentMonth = cal.get(Calendar.MONTH) + 1;
try {
if (currentMonth >= 1 && currentMonth <= 3)
cal.set(Calendar.MONTH, 2);
else if (currentMonth >= 4 && currentMonth <= 6)
cal.set(Calendar.MONTH, 5);
else if (currentMonth >= 7 && currentMonth <= 9)
cal.set(Calendar.MONTH, 8);
else if (currentMonth >= 10 && currentMonth <= 12)
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
} catch (Exception e) {
e.printStackTrace();
}
return cal.getTime();
} /*
所在年前/后半年开始日期
*/
public static Date getHalfYearStartTime(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int currentMonth = c.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 6) {
c.set(Calendar.MONTH, 0);
} else if (currentMonth >= 7 && currentMonth <= 12) {
c.set(Calendar.MONTH, 6);
}
c.set(Calendar.DATE, 1);
return c.getTime();
} /**
所在年前/后半年的结束时间
*/
public static Date getHalfYearEndTime(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int currentMonth = c.get(Calendar.MONTH) + 1;
if (currentMonth >= 1 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
return c.getTime();
} /**
* 根据日期获取年第一天
*/
public static Date getYearFirstDay(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year=calendar.get(Calendar.YEAR);
Calendar cd = Calendar.getInstance();
cd.clear();
cd.set(Calendar.YEAR, year);
cd.roll(Calendar.DAY_OF_YEAR,0);
Date currYearLast = cd.getTime();
return currYearLast;
} /*
获取当月指定天数日期
*/
public static Date getDateByDay(Date date, int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.DATE, day);
return cal.getTime();
} /*
获取上月指定天数日期
*/
public static Date getLastMonthByDay(Date date, int day) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
cal.set(Calendar.MONTH, month - 1);
cal.set(Calendar.DATE, day);
return cal.getTime();
} /**
* 获取传入时间所在季度总天数
*/
public static int getCurrentQuarterDay(Date date) {
Date firstDayDate = getFirstQuarterDay(date);
Date lastDayDate = getLastQuarterDay(date);
int day = (int) ((lastDayDate.getTime() - firstDayDate.getTime()) / (24*3600*1000) + 1);
return day;
} /*
判断日期是否为周日
*/
public static boolean isWeekEnd(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
return true;
} else{
return false;
}
} /*
判断日期是否为月末
*/
public static boolean isMonthEnd(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int getDay = calendar.get(Calendar.DAY_OF_MONTH);
if(getDay == calendar.getActualMaximum(Calendar.DAY_OF_MONTH)){
return true;
}else{
return false;
}
} /*
判断日期是否为季末(最后一天)
*/
public static boolean isQuarterEnd(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int currentMonth = c.get(Calendar.MONTH) + 1;
Date dt = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
if (currentMonth >= 1 && currentMonth <= 3) {
c.set(Calendar.MONTH, 2);
c.set(Calendar.DATE, 31);
} else if (currentMonth >= 4 && currentMonth <= 6) {
c.set(Calendar.MONTH, 5);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 7 && currentMonth <= 9) {
c.set(Calendar.MONTH, 8);
c.set(Calendar.DATE, 30);
} else if (currentMonth >= 10 && currentMonth <= 12) {
c.set(Calendar.MONTH, 11);
c.set(Calendar.DATE, 31);
}
dt = sdf.parse(sdf.format(c.getTime()));
} catch (Exception e) {
e.printStackTrace();
}
if (date.compareTo(dt) == 0) {
return true;
} else {
return false;
}
} /*
据日期获取去年年末
*/
public static Date getLastYearLastDay(Date date){
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year=calendar.get(Calendar.YEAR)-1;
Calendar cd = Calendar.getInstance();
cd.clear();
cd.set(Calendar.YEAR, year);
cd.roll(Calendar.DAY_OF_YEAR, -1);
Date currYearLast = cd.getTime();
return currYearLast;
} /**
* 属于本年第几天
*/
public static int getYearDayIndex(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_YEAR);
} /**
* 属于本年第几周
*/
public static int getYearWeekIndex(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.setTime(date);
return calendar.get(Calendar.WEEK_OF_YEAR);
} /*
获取两个日期间的天数,直接传入Date
*/
public static Long getDayByDates(Date startDate, Date endDate) {
Long day = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
return day;
} /*
获取两个日期间的天数,直接传入StrDate
*/
public static Long getDayByStrDates(String startStrDate, String endStrDate) throws ParseException {
Date startDate = sdf.parse(startStrDate);
Date endDate = sdf.parse(endStrDate);
Long day = (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
return day;
} /**
* 判断是否闰年
* 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
* 3.能被4整除同时能被100整除则不是闰年
*/
public static boolean isLeapYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int year = cal.get(Calendar.YEAR);
if ((year % 400) == 0) {
return true;
} else if ((year % 4) == 0) {
if ((year % 100) == 0) {
return false;
} else {
return true;
}
} else {
return false;
}
} public static void main(String[] args) throws ParseException {
Date date = new Date();
System.out.println("前一天日期为:" + sdf.format(getYesterday(date)));
System.out.println("该日期的日份:" + getDay(date));
System.out.println("该日期的月份:" + getMonth(date));
System.out.println("该日期的季度:" + getQuarter(date));
System.out.println("该日期的年份:" + getYear(date));
System.out.println("所在月总天数:" + getCurrentMonthDay(date));
System.out.println("所在季度总天数:" + getCurrentQuarterDay(date));
System.out.println("该日期为本周星期:" + getWeekDay(date));
System.out.println("所在周的星期天的日期:" + sdf.format(getWeekDayDate(date, 7)));
System.out.println("所在周第一天日期:" + sdf.format(getFirstWeekDay(date)));
System.out.println("所在周最后一天日期:" + sdf.format(getLastWeekDay(date)));
System.out.println("所在月第一天日期:" + sdf.format(getFirstMonthDay(date)));
System.out.println("所在月最后一天日期:" + sdf.format(getLastMonthDay(date)));
System.out.println("所在季度第一天日期:" + sdf.format(getFirstQuarterDay(date)));
System.out.println("所在季度最后一天日期:" + sdf.format(getLastQuarterDay(date)));
System.out.println("所在年前/后半年开始日期:" + sdf.format(getHalfYearStartTime(date)));
System.out.println("所在年前/后半年结束日期:" + sdf.format(getHalfYearEndTime(date)));
System.out.println("所在年第一天日期:" + sdf.format(getYearFirstDay(date)));
System.out.println("所在月指定天数日期:" + sdf.format(getDateByDay(date, 10)));
System.out.println("上月指定天数日期:" + sdf.format(getLastMonthByDay(date, 10)));
System.out.println("是否为周日:" + isWeekEnd(date));
System.out.println("是否为月末:" + isMonthEnd(date));
System.out.println("是否为季末:" + isQuarterEnd(date));
System.out.println("去年最后一天日期:" + sdf.format(getLastYearLastDay(date)));
System.out.println("属于本年第几天:" + getYearDayIndex(date));
System.out.println("属于本年第几周:" + getYearWeekIndex(date));
System.out.println("两个日期的间隔天数:" + getDayByDates(sdf.parse("2019-10-28"), date));
System.out.println("两个日期的间隔天数:" + getDayByStrDates("2019-10-23", "2019-10-30"));
System.out.println("是否为闰年:" + isLeapYear(date));
}
}

  测试结果:

前一天日期为:2019-10-29
该日期的日份:30
该日期的月份:10
该日期的季度:4
该日期的年份:2019
所在月总天数:31
所在季度总天数:92
该日期为本周星期:3
所在周的星期天的日期:2019-11-03
所在周第一天日期:2019-10-28
所在周最后一天日期:2019-11-03
所在月第一天日期:2019-10-01
所在月最后一天日期:2019-10-31
所在季度第一天日期:2019-10-01
所在季度最后一天日期:2019-12-31
所在年前/后半年开始日期:2019-07-01
所在年前/后半年结束日期:2019-12-31
所在年第一天日期:2019-01-01
所在月指定天数日期:2019-10-10
上月指定天数日期:2019-09-10
是否为周日:false
是否为月末:false
是否为季末:false
去年最后一天日期:2018-12-31
属于本年第几天:303
属于本年第几周:44
两个日期的间隔天数:2
两个日期的间隔天数:7
是否为闰年:false

  

上一篇:MySql技术内幕之MySQL入门(2)


下一篇:在Spring MVC 中配置自定义的类型转换器