博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Calendar 对象的使用实例
阅读量:5792 次
发布时间:2019-06-18

本文共 5435 字,大约阅读时间需要 18 分钟。

1.Calendar demo例子

Calendar 类时间操作,示范代码。

public class CalendarDemo {       private static SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");       public static void main(String[] args) {                      //获取calendar实例;           Calendar calendar = Calendar.getInstance();                      //判断calendar是不是GregorianCalendar类的实例;           if(calendar instanceof GregorianCalendar){               System.out.println("属于GregorianCalendar类的实例!");           }                      //从calendar对象中获得date对象,当前时间;           Date dates =  calendar.getTime();                      //格式化时间;           String date_str= date_format.format(dates);           System.out.println(date_str);                      //设置月份05;代表日历的月份6月,因为月份从0开始。           calendar.set(Calendar.MONTH, 05);                      int months = calendar.get(Calendar.MONTH);           System.out.println(months);  //输出05;                      //设置日期为2011-07-24 09:59:50           calendar.set(2011, 06, 24, 9, 59, 50);             String getDate = date_format.format(calendar.getTime());           System.out.println(getDate);   //输出2011-07-24 09:59:50;                      //比较日前大小;           if(new Date().getTime() > calendar.getTimeInMillis()){               System.out.println("当前日期在后!");           }else{               System.out.println("当前日期在前!");           }                      //设置当前时间为:2011-07-24 11:06:00           calendar.setTime(new Date());             int year   = calendar.get(Calendar.YEAR);   //获取年;           int month  = calendar.get(Calendar.MONTH);  //获取月;           int date   = calendar.get(Calendar.DATE);   //获取天;           int hour   = calendar.get(Calendar.HOUR);   //获取小时;           int minute = calendar.get(Calendar.MINUTE); //获取分钟;           int second = calendar.get(Calendar.SECOND); //获取秒钟;           int hour_of_day = calendar.get(Calendar.HOUR_OF_DAY);    //第几个小时,           int day_of_month  = calendar.get(Calendar.DAY_OF_MONTH); //这天,在一个月内是第几天.            int day_of_week = calendar.get(Calendar.DAY_OF_WEEK);    //这天,在一周内,是第几天.           int day_of_year = calendar.get(Calendar.DAY_OF_YEAR);    //这天,在一年内,是第几天。           int week_of_year = calendar.get(Calendar.WEEK_OF_YEAR);  //这周,在一年内是第几周;           int week_of_month = calendar.get(Calendar.WEEK_OF_MONTH);//这周,在这个月是第几周;以以星为标准;           int zone_offset = calendar.get(Calendar.ZONE_OFFSET);    //获取时区;           int day_of_week_in_month = calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);  //某月中第几周,按这个月1号算,1号起就是第1周,8号起就是第2周。以月份天数为标准            int r = calendar.get(Calendar.AM_PM);           if(r==calendar.AM){               System.out.println("现在是上午");           }                      if(r==calendar.PM){               System.out.println("现在是下午");           }           System.out.println("==================================================");           System.out.println(year);           System.out.println(month);           System.out.println(date);           System.out.println(hour);           System.out.println(minute);           System.out.println(second);           System.out.println(hour_of_day);           System.out.println(day_of_month);           System.out.println(day_of_week);           System.out.println(day_of_year);           System.out.println(week_of_year);           System.out.println(week_of_month);           System.out.println(zone_offset);           System.out.println(day_of_week_in_month);       }   }

 

 

2.项目中应用

@RequestMapping(method = RequestMethod.GET, produces = { "application/json" })    @ResponseBody    public ListWithTotalCount
aucLotQuery(@ModelAttribute("selectedAgency") SysAgencyDto selectedAgency, int page, int rows, String order, String sort) { Pageable pageable; String agencyId = selectedAgency.getId().toString(); if (sort != null && !sort.isEmpty()) { pageable = new PageRequest(page - 1, rows, Direction.fromStringOrNull(order), sort); } else { pageable = new PageRequest(page - 1, rows); } Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); Date less = cal.getTime(); cal.set(Calendar.HOUR_OF_DAY, 23); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MILLISECOND, 999); Date great = cal.getTime(); Specification
spec = (root, query, cb) -> { List
predicates = new ArrayList
(); if (agencyId != null && !agencyId.isEmpty() && !"0".equals(agencyId)) { Predicate predicate = cb.equal(root.get(Auction_.agencyId), agencyId); predicates.add(predicate); } Predicate published = cb.equal(root.get(Auction_.isPublished), Auction.AUCLOT_ISPUBLISHED_FINISH); predicates.add(published);        //获取当天的检索条件 Predicate time =cb.between(root.get(Auction_.startTime),less,great); predicates.add(time); return cb.and(predicates.toArray(new Predicate[0])); }; Page
pageresult = auctionRepository.findAll(spec, pageable); List
dtoList = (new AuctionsDTOAssembler()).toDTOList(pageresult.getContent()); return new ListWithTotalCount
(dtoList, (int) pageresult.getTotalElements()); }

 

转载地址:http://gfzfx.baihongyu.com/

你可能感兴趣的文章
DEV-C++ 调试方法简明图文教程(转)
查看>>
VS2017+EF+Mysql生成实体数据模型(解决闪退的坑)
查看>>
C++多态、继承的简单分析
查看>>
库克称未来苹果用户可自己决定是否降频 网友:你是在搞笑吗?
查看>>
6倍性能差100TB容量,阿里云POLARDB咋实现?
查看>>
Sublime Text 2 技巧
查看>>
使用fscanf()函数从磁盘文件读取格式化数据
查看>>
参加婚礼
查看>>
刚毕业从事java开发需要掌握的技术
查看>>
CSS Custom Properties 自定义属性
查看>>
vim
查看>>
MVVM计算器(下)
查看>>
C++中指针和引用的区别
查看>>
簡單分稀 iptables 記錄 udp 微軟 138 端口
查看>>
Java重写equals方法和hashCode方法
查看>>
Spark API编程动手实战-07-join操作深入实战
查看>>
H3C-路由策略
查看>>
centos 修改字符界面分辨率
查看>>
LNMP之Mysql主从复制(四)
查看>>
阅读Spring源代码(1)
查看>>