关于时间API

1.Java.lang.System
public static long currentTimeMillis(); 主要用来计算时间差 返回当前时间与1970年1月1日0时0分0秒的时间差(毫秒数)

2.Java.util.Date
表示特定瞬间,精确到毫秒
1)两个构造器的使用
Date dt = new Date();
System.out.println(dt);//Thu Jan 20 11:47:51 CST 2022

    long time = 1000*60*60;
    Date dt2 = new Date(time);
    System.out.println(dt2);//Thu Jan 01 09:00:00 CST 1970
2)两个方法的使用
	->toString()  显示年月日时分秒星期的
	->getTime() 获取当前时间与1970年1月1日0时0分0秒的时间差(毫秒数)
	->setTime(long) 设置时间对象
   Date date1 = new Date();//空参构造器
   System.out.println(date1.toString());//Fri Dec 31 14:50:06 CST 2021   toString方法
	Date date2 = new Date(1640933674184L);//设定指定时间的对象
	
	System.out.println(d1.getTime());//1650623619839
	
	long date = 1000*60*60;
	d1.setTime(date);
    System.out.println(d1);//Thu Jan 01 09:00:00 CST 1970

3.java.sql.Date 对应数据库中日期的类型

4.SimpleDateFormat
SimpleDateFormat:对Date日期的格式化和解析
1.两个功能
1.1格式化 日期–》指定格式的字符串
1.2解析 字符串转换为日期

2.SimpleDateFormat实例化
			//实例化使用默认构造器
        SimpleDateFormat sdf= new SimpleDateFormat();

        //格式化  日期--》字符串
        Date date = new Date();
        String format = sdf.format(date);
        System.out.println(format);

			//解析
        String str = "22-1-4 下午4:37";

        Date date1 = sdf.parse(str);

        System.out.println(date1);//Tue Jan 04 16:37:00 CST 2022
				SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        //格式化  日期--》字符串
        Date date = new Date();
        String format = sdf.format(date);
        System.out.println(format);//2022-01-04 04:57:20

        //解析
        String str = "2022-11-12 13:12:44";

        Date date1 = sdf.parse(str);

        System.out.println(date1);//Sat Nov 12 13:12:44 CST 2022

5.Calendar:日历类的使用
抽象类,主要用于完成日期字段之间相互操作之间的功能
可以使用getInstance方法

    public static void main(String[] args) throws Exception{
        //实例化
        //方式一:创建其子类的对象
        //方式二:调用它的静态方法
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.getClass());//GregorianCalendar

        //常用方法
        //get
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(day);
        day = calendar.get(Calendar.DAY_OF_WEEK);
        System.out.println(day);

        //set
        calendar.set(Calendar.DAY_OF_MONTH,11);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//11

        //add
        calendar.add(Calendar.DAY_OF_MONTH,3);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//14

        //getTime日历类--->date
        Date date = calendar.getTime();
        System.out.println(date);

        //setTime date--->日历类
        Date date1 = new Date();
        calendar.setTime(date1);
    }

6 java.time

			//now获取当前时间和日期
				LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);//2022-01-05
        System.out.println(localTime);//17:20:41.919
        System.out.println(localDateTime);//2022-01-05T17:20:41.919

			//of设置指定的年月日时分秒
			 LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
        System.out.println(localDateTime);//2020-11-11T11:11:11
			
			//get***
        System.out.println(localDateTime.getDayOfMonth());//11
        System.out.println(localDateTime.getMonth());//NOVEMBER

			//with***设置
        LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(22);
        System.out.println(localDateTime);//2020-11-11T11:11:11
        System.out.println(localDateTime1);//2020-11-22T11:11:11

			//相加plus***
        LocalDateTime localDateTime2 = localDateTime.plusHours(2);
        System.out.println(localDateTime);//2020-11-11T11:11:11
        System.out.println(localDateTime2);//2020-11-22T13:11:11

				// 相减minus***
        LocalDateTime localDateTime3 = localDateTime.minusMonths(2);
        System.out.println(localDateTime);//2020-11-11T11:11:11
        System.out.println(localDateTime3);//2020-09-22T13:11:11

7.Instant

//初始化
        Instant instant = Instant.now();//获取本初子午线对应的标准时间
        System.out.println(instant);//2022-01-06T11:45:27.155Z

        // 偏移
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));//获取北京时间+8小时
        System.out.println(offsetDateTime);//2022-01-06T19:45:27.155+08:00

        //获取对应的毫秒数 toEpochMilli
        long milli = instant.toEpochMilli();
        System.out.println(milli);//1641469527155

        //设置对应时间ofEpochMilli
        Instant instant2 = Instant.ofEpochMilli(1641469527155L);
        System.out.println(instant2);//2022-01-06T11:45:27.155Z

8.DateTimeFormatter
格式化或解析日期时间

//创建实例
        //方式一预定义标准格式:ISO_LOCAL_DATE_TIME
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
        //格式化  日期--->字符串
        LocalDateTime localDateTime = LocalDateTime.now();
        String format = formatter.format(localDateTime);
        System.out.println(localDateTime);//2022-01-07T09:26:41.428
        System.out.println(format);//2022-01-07T09:26:41.428

        //字符串--->日期
        TemporalAccessor parse = formatter.parse("2022-01-07T09:26:41.428");
        System.out.println(parse);//{},ISO resolved to 2022-01-07T09:26:41.428

			//方式二:本地相关格式化 ofLocalizedDate  日期--->字符串
        DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
        String format1 = formatter1.format(localDateTime);
        //System.out.println(format1);//22-1-7FormatStyle.SHORT
        System.out.println(format1);//2022年1月7日

        //字符串--->日期
        TemporalAccessor parse1 = formatter1.parse("2022年1月7日");
        System.out.println(parse1);//{},ISO resolved to 2022-01-07

			//自定义格式化 通常使用这个  ofPattern("yyyy-MM-dd hh:mm:dd")
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:dd");
        String format2 = formatter2.format(localDateTime);
        System.out.println(format2);//2022-01-07 10:18:07

        //字符串--->日期
        TemporalAccessor parse2 = formatter2.parse("2022-01-07 10:18:07");
        System.out.println(parse2);//{HourOfAmPm=10, MinuteOfHour=18},ISO resolved to 2022-01-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值