Java日期类(Date、Calendar、LocalDateTime)

Java日期类(Date、Calendar、LocalDateTime)

一.Date

场景1:计算某天距今某天(或今天)过了多少天

计算某天距今天过了多少天(比如:2020-11-25距2018-10-8过了779天)
1-键盘录入某一天,将录入的String转换为Date日期类
2-日期类Date,转换为ms值(距离1979-1-1日过了多少ms)
3-同理,输入另外某一天(如果是此时此刻当天,就使用系统时间,不需要手动输入),也转换为ms值
只有毫秒值才能计算差,日期不可以计算
days =diffMs / 1000 /60 /60 / 24

public class Main
{
    public static void main(String[] args) throws ParseException
    {
        //求今天的日期(比如2020-11-25)
        Date date = new Date();
        //输入要求的那天日期
        Scanner sc = new Scanner(System.in);
        System.out.println("输入日期,即今天距离的那天格式为yyyy-MM-dd");
        String oneday = sc.nextLine();
        //调用DateFormat类
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //将字符串转化为日期
        Date date1 = sdf.parse(oneday);
        //调用Date类的方法,求出该日期,距离1971-1-1过了多少ms
        long timee = date.getTime();
        long time1 = date1.getTime();
        //将ms差,转换为天
        long diffdays = (timee - time1) / 60 / 60 / 24 / 1000;
        System.out.println(diffdays);
    }
}
场景2:查看当前时间(字符串格式)
        Date date = new Date();//Wed Nov 25 14:28:31 CST 2020
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = sdf.format(date);
        System.out.println(str);//2020-11-25 14:27:37
缺陷:线程不安全

1-Date日期类需要使用SimpleFormat类对日期date(Wed Nov 25 14:28:31 CST 2020)进行格式化成String类型(2020-11-25 14:27:37),否则可读性太差
2-但,SimpleDateFormat类对象的format(date)方法,底层会调用calendar.setTime(date)

private StringBuffer format(Date date, StringBuffer toAppendTo, FieldDelegate delegate) {
        // Convert input date to time field list
        calendar.setTime(date);
        boolean useDateFormatSymbols = useDateFormatSymbols();
        for (int i = 0; i < compiledPattern.length; ) {
           //逻辑代码
        }
        return toAppendTo;
    }

3-在多线程时,会存在线程不安全问题。
因为SimpleDateFormat对象sdf被多个线程调用sdf.format()方法时,加入t1线程进入方法,执行了calendar.setTime(date),设置成功了;此时t2线程也执行该方法,也执行setTime(date),这就会导致线程在切换时,t1线程设置好的时间被修改了,导致t1返回的时间格式是错误的。
4-当然,可以使用ThreadLocal使线程和共享变量sdf一一绑定,解决线程不安全的问题。或者使用LocalDateTime类


二.Calendar

场景1:某年某月某日(2020-11-25)(或者此时此刻)是当年的第多少周、第多少天、星期几等
		//此时此刻
        Calendar calendar = Calendar.getInstance();//今天是2020/11/25
        int days = calendar.get(Calendar.DAY_OF_YEAR);//330

        int weeks = calendar.get(Calendar.WEEK_OF_YEAR);//48

        int which = calendar.get(Calendar.DAY_OF_WEEK)-1;//3即周三(之所以减1,是因为国外是周天-1-2-3-4-5-6)
	//某事某刻
	Calendar calendar = Calendar.getInstance();
        calendar.set(2020, 11-1, 25);//这里如果想查看是11月就输入10,因为中国是1-12月
        int days = calendar.get(Calendar.DAY_OF_YEAR);//330

        int weeks = calendar.get(Calendar.WEEK_OF_YEAR);//48

        int which = calendar.get(Calendar.DAY_OF_WEEK)-1;//3即周三(之所以减1,是因为国外是周天-1-2-3-4-5-6)

三.LocalDateTime

场景1:计算某天距今某天(或今天,即此时此刻)过了多少天(和Date类场景1相同),线程安全!!
public class LocalDateTimeDemo
{
    public static void main(String[] args)
    {
        
        LocalDateTime localDateTime = LocalDateTime.now();//此时此刻
        ZonedDateTime zone = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));//“America/Los_angles”
        long time = zone.toInstant().toEpochMilli();//将ZoneDateTime转换为距离1979-1-1过了多少ms

        LocalDateTime ldf = LocalDateTime.of(2018, 10, 8, 14, 50);
        ZonedDateTime zone1 = ldf.atZone(ZoneId.of("Asia/Shanghai"));
        long time2 = zone1.toInstant().toEpochMilli();

        System.out.println((time - time2) / 1000 / 60 / 60 / 24);
    }
}
场景2:某年某月某日(2020-11-25)(或者此时此刻)是当年的第多少周、第多少天、星期几等(和Calendar类场景1相同)
        LocalDateTime ldt = LocalDateTime.now();//2020/11/25
        
        int dayOfYear = ldt.getDayOfYear();//是第330天

        int dayofweek = ldt.getDayOfWeek().getValue();//周三

		//貌似,没有找到求2020/11/25是当年的第多少周的api
场景3:日期的加减法

eg:求60s之前的时间
eg:求60s之后的时间
···
eg:求50分钟前的时间
eg:求50分钟后的时间
···
eg:求2小时之前的时间
eg:求2小时之后的时间
···
eg:求一周之前的时间
eg:求一周之后的时间
···
eg:求779天之前的时间
eg:求100天之后的时间

(1).second的加减法:
		//60s之前,即减法
        LocalDateTime ldt = LocalDateTime.now();//此时此刻:2020-11-25T15:34:23.398
        //60s前:
        LocalDateTime time = ldt.minusSeconds(60);//2020-11-25T15:33:23.398 
        //60s之后,即加法
        LocalDateTime time = ldt.plusSeconds(60);//2020-11-25T15:35:23.398
(2).minute的加减法:
        LocalDateTime ldt = LocalDateTime.now();//2020-11-25T15:40:45.340
        //50分钟前
        LocalDateTime time = ldt.minusMinutes(50);//2020-11-25T14:50:45.340
        //50分钟后
        LocalDateTime time1 = ldt.plusMinutes(50);//2020-11-25T16:30:45.340
(3).hour的加减法:
        LocalDateTime ldt = LocalDateTime.now();//2020-11-25T15:46:22.499
        //2h之前
        LocalDateTime time = ldt.minusHours(2);//2020-11-25T13:46:22.499
        //2h之后
        LocalDateTime time1 = ldt.plusHours(2);//2020-11-25T17:46:22.499
(4).week的加减法:
        LocalDateTime ldt = LocalDateTime.now();//2020-11-25T15:49:53.562
        //1周之前
        LocalDateTime time = ldt.minusWeeks(1);//2020-11-18T15:49:53.562
        //1周之后
        LocalDateTime time1 = ldt.plusWeeks(1);//2020-12-02T15:49:53.562
(5).day的加减法:
        LocalDateTime ldt = LocalDateTime.now();//2020-11-25T15:53:48.771
        //779天之前
        LocalDateTime time = ldt.minusDays(779);//2018-10-08T15:53:48.771
        //20天周之后
        LocalDateTime time1 = ldt.plusDays(20);//2020-12-15T15:53:48.771

总结:JDK1.8之后推荐使用LocalDateTime类来处理日期。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值