在java中经常使用到时间,在页面上展示与存在数据库的格式显示的是不一样的,所以就在后台需要进行转换 2、怎么设置自己的时间,对于date这个类已经不能进行设置,需要使用calendar进行设置时间
代码:
DateFormat format2=new SimpleDateFormat("yyyy-MM-dd");
if(StringUtils.isEmpty(searchForm.getTimeStart())){
Date date2=new Date();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
String timeNow=format.format(date2);
searchForm.setTimeStart(timeNow);
}
/**
* 获取时间,此时间的格式为“yyyy-MM-dd”,需要把时间设置成当天的23.59.59分!
* 设置时间需要用到calendar,date设置已经不支持!
*/
String timeStart=searchForm.getTimeStart().toString();
Date date=null;
try {
date = format2.parse(searchForm.getTimeStart());
} catch (ParseException e) {
e.printStackTrace();
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
TimeZone tz1 = TimeZone.getTimeZone("GMT+8:00");
Calendar now = Calendar.getInstance(tz1);
now.set(Calendar.SECOND, 59);
now.set(Calendar.HOUR_OF_DAY, 23);
now.set(Calendar.MINUTE,59);
now.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
now.set(Calendar.MONTH,calendar.get(Calendar.MONTH));
now.set(Calendar.DAY_OF_MONTH,calendar.get(Calendar.DAY_OF_MONTH));
Date dateEnd=now.getTime();
DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timeEnd=format.format(dateEnd).toString();
//判断输入的数据是否为空,进行查询
String diaoyuType=null;
if(searchForm.getDiaoYuType()==" "||searchForm.getDiaoYuType()==""||searchForm.getDiaoYuType()==null){
diaoyuType=null;
}else{
diaoyuType=searchForm.getDiaoYuType();
}
String city=null;
if(searchForm.getCity()==null||searchForm.getCity()==""||searchForm.getCity().equals("浙江")){
city=null;
}else{
city=searchForm.getCity();
}
List<CityCount> list= webVisitorInfoMapper.queryCount(timeStart,timeEnd,diaoyuType,city);
return list;
引入的一个例子说明: http://blog.csdn.net/afterlife_qiye/article/details/38226553