Calendar类以及java8新特性的日期表示

日历类 Calendar

package com.sdut.k3;

import java.util.Calendar;

public class MyTest6 {
    public static void main(String[] args) {
        //获取日历  ------- 当前时间的日历
        Calendar cal = Calendar.getInstance();
        System.out.println(cal);

        System.out.println("年份:"+cal.get(Calendar.YEAR));
        System.out.println("月:"+(cal.get(Calendar.MONTH)+1));//月份是从0开始的
        System.out.println("日:"+cal.get(Calendar.DATE));
        System.out.println("日:"+cal.get(Calendar.DAY_OF_MONTH));
        System.out.println("时:"+cal.get(Calendar.HOUR_OF_DAY));//全日制
        System.out.println("时:"+cal.get(Calendar.HOUR));
        System.out.println("分:"+cal.get(Calendar.MINUTE));
        System.out.println("秒:"+cal.get(Calendar.SECOND));

        //星期天是第一天
        System.out.println("星期几:" + (cal.get(Calendar.DAY_OF_WEEK)+1));

        //获取特定时间的日历
        Calendar cal1 = Calendar.getInstance();
        cal1.set(Calendar.YEAR, 2018);
        //12月
        cal1.set(Calendar.MONTH, 11);
        cal1.set(Calendar.DAY_OF_MONTH, 1);
        System.out.println("星期几:" + cal1.get(Calendar.DAY_OF_WEEK));

        cal1.set(2019, 0, 1);
        System.out.println("星期几:" + cal1.get(Calendar.DAY_OF_WEEK));

    }
}

Java8新特性 java.time

LocalDate()
LocalTime()
LocalDateTime()
package com.sdut.k3;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class MyTest7 {
    public static void main(String[] args) {

        // java8  新特性  java.time
        //now():获取当前的日期、时间、日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);

        //of():设置指定的年、月、日、时、分、秒。没有偏移量
        LocalDateTime localDateTime1 = LocalDateTime.of(2021, 10, 6, 13, 23, 43);
        System.out.println(localDateTime1);


        //getXxx():获取相关的属性
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMinute());

        //体现不可变性
        //withXxx():设置相关的属性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System.out.println(localDate);
        System.out.println(localDate1);


        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println(localDateTime);
        System.out.println(localDateTime2);

        //不可变性
        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
        System.out.println(localDateTime);
        System.out.println(localDateTime3);

        LocalDateTime localDateTime4 = localDateTime.minusDays(6);
        System.out.println(localDateTime);
        System.out.println(localDateTime4);
    }

    }

以LacalDateTime为例 和特定格式字符串的转换

package com.sdut.k3;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class MyTest8 {
    public static void main(String[] args) {
        //创建LocalDateTime对象
        LocalDateTime dt1 = LocalDateTime.now();
        //LocalDateTime --->特定格式的字符串, LocalDateTime对象.format(预定义的格式)
        //预定义的标准格式
        String str1 = dt1.format(DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(str1);
        String s1 = dt1.format(DateTimeFormatter.ISO_DATE_TIME);
        System.out.println(s1);

        //自定义的格式
        //创建DateTimeFormatter 对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        String str2 = dt1.format(formatter);
        System.out.println(str2);

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss a");
        String str3 = dt1.format(formatter1);
        System.out.println(str3);

        //将特定格式的字符串转化为日期对象
        LocalDateTime localDateTime1 = LocalDateTime.parse("2022-11-16T11:13:06.415");
        System.out.println(localDateTime1);
        LocalDateTime localDateTime2 = LocalDateTime.parse("2022年11月16日 11:13:06 上午",formatter1);
        System.out.println(localDateTime2);

        //TemporalAccessor parse = formatter.parse("2022年01月07日 05:38:17");
        将特定格式的字符串转化为日期时间对象
        //LocalDateTime localDateTime3 = LocalDateTime.from(parse);
        //System.out.println(localDateTime3); //2022-01-07T05:38:17
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值