/**
* String转为Date
* @param str
* @return
*/
public static Date stringToDate(String str) {
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* date转为String
* @param date
* @return
*/
public static String dateToString(Date date){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
String dateStr=sdf.format(date);
return dateStr;
}
/**
* 比较两个时间大小
* @param date1
* @param date2
* @return
*/
public static int compare_date(String date1, String date2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(date1);
Date dt2 = df.parse(date2);
if (dt1.getTime() > dt2.getTime()) {
System.out.println("dt1>dt2");
return 1;
} else if (dt1.getTime() < dt2.getTime()) {
System.out.println("dt1<dt2");
return -1;
} else {
return 0;
}
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
/**
* 日期加一天
* @param date
* @return
*/
public static Date addDateOneDay(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date); //设置当前日期
c.add(Calendar.DATE, 1); //日期加1天
date = c.getTime();
return date;
}
/**
* 日期加一天
* @param dateStr
* @return
*/
public static String addDateOneDay(String dateStr){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar c = Calendar.getInstance();
c.setTime(date); //设置当前日期
c.add(Calendar.DATE, 1); //日期加1天
date = c.getTime();
SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");
String dateStr2=sdf2.format(date);
return dateStr2;
}
/**
* 日期减一天
* @param date
* @return
*/
public static Date minusDateOneDay(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date); //设置当前日期
c.add(Calendar.DATE, -1); //日期减1天
date = c.getTime();
return date;
}
/**
* 日期减一天
* @param dateStr
* @return
*/
public static String minusDateOneDay(String dateStr){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar c = Calendar.getInstance();
c.setTime(date); //设置当前日期
c.add(Calendar.DATE, -1); //日期减1天
date = c.getTime();
SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");
String dateStr2=sdf2.format(date);
return dateStr2;
}
/**
* 得到指定日期的第一天(周一)
* @param date
* @return
*/
public static Date getMonday(Date date){
Calendar cDay = Calendar.getInstance();
// cDay.setTime(date);
// cDay.set(Calendar.DAY_OF_WEEK, 2);//老外将周日定位第一天,周一取第二天
cDay.setFirstDayOfWeek(Calendar.MONDAY);
cDay.setTime(date);
cDay.set(Calendar.DAY_OF_WEEK, cDay.getFirstDayOfWeek());
return cDay.getTime();
}
/**
* 得到指定日期的第一天(周一)
* @param dateStr
* @return
*/
public static String getMonday(String dateStr){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cDay = Calendar.getInstance();
cDay.setFirstDayOfWeek(Calendar.MONDAY);
cDay.setTime(date);
cDay.set(Calendar.DAY_OF_WEEK, cDay.getFirstDayOfWeek());
Date date2 = cDay.getTime();
SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");
String dateStr2=sdf2.format(date2);
return dateStr2;
}
/**
* 获得指定日期所在周日
* @param date
* @return
*/
public static Date getSunday(Date date){
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
if(cDay.get(Calendar.DAY_OF_WEEK)==cDay.getFirstDayOfWeek()){ //如果刚好是周日,直接返回
return date;
}else{//如果不是周日,加一周计算
cDay.add(Calendar.DAY_OF_YEAR, 7);
cDay.set(Calendar.DAY_OF_WEEK, 1);
return cDay.getTime();
}
}
/**
* 获得指定日期所在周日
* @param dateStr
* @return
*/
public static String getSunday(String dateStr){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
if(cDay.get(Calendar.DAY_OF_WEEK)==cDay.getFirstDayOfWeek()){ //如果刚好是周日,直接返回
return dateStr;
}else{//如果不是周日,加一周计算
cDay.add(Calendar.DAY_OF_YEAR, 7);
cDay.set(Calendar.DAY_OF_WEEK, 1);
Date date2 = cDay.getTime();
SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");
String dateStr2=sdf2.format(date2);
return dateStr2;
}
}
/**
* 获得本月第一天
* @param date
* @return
*/
public static Date getFirstDayOfMonth(Date date){
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
cDay.set(Calendar.DAY_OF_MONTH, 1);
return cDay.getTime();
}
/**
* 获得本月第一天
* @param dateStr
* @return
*/
public static String getFirstDayOfMonth(String dateStr){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
cDay.set(Calendar.DAY_OF_MONTH, 1);
Date date2 = cDay.getTime();
SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");
String dateStr2=sdf2.format(date2);
return dateStr2;
}
/**
* 获得本月最后一天
* @param date
* @return
*/
public static Date getLastDayOfMonth(Date date) {
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
return cDay.getTime();
}
/**
* 获得本月最后一天
* @param dateStr
* @return
*/
public static String getLastDayOfMonth(String dateStr){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
Date date2 = cDay.getTime();
SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");
String dateStr2=sdf2.format(date2);
return dateStr2;
}
/**
* 获得本季度第一天日期
* @param date
* @return
*/
public static Date getFirstDayOfQuarter(Date date){
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
int curMonth = cDay.get(Calendar.MONTH);
if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH){
cDay.set(Calendar.MONTH, Calendar.JANUARY);
}
if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE){
cDay.set(Calendar.MONTH, Calendar.APRIL);
}
if (curMonth >= Calendar.JULY && curMonth <= Calendar.SEPTEMBER) {
cDay.set(Calendar.MONTH, Calendar.JULY);
}
if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
cDay.set(Calendar.MONTH, Calendar.OCTOBER);
}
cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMinimum(Calendar.DAY_OF_MONTH));
return cDay.getTime();
}
/**
* 获得本季度第一天日期
* @param dateStr
* @return
*/
public static String getFirstDayOfQuarter(String dateStr){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
int curMonth = cDay.get(Calendar.MONTH);
if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH){
cDay.set(Calendar.MONTH, Calendar.JANUARY);
}
if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE){
cDay.set(Calendar.MONTH, Calendar.APRIL);
}
if (curMonth >= Calendar.JULY && curMonth <= Calendar.SEPTEMBER) {
cDay.set(Calendar.MONTH, Calendar.JULY);
}
if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
cDay.set(Calendar.MONTH, Calendar.OCTOBER);
}
cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMinimum(Calendar.DAY_OF_MONTH));
Date date2 = cDay.getTime();
SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");
String dateStr2=sdf2.format(date2);
return dateStr2;
}
/**
* 获得本季度最后一天日期
* @param date
* @return
*/
public static Date getLastDayOfQuarter(Date date){
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
int curMonth = cDay.get(Calendar.MONTH);
if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH){
cDay.set(Calendar.MONTH, Calendar.MARCH);
}
if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE){
cDay.set(Calendar.MONTH, Calendar.JUNE);
}
if (curMonth >= Calendar.JULY && curMonth <= Calendar.SEPTEMBER) {
cDay.set(Calendar.MONTH, Calendar.SEPTEMBER);
}
if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
cDay.set(Calendar.MONTH, Calendar.DECEMBER);
}
cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
return cDay.getTime();
}
/**
* 获得本季度最后一天日期
* @param dateStr
* @return
*/
public static String getLastDayOfQuarter(String dateStr){
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sdf.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Calendar cDay = Calendar.getInstance();
cDay.setTime(date);
int curMonth = cDay.get(Calendar.MONTH);
if (curMonth >= Calendar.JANUARY && curMonth <= Calendar.MARCH){
cDay.set(Calendar.MONTH, Calendar.MARCH);
}
if (curMonth >= Calendar.APRIL && curMonth <= Calendar.JUNE){
cDay.set(Calendar.MONTH, Calendar.JUNE);
}
if (curMonth >= Calendar.JULY && curMonth <= Calendar.SEPTEMBER) {
cDay.set(Calendar.MONTH, Calendar.SEPTEMBER);
}
if (curMonth >= Calendar.OCTOBER && curMonth <= Calendar.DECEMBER) {
cDay.set(Calendar.MONTH, Calendar.DECEMBER);
}
cDay.set(Calendar.DAY_OF_MONTH, cDay.getActualMaximum(Calendar.DAY_OF_MONTH));
Date date2 = cDay.getTime();
SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd");
String dateStr2=sdf2.format(date2);
return dateStr2;
}