java.util.Calendar 类是一个抽象类 ,Calendar 类进行时间和日期的处理

首先获取类实例

Calendar calendar = Calendar.getInstance();//获取当前系统时间

1.设置时间

calendar.set(2013, 5, 4, 13, 44, 51);//年月日时分秒(月份0代表1月)
calendar.set(Calendar.YEAR, 2014);//设置为2014年
calendar.set(Calendar.MONTH, 7);//设置为8月(0代表1月,1代表2月,2代表3月...12代表1月)
calendar.set(Calendar.DATE, 11);//设置为11日
calendar.set(Calendar.HOUR_OF_DAY, 15);//设置为15时
calendar.set(Calendar.MINUTE, 33);//设置为33分
calendar.set(Calendar.SECOND, 32);//设置为32秒

2.获取年月日时分秒

calendar.get(Calendar.YEAR);//年
calendar.get(Calendar.MONTH) + 1;//前一个月+1(必须要+1,不然表示获取前一个月份)
calendar.get(Calendar.DATE);//日
calendar.get(Calendar.HOUR_OF_DAY);//时
calendar.get(Calendar.MINUTE);//分
calendar.get(Calendar.SECOND);//秒
calendar.get(Calendar.DAY_OF_WEEK);//星期(Locale.ENGLISH情况下,周日是1,剩下自己推算)

3.通过运算获取时间

calendar.add(Calendar.YEAR, 1);//年
calendar.add(Calendar.MONTH, 1);//月
calendar.add(Calendar.DATE, 1);//后一天的时间,-1代表前一天的时间
calendar.add(Calendar.HOUR_OF_DAY, -1);//时
calendar.add(Calendar.MINUTE, 1);//分
calendar.add(Calendar.SECOND, 1);//秒
calendar.add(Calendar.DATE, 7);//周

4.和Date类转换

Date转Calendar

//1.首先获取类实例
Calendar calendar = Calendar.getInstance();
Date date = new Date();//当前时间
calendar.setTime(date);//设置为当期时间

Calendar转Date

Calendar calendar = Calendar.getInstance();
Date date = calendar.getTime();

5.格式化时间

Date转String

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//自定义格式:"yyyy年MM月dd日 HH:mm:ss" ,"yyyy-MM-dd HH:mm:ss" 等...
Date time = calendar.getTime();
String dateStr = simpleDateFormat.format(time);

String转Date

String dateStr = "2020-07-21 18:10:12";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = simpleDateFormat.parse(dateStr);

6.新功能LocalDate:当前日期格式化

/**
* @description: LocalDate日期处理
* @author: HaoWu
* @create: 2020年09月04日
*/
public class TestPublisher {
public static void main(String[] args) {
/***************** LocalDate ********************/
LocalDate now = LocalDate.now();
//当前日期 2020-09-05
String nowStr = now.toString();
//前一天 2020-09-04
String yesterday1 = now.plusDays(-1).toString();
String yesterday2 = now.minusDays(1).toString(); /***************** LocalTime ********************/
LocalTime nowTime = LocalTime.now();
//hh:MM:ss 时分秒
String nowStr1 = nowTime.toString();
//时
int hour = nowTime.getHour();
//分
int minute = nowTime.getMinute();
//秒
int second = nowTime.getSecond();
/***************** LocalTime ********************/
//这个不标准:2020-09-05T10:20:37.188
LocalDateTime.now().toString();
}
}

7.示例

求打印出前七天的日期,格式为:“2020年07月21日 11:22:22”

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; /**
* @description: TODO Calendar类基本使用
* @author: HaoWu
* @create: 2020/7/21 17:37
*/
public class CalendarDemo {
public static void main(String[] args) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
for (int i = 1; i < 8; i++) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE,-i);
Date time = calendar.getTime();
System.out.println(simpleDateFormat.format(time));
}
}
}

打印结果

2020年07月20日 18:21:44
2020年07月19日 18:21:44
2020年07月18日 18:21:44
2020年07月17日 18:21:44
2020年07月16日 18:21:44
2020年07月15日 18:21:44
2020年07月14日 18:21:44 Process finished with exit code 0

Java【常用的日期操作】的更多相关文章

  1. Java中的日期操作

    在日志中常用的记录当前时间及程序运行时长的方法: public void inject(Path urlDir) throws Exception { SimpleDateFormat sdf = n ...

  2. Java中的日期操作 分类: B1_JAVA 2015-02-16 17:55 6014人阅读 评论(0) 收藏

    在日志中常用的记录当前时间及程序运行时长的方法: public void inject(Path urlDir) throws Exception { SimpleDateFormat sdf = n ...

  3. Java 常用IO流操作详解

    1.基本概念 IO:Java对数据的操作是通过流的方式,IO流用来处理设备之间的数据传输,上传文件和下载文件,Java用于操作流的对象都在IO包中. 2.IO流的分类 图示:(主要IO流) 3.字节流 ...

  4. Java常用api和操作必背

    1.数组排序 Java的Arrays类(java.util中)包含用来操作数组(比如排序和搜索)的各种方法. Arrays.sort(各种类型数组) 2.数组转字符串 1)打印数组时可用Arrays. ...

  5. NSDate常用的日期操作

    // 当前时间创建NSDate NSDate *myDate = [NSDate date]; NSLog(@"myDate = %@",myDate); //从现在开始的24小时 ...

  6. Java经常使用日期操作具体解释

    Date类型大多数时间分量计算方法已经被Calendar代替 Date经常用法setTime getTime() new Date();默认获取当前的时间 SimpleDateFormat用来格式化和 ...

  7. java中的日期操作Calendar和Date

    1. Calendar转Date Calendar calendar = Calendar.getInstance(); Date date = calendar.getTime(); 2. Date ...

  8. Java常用日期操作

    对java中常用的日期操作进行整理. 1.日期格式化 /* * 日期格式化类(必须掌握) * API: * G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Mo ...

  9. java常用日期类总结

    java 常用的日期类有三个Date.SimpleDateFormat.Calendar

随机推荐

  1. populating-next-right-pointers-in-each-node leetcode C++

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  2. DeWeb --- Hello,World!

    1.新建一个DLL,命名为hello.dpr 2.新增一个Form.(File->New->VCL Form - Delphi),建议不要更改单元名称和Form名称,即分别为unit1.p ...

  3. 修改 openssh 版本号

    1.查看 sshd 位置 #which sshd 2.查看 /usr/sbin/sshd(二进制文件) 内容 #strings /usr/sbin/sshd | grep nicai 3.修改版本号, ...

  4. centos yum更换阿里镜像

    #1.如果没有wget命令,则需要执行下面命令进行安装.为保险期间,先执行下面命令. yum install wget #2.备份原镜像源,以免出错后可以恢复. mv /etc/yum.repos.d ...

  5. Visual Studio 2019连接MySQL数据库详细教程

    前言 如果要在 Visual Studio 2019中使用MySQL数据库,首先需要下载MySQL的驱动 Visual Studio默认只显示微软自己的SQL Server数据源,点击其它也是微软自己 ...

  6. cwRsync 实现两台服务器Windows Server 2012 R2间的文件同步(备份)

    sync下载链接:https://pan.baidu.com/s/1aZeGDA5bU9f1h6nxvVJsDw  提取码:jtv3 1.配置IP地址 Server端:192.167.1.2(自定义) ...

  7. GoLang设计模式14 - 状态模式

    状态模式,顾名思义,是一种基于有限状态机制的设计模式.在这种设计模式中,行为是由相应的状态来决定的.接下来我们会用一个售卖机的例子来说明下状态模式.为了便于说明,我们把场景简化一下,假设有一台售卖机只 ...

  8. 【JAVA】笔记(3)---封装;如何选择声明静态变量还是实例变量;如何选择声明静态方法还是实例方法;静态代码块与实例代码块的执行顺序与用途;

    封装: 1.目的:保证对象中的实例变量无法随意修改/访问,只能通过我们自己设定的入口,出口(set / get)来间接操作:屏蔽类中复杂的结构,使我们程序员在主方法中关联对象写代码时,思路/代码格式更 ...

  9. Java学习(十六)

    今天先学了文本标签 <p> <strong>永远不要相信诺克萨斯人的血条!</strong><!--表示一段内容的重要性--> <br /> ...

  10. Python如何格式化输出

    目录 Python中的格式化输出 1.旧格式化 2.新格式format( ) 函数 Python中的格式化输出 格式化输出就是将字符串中的某些内容替换掉再输出就是格式化输出 旧格式化输出常用的有%d( ...