java Date与string,Timestamp的转换

在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伏特加的滋味

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值