Calendar 对象的使用实例
1.Calendar demo例子
Java 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<AuctionsDTO> 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<Auction> spec = (root, query, cb) -> {
List<Predicate> predicates = new ArrayList<Predicate>(); 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<Auction> pageresult = auctionRepository.findAll(spec, pageable);
List<AuctionsDTO> dtoList = (new AuctionsDTOAssembler()).toDTOList(pageresult.getContent()); return new ListWithTotalCount<AuctionsDTO>(dtoList, (int) pageresult.getTotalElements());
}
Calendar 对象的使用实例的更多相关文章
- new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。
new运算符 - JavaScript | MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operator ...
- js的dom对象(带实例超详细全解)
js的dom对象(带实例超详细全解) 一.总结 一句话总结: 1.DOM中的方法区分大小写么? 解答:区分 2.DOM中元素和节点的关系式什么? 解答:元素就是标签,节点中有元素节点,也是标签,节点中 ...
- 【Python】[面性对象编程] 获取对象信息,实例属性和类属性
获取对象信息1.使用isinstance()判断class类型2.dir() 返回一个对象的所有属性和方法3.如果试图获取不存在的对象会抛出异常[AttributeError]4.正确利用对象内置函数 ...
- [转]Java中的对象和对象引用实例浅析
在Java中,有一组名词经常一起出现,它们就是“对象和对象引用”,很多朋友在初学Java的时候可能经常会混淆这2个概念,觉得它们是一回事,事实上则不然.今天我们就来一起了解一下对象和对象引用之间的区别 ...
- Python面向对象 -- 继承和多态、获取对象信息、实例属性和类属性
继承和多态 继承的好处: 1,子类可以使用父类的全部功能 2,多态:当子类和父类都存在相同的方法时,子类的方法会覆盖父类的方法,即调用时会调用子类的方法.这就是继承的另一个好处:多态. 多态: 调用方 ...
- 构造函数、原型对象prototype、实例、隐式原型__proto__的理解
(欢迎一起探讨,如果有什么地方写的不准确或是不正确也欢迎大家指出来~) PS: 内容中的__proto__可能会被markdown语法导致显示为proto. 建议将构造函数中的方法都定义到构造函数的原 ...
- 二.数据库游标对象cursor与实例
1.数据库游标对象cursor 2.select实例 代码展示: import pymysql conn=pymysql.connect( host='192.168.199.249', port=3 ...
- Java中JSON字符串与java对象的互换实例详解
这篇文章主要介绍了在java中,JSON字符串与java对象的相互转换实例详解,非常不错,具有参考借鉴价值,需要的朋友可以参考下 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JS ...
- Java中JSON字符串与java对象的互换实例详解(转)
http://www.jb51.net/article/90914.htm 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.JSON等,JSON作为一个轻量级的数据格式比xml效率要 ...
随机推荐
- day6 ConfigParser模块 yaml模块
yaml模块: python可以处理yaml文件,yaml文件安装的方法为:$ pip3 install pyyaml configparser模块,用来处理文件的模块,可以实现文件的增 ...
- linux的IPC进程通信方式-匿名管道(一)
linux的IPC进程通信-匿名管道 什么是管道 如果你使用过Linux的命令,那么对于管道这个名词你一定不会感觉到陌生,因为我们通常通过符号"|"来使用管道,但是管道的真正定义是 ...
- ASP.NET MVC5+ 路由特性
概述 ASP.NET MVC 5支持一种新的路由协议,称为路由特性. MVC5也支持以前定义路由的方式,你可以在一个项目中混合使用这两种方式来定义路由. 案例 1.使用Visual Studio 20 ...
- USACO 6.3 Cowcycles
CowcyclesOriginally by Don Gillies [International readers should note that some words are puns on co ...
- 基于 Struts2 的文件下载
介于上篇我们讲述了基于 Struts2 的单文件和多文件上传,这篇我们来聊一聊基于 Struts2 的文件下载. 1.导 jar 包 commons-io-2.0.1.jar struts2-core ...
- bzoj 1228 [SDOI2009]E&D
sg表很好打,规律很不好找.... #include<bits/stdc++.h> #define LL long long #define fi first #define se sec ...
- phpstorm 输入法中文不同步 phpstorm 输入法不跟随光标解决办法
win7系统新安装的phpstorm2017.2版本,试了很多输入法,要么是不显示候选次,要么是输入法候选词总是在屏幕右下角,没有跟随光标移动.百度很久,重要找到解决方案. 就是替换phpstorm安 ...
- autoit v3安装
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
- UVA - 10815 - Andy's First Dictionary STL
Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him ...