java日期类

本文介绍了Java中Date类和LocalDateTime类的使用,包括获取当前时间、毫秒值的计算、时间转换以及日期时间的增减和修改。同时,展示了如何进行Date和String类型之间的转换,并提供了计算年龄的小案例。此外,还涉及到了Period和Duration类在处理时间间隔上的应用。
摘要由CSDN通过智能技术生成

Date类

获取当前时间

//public Date();
//获取当前时间
Date date = new Date();
System.out.println(date);
//Mon Jul 31 09:14:42 CST 2023

1970/1/1 0:0:0 + date毫秒值

/public Date(long date)
 Date date1 = new Date(1000L);
System.out.println(date1);
//Thu Jan 01 08:00:01 CST 1970
//(获取的东八区的时间)

getTime()

//public long getTime() : 
//返回的是long类型的毫秒值 = 当前Date代表的时间 - 1970/1/1 0:0:0
Date date2 = new Date();
System.out.println(date2.getTime());
//1690766082936

setTime()

//public void setTime(long time) :
// Date对象 = 1970/1/1 0:0:0 + time毫秒值
Date date3 = new Date();
date3.setTime(1000L);
System.out.println(date3);
//Thu Jan 01 08:00:01 CST 1970

类型转换

Date类型转String类型

Date date4 = new Date();
//创建的是日期格式化对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDate = sdf.format(date4);
System.out.println(strDate);
//2023-07-31 09:14:42

String类型转Date类型

String s ="2020年10月31日 17:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date5 = sdf.parse(s);
System.out.println(date5);
//Sat Oct 31 17:00:00 CST 2020

小案例

计算自己活了多少年

//计算自己活力多少天
public class SimpleDateFormatTest {
    public static void main(String[] args) throws ParseException {
        Scanner sc  = new Scanner(System.in);
        System.out.println("请输入您的生日(xxxx-xx-xx)");
        String birthday = sc.nextLine();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = sdf.parse(birthday);

        //拿到生日到1970/1/1所经历的毫秒值
        long start = d1.getTime();

        //获取当前时间
        Date d2  = new Date();

        //拿到当前时间到1970/1/1所经历的毫秒值
        long end = d2.getTime();

        System.out.println("你活了 "+ (end-start)/(1000L*60*60*24*365)+" 年");
    }
}

LocalDateTime类

获取当前系统时间

// public static LocalDateTime now();
//获取当前系统时间
LocalDateTime time = LocalDateTime.now();
System.out.println(time);
//2023-07-31T10:18:07.620679700

使用指定年月日和时分秒初始化一个LocalDateTime对象

//public static LocalDateTime of (年,月,日,时, 分 ,秒)
//使用指定年月日和时分秒初始化一个LocalDateTime对象
 LocalDateTime time1 = LocalDateTime.of(2022, 11, 11, 12, 12, 12);
System.out.println(time1);
//2022-11-11T12:12:12

LocalDateTime获取功能

首先获取当前时间 
 LocalDateTime nowTime = LocalDateTime.now();

获取年

//获取年 public int getYear()
System.out.println(nowTime.getYear());
//2023

获取月份

//获取月份 public int getMonthValue()
 System.out.println(nowTime.getMonthValue());
 //7

获取月份中的第几天

//获取月份中的第几天 public int getDayOfMonth()
 System.out.println(nowTime.getDayOfMonth());
//31

获取一年中的第几天

// 获取一年中的第几天 public int getDayOfYear()
 System.out.println(nowTime.getDayOfYear());
 //212

获取星期

//获取星期 public DayOfWeek getDayOfWeek()
System.out.println(nowTime.getDayOfWeek());
//MONDAY

获取小时

//获取小时 public int getHour()
 System.out.println(nowTime.getHour());
//10

获取分钟

//获取分钟 public int getMinute()
 System.out.println(nowTime.getMinute());
//39

LocalDateTime() 修改

时间的增加或减少
        public LocalDateTime plusYears(long years )    添加或者减去年
        public LocalDateTime plusMonths(long months)   添加或者减去月
        public LocalDateTime plusDays(long days)       添加或者减去日
        public LocalDateTime plusHours(long hours)     添加或者减去时
        public LocalDateTime plusMinutes(long minutes) 添加或者减去分
        public LocalDateTime plusSecond(long seconds)   添加或者减去秒
        public LocalDateTime plusWeeks(long weeks)     添加或者减去周

 时间的修改
        public LocalDateTime withYear(int year )   直接修改年
        public LocalDateTime withMonth(int month)  直接修改月
        public LocalDateTime withDaysOfMonth(int dayofmonth)      直接修改日期(一个月中第几天)
        public LocalDateTime withDayOfYear(int dayofyear)    直接修改日期(一年中的第几天)
        public LocalDateTime withHour(int hour)    直接修改时
        public LocalDateTime withMinutes(int minute)直接修改分
        public LocalDateTime withSecond(int second)  直接修改秒

使用方法(演示一部分)

public class LocalDateTimeDemo3 {

    public static void main(String[] args) {
        //获取当前时间
        LocalDateTime nowTime = LocalDateTime.now();
        System.out.println(nowTime);
        //2023-07-31T11:26:13.024785700

        System.out.println(nowTime.plusYears(1));
        //2024-07-31T11:26:13.024785700
    }
}

类型转换

String转日期对象

//String --> 日期对象
String strDate = "2021/11/11 12:12:12";

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

LocalDateTime dataTime = LocalDateTime.parse(strDate, dateTimeFormatter);
System.out.println(dataTime);
 //2021-11-11T12:12:12

日期对象转换String 类型

 //格式化
LocalDateTime now = LocalDateTime.now();

 //DateTimeFormatter(日期格式化器)
//public static DateTimeFormatter ofPatter(String patter)
//使用指定的日期模板获取一个日期格式化器DateTimeFormatter对象

DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd HH:mm:ss:SSS");


//public String format(日期格式化对象)
//把一个LocalDate格式化成为一个字符串
String format = now.format(dateTimeFormatter);

System.out.println(format);
 //2023年07月31 11:45:01:368

Period(时间间隔类)(年月日)

public class PeriodAndDuration {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();

        LocalDate startTime = LocalDate.of(2021,11,11);
        LocalDate endTime = LocalDate.of(2030,12,12);
        Period between = Period.between(startTime, endTime);

        //间隔的年
        int years = between.getYears();
        System.out.println(years);
        //9

        //间隔月份
        int months = between.getMonths();
        System.out.println(months);
        //1

        //间隔天数
        int days = between.getDays();
        System.out.println(days);
        //1
    }
}

Duration(时分秒)

//Duration(时分秒)
LocalDateTime startTime1 = LocalDateTime.of(2020, 10, 10, 11, 11, 11);
LocalDateTime endTime1 = LocalDateTime.of(2020, 11, 10, 11, 11, 11);
Duration between1 = Duration.between(startTime1, endTime1);

//间隔天数
System.out.println(between1.toDays());
//31

//间隔的小时
System.out.println(between1.toHours());
//744

//间隔的分钟
System.out.println(between1.toMinutes());
//44640

学的不是技术,更是梦想!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

君生我老

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值