Calendar 日期判断 等于 。小于。大于
public static void main(String[] args) throws Exception {
String startTime = "2012-12-12 12:45:45";
String endTime = "2012-04-12 12:45:40";
String SYSendTime = "2012-11-12 12:45:40";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(startDate);
end.setTime(endDate);
if(start.before(end)){
System.err.println("开始时间小于结束时间");
}else if(start.after(end)){
System.err.println("开始时间大于结束时间");
}else if(start.equals(end)){
System.err.println("开始时间等于结束时间");
}
}
//常用方法
public static void method(){
Date date = new Date();
Calendar c = Calendar.getInstance();
//setTime():使用给定的Date设置此 Calendar 的时间
c.setTime(date);
//获取Calendar对象
Calendar cm = Calendar.getInstance();
//getTime():获取当前时间,类似于new Date();
Date d = cm.getTime();
System.err.println("Calendar获得时间:" + d);
System.err.println("new Date创建的时间:" + date);
//getTimeInMillis():返回此 Calendar 的时间值,以毫秒为单位。
long dl = c.getTimeInMillis();
long ddate = cm.getTimeInMillis();
System.err.println("毫秒数:" + dl);
System.err.println("毫秒数:" + ddate);
//setTimeInMillis():用给定的 long 值设置此Calendar的当前时间值。
long sv = 123456;
Calendar sc = Calendar.getInstance();
sc.setTimeInMillis(sv);
SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String st = ss.format(sc.getTime());
System.err.println(st);
//get():返回给定日历字段的值。
int year = c.get(Calendar.YEAR);
System.err.println(year);
//set():将给定的日历字段设置为给定值
c.set(Calendar.YEAR, 2);
int y = c.get(Calendar.YEAR);
System.err.println(y);//输出2
//Calendar比较:before(),after(),equals(),compareTo().
try{
String startTime = "2012-12-12 12:45:39";
String endTime = "2012-12-12 12:45:40";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = sdf.parse(startTime);
Date endDate = sdf.parse(endTime);
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
start.setTime(startDate);
end.setTime(endDate);
if(start.before(end)){
System.err.println("开始时间小于结束时间");
}else if(start.after(end)){
System.err.println("开始时间大于结束时间");
}else if(start.equals(end)){
System.err.println("开始时间等于结束时间");
}
/*
* start < end 返回-1
* start = end 返回0
* start > end 返回1
*/
int count = start.compareTo(end);
System.err.println(count);
//add():为给定的日历字段添加或减去指定的时间量
start.add(Calendar.YEAR, -3);
System.err.println("原来的时间:" + startTime);
System.err.println("add后的时间:" + sdf.format(start.getTime()));
//toString():转换为字符串
System.err.println(start.toString());
}catch(Exception e){}
}
//常用属性
public static void param(){
Date date = new Date();
Calendar c = Calendar.getInstance();
c.setTime(date);
//Calendar.YEAR:日期中的年
int year = c.get(Calendar.YEAR);
//Calendar.MONTH:日期中的月,需要加1
int mounth = c.get(Calendar.MONTH) + 1;
//Calendar.DATE:日期中的日
int day = c.get(Calendar.DATE);
//Calendar.HOUR:日期中的小时(12小时制)
int hour = c.get(Calendar.HOUR);
//Calendar.HOUR_OF_DAY:24小时制
int HOUR_OF_DAY = c.get(Calendar.HOUR_OF_DAY);
//Calendar.MINUTE:日期中的分钟
int minute = c.get(Calendar.MINUTE);
//Calendar.SECOND:日期中的秒
int second = c.get(Calendar.SECOND);
System.err.println(year + "-" + mounth + "-" + day + " " + hour + ":" + minute + ":" + second);
//Calendar.WEEK_OF_YEAR:当前年中星期数
int WEEK_OF_YEAR = c.get(Calendar.WEEK_OF_YEAR);
//Calendar.WEEK_OF_MONTH:当前月中星期数
int WEEK_OF_MONTH = c.get(Calendar.WEEK_OF_MONTH);
//Calendar.DAY_OF_YEAR:当前年中的第几天
int DAY_OF_YEAR = c.get(Calendar.DAY_OF_YEAR);
//Calendar.DAY_OF_MONTH:当前月中的第几天
int DAY_OF_MONTH = c.get(Calendar.DAY_OF_MONTH);
//Calendar.DAY_OF_WEEK:当前星期的第几天(星期天表示第一天,星期六表示第七天)
int DAY_OF_WEEK = c.get(Calendar.DAY_OF_WEEK);
//Calendar.DAY_OF_WEEK_IN_MONTH:当前月中的第几个星期
int DAY_OF_WEEK_IN_MONTH = c.get(Calendar.DAY_OF_WEEK_IN_MONTH);
try{
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date ampm = format.parse("2012-12-15 21:59:59");
Calendar cc = Calendar.getInstance();
cc.setTime(ampm);
//AM_PM:HOUR 是在中午之前还是在中午之后,在中午12点之前返回0,在中午12点(包括12点)之后返回1
int AM_PM = cc.get(Calendar.AM_PM);
}catch(Exception e){}
}
}
Calendar 日期判断 等于 。小于。大于的更多相关文章
- 简单的股票信息查询系统 1 程序启动后,给用户提供查询接口,允许用户重复查股票行情信息(用到循环) 2 允许用户通过模糊查询股票名,比如输入“啤酒”, 就把所有股票名称中包含“啤酒”的信息打印出来 3 允许按股票价格、涨跌幅、换手率这几列来筛选信息, 比如输入“价格>50”则把价格大于50的股票都打印,输入“市盈率<50“,则把市盈率小于50的股票都打印,不用判断等于。
'''需求:1 程序启动后,给用户提供查询接口,允许用户重复查股票行情信息(用到循环)2 允许用户通过模糊查询股票名,比如输入“啤酒”, 就把所有股票名称中包含“啤酒”的信息打印出来3 允许按股票价格 ...
- java成神之——date和calendar日期的用法
date和calendar日期的用法 util的data转换成sql的data 创建Date对象 格式化 Instant ChronoUnit LocalTime LocalDate LocalDat ...
- 分享一个根据具体的日期判断星座的PHP函数
其实原理很简单,也就是把所有的星座月份日期范围存储到一个数组中,然后根据日期判断属于哪个范围,这样就得到是哪个星座了. 下面的这个函数写的比较精炼,可以参考一下 function constellat ...
- 【C#】wpf自定义calendar日期选择控件的样式
原文:[C#]wpf自定义calendar日期选择控件的样式 首先上图看下样式 原理 总览 ItemsControl内容的生成 实现 界面的实现 后台ViewModel的实现 首先上图,看下样式 原理 ...
- 8.算法竞赛中的常用JAVA API :Calendar日期类
8.算法竞赛中的常用JAVA API :Calendar日期类 摘要 在蓝桥杯中有关于日期计算的问题,正好java中的Date类和Calendar类提供了对日期处理的一些方法.Date类大部分方法已经 ...
- mysql 中不等于过滤 null 的问题(同时比较等于,大于和小于)
在写 SQL 条件语句是经常用到 不等于'!='的筛选条件,此时要注意此条件会将字段为 null 的数据也当做满足不等于的条件而将数据筛选掉. 1.原始数据和表结构 CREATE TABLE `tes ...
- Calendar - SGU 115(日期判断)
这年的开始的第一天是星期 1 代码如下: =============================================================================== ...
- tp5 关键字模糊查询 日期查询 小于大于某范围等查询的优点
挺不错,用熟了这tp5封装的很方便. 类似上边一个查询多个操作,基本在model 一个方法搞定代码也不用很多, 首先要学会用scope 网上搜tp scope 有几个例子可以借鉴 model 内添加 ...
- 【转载】java8中的Calendar日期对象(LocalDateTime)
Java 8 推出了全新的日期时间API,Java 8 下的 java.time包下的所有类都是不可变类型而且线程安全. 下面是新版API中java.time包里的一些关键类: Instant:瞬时实 ...
随机推荐
- Java通过jni调用动态链接库
(1)JNI简介 JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++).从Java1.1开始,JNI标准成为java ...
- C语言-表达式和运算符
表达式:表达式是c语言的主体,在c语言中,表达式由操作符和操作数组成.简单的表达式可以只有一个操作数.根据操作符的个数,可以将表达式分为简单表达式和复杂表达式,简单的表达式只含有一个操作符(如:5+5 ...
- 如何解决Visual Studio 首次调试 docker 的 vs2017u5 exists, deleting Opening stream failed, trying again with proxy settings
前言 因为之前我电脑安装的是windows10家庭版,然而windows10家庭没有Hyper-v功能. 搜索了几篇windows10家庭版安装docker相关的博客,了解一些前辈们走过的坑. 很多人 ...
- #2使用html+css+js制作网站教程 测试
#2使用html+css+js制作网站教程 测试 本系列链接 1 测试 1.1 运行 1.2 审查 1.3 审查技巧 1.4 其他 引言: 编写完代码后就要上机测试代码,获得用户体验,筛选bug 笔者 ...
- OBKoro1的2020年年终总结
前言 一晃眼2020年马上就要过去了,今年感觉过的特别快. 工作已经三年了,之前都没有写过年终总结,结果造成了下面这个现象: 回首过去的几年,记忆已经很模糊了,需要很用力才能想起过去一部分往事. 人生 ...
- 010_MySQL
目录 初识MySQL 为什么学习数据库 什么是数据库 数据库分类 MySQL简介 Windows安装MySQL 安装建议 软件下载 安装步骤 安装SQLyog 下载安装 连接数据库 简单操作 命令行连 ...
- SQLI-LABS复现通关
sql-lab 复现通关(深入学习) less-1 基于错误的单引号字符串 - 正常访问 127.0.0.1/?id=1 - 添加 ' 返回报错信息:You have an error in your ...
- MySQL select 语句指定字段查询
指定字段查询 SELECT 语法 SELECT [ALL | DISTINCT] {* | table.* | [table.field1[as alias1][,table.field2[as al ...
- 为什么会有 AtomicReference ?
我把自己以往的文章汇总成为了 Github ,欢迎各位大佬 star https://github.com/crisxuan/bestJavaer 我们之前了解过了 AtomicInteger.Ato ...
- 你这样用过DO循环吗?
DATA: BEGIN OF text, word1(4) TYPE c VALUE 'This', word2(4) TYPE c VALUE 'is', ...