经常处理一些日期相关的信息,Calendar类是处理日期的常用类,写下几个方法,不用重复造轮子了。

1.求上一天,下一天的日期

Date now = new Date();
Calendar c = Calendar.getInstance();
c.setTime(now);
c.add(Calendar.DAY_OF_MONTH, -1);  // 下一天,上一天-1改为1
Date yesterday = c.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
System.out.println(sdf.format(yesterday));

2.给定开始时间和结束时间,输出中间每一天

Date now = new Date();
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String begin = "20161201";
String end = "20170210";
try {
Date begin_date = sdf.parse(begin);
Date end_date = sdf.parse(end);

List<Date> lDate = new ArrayList<Date>();
lDate.add(begin_date);
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(begin_date);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(end_date);
// 测试此日期是否在指定日期之后
while (end_date.after(calBegin.getTime())) {
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
lDate.add(calBegin.getTime());
}

for (Date date : lDate) {
String format = sdf.format(date);
System.out.println(format);
}

3.compareTo()方法,比较两个Calendar的日期谁在前谁在后,在之前的话为-1,相同为0,在之后为1

4.Calendar类的before和after方法,参数都为Calendar才能正确比较

Calendar类常用需求方法的更多相关文章

  1. Delphi中TStringList类常用属性方法详解

    TStrings是一个抽象类,在实际开发中,是除了基本类型外,应用得最多的. 常规的用法大家都知道,现在来讨论它的一些高级的用法. 先把要讨论的几个属性列出来: 1.CommaText 2.Delim ...

  2. File类常用的方法与字节流类方法简介

    File类常用的方法 获取功能的方法 public String getAbsolutePath() :返回此File的绝对路径名字符串. public String getPath() :将此Fil ...

  3. WebDriverWait类以及类常用的方法

    WebDriverWait类提供了显式等待和隐式等待,显式等待的等待时间是固定的,固定了10s就必须等待10s,隐式等待的等待时间是个范围,例如最大10s,那么如果在3s的时候程序达到预期的结果,那么 ...

  4. Collections集合工具类常用的方法

    java.utils.Collections //是集合工具类,用来对集合进行操作.部分方法如下: public static <T> boolean addAll(Collection& ...

  5. QueryRunner类常用的方法

    public Object query(Connection conn, String sql, Object[] params, ResultSetHandler rsh) throws SQLEx ...

  6. Spring JdbcTemplate类常用的方法

    execute(String  sql) 可执行任何sql语句,但返回值是void,所以一般用于数据库的新建.修改.删除和数据表记录的增删改. int  update(String sql) int  ...

  7. WinForm BaseClass类常用通用方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  8. 集合框架二(Collection接口实现类常用遍历方法)

    四种常用遍历方式 Collection coll = new ArrayList(); coll.add("123"); coll.add("456"); co ...

  9. StringUtils类常用的方法讲解

    StringUtils 方法的操作对象是 Java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 ...

随机推荐

  1. git比较两个分支的文件的差异

    Git diff branch1 branch2 --stat   //显示出所有有差异的文件列表 Git diff branch1 branch2 文件名(带路径)   //显示指定文件的详细差异 ...

  2. 02-模拟Junit4功能

    package com.day2; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; im ...

  3. spark java.lang.OutOfMemoryError: unable to create new native thread

    最近迁移集群,在hadoop-2.8.4 的yarn上跑 spark 程序 报了以下错误 java.lang.OutOfMemoryError: unable to create new native ...

  4. Bash常用快捷键及其作用

    在 Bash 中有非常多的快捷键,如果可以熟练地使用这些快捷键,可有效地提高我们的工作效率.只是快捷键相对较多,不太好记忆,这就要多加练习和使用.这些快捷键如表 1 所示. 表 1 Bash 常用快捷 ...

  5. leetcode114

    class Solution { public: void flatten(TreeNode* root) { while(root){ if(root->left){ TreeNode* pr ...

  6. 数据库设计,表与表的关系,一对多。One-To-Many(2)

    一对多:主键数据表中只能包含一个记录,而在其关系记录表中这条记录可以与一个或多个记录相关,也可以没有记录与之相关. 关联映射:一对多/多对一存在最普遍的映射关系,简单来讲就如球员与球队的关系:一对多: ...

  7. .do的消除

    其实就是在web.xml中去掉.do即可  那里有拦截器作用,什么样的文件可以进入前端控制器1

  8. 退出vim编辑器(转)

    在linux家族中,vim编辑器是系统自带的文本编辑器,其功能强大自不必说了. 偶有小白,刚接触linux,要修改某个文本文件,不可能像WINDOWS那样操作,更有甚者,进入VI编辑器后,无法退出以致 ...

  9. ubuntu系统中安装eclipse

    具体可以看这篇博文 .https://www.cnblogs.com/sanduo1314/articles/5137090.html 然后再/usr/share/applications中找到ecl ...

  10. vi怎么统计查找字符串的个数

    vi怎么统计查找字符串的个数 用vi打开一个比较大的文本,用vi查找指定字符串,现在怎么统计该字符串的个数呢?比如我查找ORA字符串,直接输入 /ORA的时候vi会高亮显示.现在怎么统计ORA的个数呢 ...