java date实现加一天代码,其他天数的一次类推
2016-07-06 15:08
573 查看
import java.text.Format; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class $ { public static void main(String[] args) { Format f = new SimpleDateFormat("yyyy-MM-dd"); Date today = new Date(); System.out.println("今天是:" + f.format(today)); Calendar c = Calendar.getInstance(); c.setTime(today); c.add(Calendar.DAY_OF_MONTH, 1);// 今天+1天 Date tomorrow = c.getTime(); System.out.println("明天是:" + f.format(tomorrow)); } } 结果: 今天是:2013-10-09 明天是:2013-10-10
因项目需要,这里附上我在Hibernate中实现取一周数据的Dao代码:
public List exportOneweekinfo() throws HibernateException{ Session session = HibernateSessionFactory.getSession(); Transaction tx = session.beginTransaction(); Format ctime = new SimpleDateFormat("yyyy-MM-dd"); Date today = new Date(); String todaydate = ctime.format(today); //当前时间日期 Calendar c = Calendar.getInstance(); c.setTime(today); c.add(Calendar.DAY_OF_MONTH, -7); Date before = c.getTime(); String beforeweekdate = ctime.format(before); //一周开始的时间 System.out.println("todaydate"+todaydate); System.out.println("beforweekdate"+beforeweekdate); Query query = session.createQuery("select e from Information as e where e.reporttime>='"+beforeweekdate+"' and e.reporttime <= '"+ todaydate +"'"); //hql语句 // query.setMaxResults(20); //设置最大显示的个数 List list = query.list(); tx.commit(); HibernateSessionFactory.closeSession(); return list; }
相关文章推荐
- Maven实践(二):Spring与classpath*加载配置文件
- java 解析josn数组
- 多线程设计模式——Guarded Suspension(保护性暂挂模式)
- 关于springmvc的拦截器
- java链表之--单向链表的增删改查
- java-socket编程_ 1.socket入门
- 利用maven/eclipse搭建ssm(spring+spring mvc+mybatis)
- 【Spring】Spring源码分析--整体架构
- java集合概述
- jdk环境变量的配置,Path路径的查看
- Hibernate整合Spring后,如何使用SchemaExport生成数据库表
- Java对象的创建
- Math对象(javasript)
- 深入分析 Java I/O 的工作机制
- Java 理论与实践: 正确使用 Volatile 变量
- java注解
- Java 高并发缓存与Guava Cache
- maven基础知识——包管理
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- java删除递归文件夹及文件夹下文件