码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Java中时间类Date和Calendar的基本知识


    一、Date中常用的方法

    • int getYear() (返回现在到1900年所经历的年份)
    • int getMonth() (返回值是0-11)
    • int getDate() (返回的是月份中的某一天,返回值在1-31之间)
    • int getDay() (返回某周的第几天, 注: 周末为第一天, 返回值为0-6)
    • int getHours() (返回第几个小时)
    • int getMinutes() (返回多少分钟)
    • int getSeconds() (返回秒数)
    • long getTime() (获取日期对象从1970年的1月1日00:00:00到现在的时间毫秒数)
    • void setTime(long time) (设置时间,time为设置的时间到1970年1月1日00:00:00的时间毫秒数)
    • boolean after(Date when) (测试日期在指定日期之后则返回为true,否则为false)
    • boolean before(Date when) (测试日期在指定日期之前则返回为true,否则为false)

    二、Calendar 

    2.1、Calendar是一个接口,实例化接口有两种方法

    1. 1 : `Calendar calendar = new GregorianCalendar();`
    2. 2 : `Calendar calendar = Calendar.getInstance();`

    2.2、Calendar中常用的方法

    • calendar.getTimeInMills() (获取calendar到1970年的时间毫秒数)
    • calendar.getTime() (将当前calendar转换成Date类型);
    • calendar.get(Calendar.Year); (获取年份,与Date中获取年不同)
    • calendar.get(Calendar.DAY_OF_WEEK)等等很多类似的方法
    • calendar.set(Calendar.Year,2012);(将年份时间设置为2012年)等等也有很多类似的方法
    • calendar.set(year,month,date)set方法也可以这样写
    • calendar.add(Calendar.Year,-1); (倒退一年时间)

    例如: 让你求没一年的2月的最后一天可以用三月第一天然后都退一天得到

    三、Date,Calendar和String类型的联系与相互转换

    3.1、String---->Date---->Calendar

    1. String time = "20121023";
    2. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    3. Date parse = simpleDateFormat.parse(time);
    4. Calendar calendar = new GregorianCalendar();
    5. calendar.setTime(parse);

    3.2、Calendar---->Date---->String

    1. Calendar calendar = Calendar.getInstance();
    2. Date date = calendar.getTime();
    3. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
    4. String format = simpleDateFormat.format(date);

    注:
    1: SimpleDateFormat不能直接用于Calendar,它主要是用于String和Date类型的相互转换.
    2: System.current.TimeMills() (从目前到1970年的时间毫秒数)
    3: 年:yyyy, 月:MM, 日: dd, 时:HH, 分: mm, 秒: ss
    4: 中国在东八区,所以会在时间上加上8个小时.
     

  • 相关阅读:
    家常菜中的黄焖鸡
    微信管理系统的便捷功能:自动回复
    七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
    易云维®工厂能耗管理平台系统方案,保证运营质量,推动广东制造企业节能减排
    猿创征文 | 使用Jquery封装的AJAX请求数据
    基于余弦相似度改进蝴蝶优化算法-附代码
    云原生架构:面向初学者的完整概述
    《福布斯》:想要创建更好的代码?遵循这 14 个步骤
    一套.Net6可落地的微服务、分布式开源项目
    中空二氧化硅纳米球 中空SiO2纳米球 Hollow SiO2 nanosphere
  • 原文地址:https://blog.csdn.net/m0_50370837/article/details/126679085
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号