转换日期,年月日

//处理选中日期到当前日期的前一天

/**

* 此方法用来判断日期的大小(按天计算)

* */

public int compareDate(String endTime,Date dt2) throws ParseException{

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

Date dt1 = sdf.parse(endTime);

if (dt1.getTime()/(1000*3600*24)+1 > dt2.getTime()/(1000*3600*24)) {

return 1;

}else if(dt1.getTime()/(1000*3600*24)+1 < dt2.getTime()/(1000*3600*24)) {//相等

return -1;

}else {

return 0;

}

}

/**

* 日期不符合进行日期的操作(按天计算)

* @throws ParseException

* */

public String CompareCanler(String date) throws ParseException {

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

//转换为日期格式

Date data = sdf.parse(date);

//创建日期

Calendar cade=Calendar.getInstance();

cade.setTime(data);

//天数减一

cade.add(Calendar.DAY_OF_MONTH, -1);

String dates=sdf.format(cade.getTime());

returndates;

}

-----------------------------------------------------

//处理所选中月份的信息,第一天到最后一天

/**处理日期格式按月查询格式

* @throws ParseException */

public int CompareMonth(String date) throws ParseException {

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");

//转换传入的月份

Date dt1 = sdf.parse(date);

//建立当前日期

Calendar car=Calendar.getInstance();

//设置为月份的第一天

car.set(Calendar.DAY_OF_MONTH, 1);

String Month=sdf.format(car.getTime());

Date month=sdf.parse(Month);

if(dt1.getTime()/(1000*3600*24)+1< month.getTime()/(1000*3600*24)) {

return 1;

}else if(dt1.getTime()/(1000*3600*24)+1>month.getTime()/(1000*3600*24)){

return -1;

}

return 0;

}

/**处理按月查询,当月的日期范围

* @throws ParseException */

public String handlingMonth(String date) throws ParseException {

if(CompareMonth(date) == 1) {

System.out.println("日期小");

//日期非本月,查询所选的月份

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");

//转换传入的日期的格式

Date month=sdf.parse(date);

//获取起始的日期

Calendar createTime=Calendar.getInstance();

createTime.setTime(month);

createTime.set(Calendar.DAY_OF_MONTH, 1);

//获取结束日期

Calendar endTime=Calendar.getInstance();

endTime.setTime(month);

// 获取本月的最后一天

int days = endTime.getActualMaximum(Calendar.DAY_OF_MONTH);

endTime.set(Calendar.DAY_OF_MONTH, days);

SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");

String start=sdf2.format(createTime.getTime());

String end=sdf2.format(endTime.getTime());

return start+","+end;

}

if(CompareMonth(date)==-1) {

System.out.println("日期大");

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

Date month=new Date();

Calendar createTime=Calendar.getInstance();

createTime.setTime(month);

createTime.set(Calendar.DAY_OF_MONTH, 1);

Calendar endTime=Calendar.getInstance();

endTime.setTime(month);

endTime.add(Calendar.DAY_OF_MONTH, -1);

Stringstart = sdf.format(createTime.getTime());

String end=sdf.format(endTime.getTime());

return start+","+end;

}

return null;

}

-------------------------------------------------------------------------

//处理年份的信息,最多2年,当前年向前推2年

/**换算年份的日期*/

public String CompareYear(String date) throws ParseException {

//获取当年的年份

Calendar car=Calendar.getInstance();

String date1=car.get(Calendar.YEAR)+"";

SimpleDateFormat sdf=new SimpleDateFormat("yyyy");

//把当年的年份进行转换

Date time1=sdf.parse(date1);

//把传入的年份进行转换

Date time3=sdf.parse(date);

//把传染的年份转换成日期格式

String date2=sdf.format(time3);

//把传入的年份转换成时间格式

Date time2=sdf.parse(date2);

if(time1.getTime()/(1000*3600*24) > time2.getTime()/(1000*3600*24)) {

//年份不等于当前的年份,也就是前2年,跟前一年

System.out.println("年份小于当年年份");

CalendarcheckDate=Calendar.getInstance();

//获取当年的年份

checkDate.set(Calendar.YEAR, checkDate.get(Calendar.YEAR)-2);

SimpleDateFormat sdfs=new SimpleDateFormat("yyyy");

String Years=sdfs.format(checkDate.getTime());

if(date.equals(Years)) {

System.out.println("进入2015");

//设置月底

Calendar car2=Calendar.getInstance();

car2.set(Calendar.YEAR,car2.get(Calendar.YEAR));

car2.set(Calendar.MONTH,car.get(Calendar.MONTH));

car2.set(Calendar.DAY_OF_MONTH,car.get(Calendar.DAY_OF_MONTH));

//转换开始的日期

SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");

String startTime=sdf1.format(car2.getTime());

//设置月底的日期

Calendar car3=Calendar.getInstance();

car3.set(Calendar.YEAR,car3.get(Calendar.YEAR)-2);

car3.set(Calendar.MONTH,11);

car3.set(Calendar.DAY_OF_MONTH,31);

String endTime=sdf1.format(car3.getTime());

return startTime+","+endTime;

}else {

//设置日期为月初

Calendar car1=Calendar.getInstance();

car1.set(Calendar.YEAR,(car.get(Calendar.YEAR))-1);

car1.set(Calendar.MONTH,0);

car1.set(Calendar.DAY_OF_MONTH,1);

SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");

String startTime=sdf1.format(car1.getTime());

//设置月底

Calendar car2=Calendar.getInstance();

car2.set(Calendar.YEAR,(car2.get(Calendar.YEAR))-1);

car2.set(Calendar.MONTH,11);

car2.set(Calendar.DAY_OF_MONTH,31);

SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");

String endTime=sdf2.format(car2.getTime());

return startTime+","+endTime;

}

}else if(time1.getTime()/(1000*3600*24) == time2.getTime()/(1000*3600*24)) {

System.out.println("年份等于当前年份");

//设置日期为月初

Calendar car1=Calendar.getInstance();

//设置年为今年

car1.set(Calendar.YEAR,car.get(Calendar.YEAR));

//设置月份为1月

car1.set(Calendar.MONTH,0);

//设置天数为第一天

car1.set(Calendar.DAY_OF_MONTH,1);

SimpleDateFormat sdf1=new SimpleDateFormat("yyyy-MM-dd");

String startTime=sdf1.format(car1.getTime());

//设置月底

Calendar car2=Calendar.getInstance();

car2.set(Calendar.YEAR,car.get(Calendar.YEAR));

car2.set(Calendar.MONTH,car.get(Calendar.MONTH));

car2.set(Calendar.DAY_OF_MONTH,car.get(Calendar.DAY_OF_MONTH)-1);

SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");

String endTime=sdf2.format(car2.getTime());

return startTime+","+endTime;

}else {

System.out.println("年份大于当前年份,不合理");

}

return null;

}

相关推荐