目录
  • 1. date日期类
    • 1.1 date的构造函数
    • 1.2 date的构造函数练习
    • 1.3 date的常用方法练习
  • 2. simpledateformat
  • 3.calendar日历类

1. date日期类

类 date 表示一个特定的瞬间,精确到毫秒

1.1 date的构造函数

date() 分配一个 date 对象,以表示分配它的时间(精确到毫秒)
date(long date) 分配一个 date 对象,表示自从标准基准时间起指定时间的毫秒数
标准基准时间:称为“历元(epoch)”,即 1970 年 1 月 1 日 00:00:00

1.2 date的构造函数练习

创建包:cn.cxy.date
创建类:testdate1

package cn.cxy.date;

import java.util.date;

/*本类用于date的入门*/
public class testdate1 {
    public static void main(string[] args) {
        //1.利用无参构造创建date类对象,注意导包:import java.util.date;
        date d1= new date();
        system.out.println(d1);//thu sep 23 23:14:59 cst 2021

        //2.利用含参构造创建date对象
        long t = 1000*60*60;//1000ms*60*60->1min*60->1h
        date d2 = new date(t);//从标准基础时间1970-1-1-00:00开始往后数1h
        system.out.println(d2);//thu jan 01 09:00:00 cst 1970
        //cst是中国时区,所以额外加了8h变成了09:00:00
    }
}

常用时间缩写
gmt 格林尼治时间 = utc 协调世界时间
utc + 时区差 = 本地时间,我们是在东八区,东加西减,所以
cst 中国标准时间:china standard time ut+8:00

1.3 date的常用方法练习

gettime() 返回自 1970 年 1 月 1 日 00:00:00 gmt 以来此 date 对象表示的毫秒值
settime(long time) 设置时间,表示 1970 年 1 月 1 日 00:00:00 gmt 后的毫秒值

创建包:cn.cxy.date
创建类:testdate2

package cn.cxy.date;

import java.util.date;

/*本类用于date的常用方法测试*/
public class testdate2 {
    public static void main(string[] args) {
        //1.创建date对象
        date d1 = new date();
        //2.获取从标准基准时间到现在时间的毫秒值,是一个时间差
        system.out.println(d1.gettime());//1632404114206
        //*1.0是为了变成浮点型,/1000变成秒,/60变成分,/60变成小时,/24变成天,/365变成年
        system.out.println(d1.gettime() * 1.0 / 1000 / 60 / 60 / 24 / 365);//约等于51,从1970到2021

        //3.打印d1就是当前的时间
        system.out.println(d1);//thu sep 23 21:39:29 cst 2021
        //4设置一个long值,作为毫秒为单位的时间差
        long t = 1000*60*60;
        d1.settime(t);
        system.out.println(d1);//打印的就是从标准基准时间经过时间差t的时间
    }
}

2. simpledateformat

simpledateformat 常用于格式化和解析日期
日期和时间格式由日期和时间模式字符串指定,字母与日期时间的对应关系如下:

2.1 常用构造函数

simpledateformat() 用默认模式的日期格式构造 simpledateformat
simpledateformat(string pattern) 用给定模式的日期格式构造 simpledateformat

2.2 日期转换格式工具类练习

创建包:cn.cxy.date
创建类:testsdf.java

package cn.cxy.date;

import java.text.parseexception;
import java.text.simpledateformat;
import java.util.date;

/*本类用于测试simpledateformat类*/
public class testsdf {
    public static void main(string[] args) throws parseexception {
        /*01 格式化:从 date 到 string*/
        //1.创建日期类对象,包含当前的时间
        date d = new date();
        //2.创建日期转换工具类的对象,此时使用的是无参构造
        //simpledateformat sdf = new simpledateformat();
        simpledateformat sdf2 = new simpledateformat("yyyy/mm/dd hh:mm:ss");
        //3.通过刚刚创建好的工具类对象转换日期的格式
        string s = sdf2.format(d);
        //4.打印转换好的时间字符串:
        //默认时间格式:21-9-23 下午11:18
        //自定义时间格式:2021/09/23 10:21:39
        system.out.println(s);
        
        /*解析:从string到 date*/
        //1.定义一个时间字符串
        string s2 = "2021/9/23 22:24:03";
        //报错:parseexception解析异常: unparseable date不可分割日期: "2021/9/23 22:24:03"
        //simpledateformat sdf3 = new simpledateformat("yyyy年mm月dd日 hh:mm:ss");
        //2.利用含参构造创建工具类对象,指定格式,注意必须是规定格式,否则会抛出异常
        simpledateformat sdf3 = new simpledateformat("yyyy/mm/dd hh:mm:ss");
        //3.利用工具类对象对日期进行转换
        date d2= sdf3.parse(s2);
        //4.打印转换好的日期
        system.out.println(d2);//thu sep 23 22:24:03 cst 2021

    }
}

2.3 日期转换综合练习

创建包:cn.cxy.date2
创建工具类:dateutils.java

package cn.cxy.date2;

import java.text.parseexception;
import java.text.simpledateformat;
import java.util.date;

/*本类用作日期类的工具类,一般工具类的要求:
* 构造方法私有化 & 成员方法静态化*/
public class dateutils {
    //1.创建一个私有化的构造方法
    private dateutils(){}

    //2.创建一个将日期转换为字符串的方法
    //参数:日期对象date  日期转换的格式:format 返回值类型:string
    public static string datetostring(date date, string format){
        //1.通过传入的日期格式,创建指定的工具类对象
        simpledateformat sdf = new simpledateformat(format);
        //2.通过工具类对象将字符串转换为对应的日期字符串,并拿到转换后的字符串结果
        string s = sdf.format(date);/*转换*/
        //3.将最终转换的字符串结果返回
        return s;
    }

    //3.创建一个将字符串转换为日期的方法
    //参数:字符串对象string  日期转换的格式:format 返回值类型:date
    public static date stringtodate(string s, string format) throws parseexception {
        //1.通过传入的日期格式,创建指定的工具类对象
        simpledateformat sdf = new simpledateformat(format);
        //2.通过工具类对象将日期对象转换为对应的字符串,并拿到转换后的日期对象
        date d = sdf.parse(s);/*解析*/
        //3.将最终转换的日期对象作为方法的结果返回
        return d;
    }
}

创建包:cn.cxy.date2
创建测试类:testdate.java

package cn.cxy.date2;

import java.text.parseexception;
import java.util.date;

/*本类用于自定义日期工具类的测试类*/
public class testdate {
    public static void main(string[] args) throws parseexception {
        //1.创建日期类对象
        date d = new date();
        //2.调用自定义日期工具类的功能:将日期对象转换成字符串
        string s = dateutils.datetostring(d, "yyyy年mm月dd日 hh:mm:ss");
        string s2 = dateutils.datetostring(d, "yyyy年mm月dd日");
        string s3 = dateutils.datetostring(d, "hh:mm:ss");
        system.out.println(s);//2021年09月23日 10:55:32
        system.out.println(s2);//2021年09月23日
        system.out.println(s3);//10:58:21

        //3.调用自定义工具类的功能,将字符串转换为对应的日期对象
        //注意,此处的format格式必须与s字符串定义时的格式一致!!!否则转换会报错!
        date d2 = dateutils.stringtodate(s, "yyyy年mm月dd日 hh:mm:ss");
        system.out.println(d2);//thu sep 23 23:01:21 cst 2021
    }
}

3.calendar日历类

3.1 概念

calendar 类是一个抽象类,不可直接实例化,但是它有一个直接已知子类gregoriancalendar
它为特定瞬间与还有比如年月日等日历字段之间的转换和操作提供了一些方法

3.2 常用方法

calendar提供了一个方法getinstance()用于获取calendar对象,它的日历字段就是当前的日期

int get(int field) 返回给定日历字段的值
abstract void add(int field, int amount) 根据日历的规则,为给定的日历字段添加或减去指定的时间量
void set(int year, int month, int date) 设置日历字段 year、month 和 day_of_month 的值

3.3 入门案例

创建包:cn.cxy.date3
创建类:testcalendar.java

package cn.cxy.date3;

import org.junit.test;

import java.util.calendar;

/*本类用于练习日历类*/
public class testcalendar {
    @test
    public void testget(){
        //1.获取日历对象
        calendar c = calendar.getinstance();
        system.out.println(c);//能获取很多信息,比如month=8,我们现在是9月,说明月份是从0开始的
        //2.通过获取到的日历对象,调用get(),传入对应的日历字段,就可以拿到对应的值
        int year = c.get(calendar.year);
        int month = c.get(calendar.month)+1;
        int day = c.get(calendar.day_of_month);
        system.out.println(year + "年" + month + "月" + day + "日");//2021年9月24日
    }
    @test
    public void testadd1(){
        //1.获取日历对象
        calendar c = calendar.getinstance();
        //2.给指定的日历字段进行指定数目的加减操作,得到10年后的今天
        c.add(calendar.year,+10);

        //3.通过获取到的日历对象,调用get(),传入对应的日历字段,就可以拿到对应的值
        int year = c.get(calendar.year);
        int month = c.get(calendar.month)+1;
        int day = c.get(calendar.day_of_month);
        //4.打印的就是10年后的今天:2031年9月24日
        system.out.println(year + "年" + month + "月" + day + "日");
    }

    @test
    public void testadd2(){//需求:打印1年后的7天后
        //1.获取日历对象
        calendar c = calendar.getinstance();
        //2.给指定的日历字段进行指定数目的加减操作,得到10年后的今天
        c.add(calendar.year,+1);
        c.add(calendar.day_of_month,+7);

        //3.通过获取到的日历对象,调用get(),传入对应的日历字段,就可以拿到对应的值
        int year = c.get(calendar.year);
        int month = c.get(calendar.month)+1;
        int day = c.get(calendar.day_of_month);
        //4.打印1年后的7天后:2022年10月1日
        system.out.println(year + "年" + month + "月" + day + "日");
    }

    @test
    public void testset(){
        //1.获取日历对象
        calendar c = calendar.getinstance();
        //2.测试set方法
        c.set(2099,9,1);
        //3.通过获取到的日历对象,调用get(),传入对应的日历字段,就可以拿到对应的值
        int year = c.get(calendar.year);
        int month = c.get(calendar.month)+1;
        int day = c.get(calendar.day_of_month);
        //4.打印:2099年10月1日
        system.out.println(year + "年" + month + "月" + day + "日");
    }
}

3.4 巩固案例

创建包:cn.cxy.date3
创建类:testcalendar.java
需求:用户任意输入一个年份,输出这年的2月有多少天

package cn.cxy.date3;

import java.util.calendar;
import java.util.scanner;

/*本类用于日历类的巩固练习
 * 需求:获取任意一年的2月有多少天*/
public class testfeb {
    public static void main(string[] args) {
        //1.提示并接收用户要测试的年份:
        system.out.println("请您输入要查询的年份:");
        int year = new scanner(system.in).nextint();
        //2.获取日历类对象,并将时间设置为用户输入那年的3月1日
        calendar c = calendar.getinstance();
        c.set(year, 2, 1);
        //3.三月一日往前推一天就是2月的最后一天
        c.add(calendar.date, -1);
        //4.获取这一天输出即可
        int date = c.get(calendar.date);
        system.out.println(year + "年的2月有" + date + "天");
    }
}

到此这篇关于新手小白学java 日期类date simpledateformat calendar(入门)的文章就介绍到这了,更多相关java date simpledateformat calendar内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!