你想成为时间管理大师吗?狗头(#^.^#)——在线辅导

目录

Date

Calendar

 LocalDateTime

 在运用中常见的计算时间差值的案例

Date;

Carlendar:

LocalDateTime:

总结:


Date

概述:Date代表了一个特定的时间,精确到毫秒

构造方法:

 代码演示:

Date d1 = new Date();
        System.out.println(d1);
        System.out.println("---------------------------------------");
        long date = 1000*60*60;
        Date d2 = new Date(date);
        System.out.println(d2);

运行结果:

 常用方法:

 代码演示:

public class DateDemo02 {
    public static void main(String[] args) {
        //创建日期对象
        Date d = new Date();

        //public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
//        System.out.println(d.getTime());
//        System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");

        //public void setTime(long time):设置时间,给的是毫秒值
//        long time = 1000*60*60;
        long time = System.currentTimeMillis();
        d.setTime(time);

        System.out.println(d);
    }
}

 运行结果:

Calendar

概述:

  • Calendar 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
  • Calendar 提供了一个类方法 getInstance 用于获取这种类型的一般有用的对象,其日历字段已使用当前日期和时间初始化:Calendar rightNow = Calendar.getInstance();

方法:

 代码演示:

Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");
        System.out.println("------------------------------------");
        c.set(2050,10,10);
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH) + 1;
        date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");

运行结果:

 LocalDateTime

        使用新时间日期API的必要性

在java8以前,或许:

  • 当你在做有关时间日期的操作时,你会想到用Date;
  • 当你在做日期、月份、天数相加减时,你会想到用Calendar;
  • 当你需要对时间日期进行格式化时,你会想到使用SimpleDateFormat或DateFormat下的其他子类;

        但是,你必须知道,以上有关的时间日期操作对象,都是可变的、线程不安全的,同时,如果作为一个经常写过类似代码的人来说,尽管有相关对象提供某些操作,但并不能很快、很简单的就能得到最终想要的结果,如:要计算两个时间点之间相差的年、月、日、周、时、分、秒等,这些计算尽管原有API也能够实现,但原有API除了线程不安全之外,另外一个不足之处就是代码繁琐,性能低!

       没错,java8出的新的时间日期API都是线程安全的,并且性能更好,代码更简洁!

        其中,LocalDate、LocalTime、LocalDateTime是新API里的基础对象,绝大多数操作都是围绕这几个对象来进行的,有必要搞清楚:

        LocalDate : 只含年月日的日期对象
        LocalTime :只含时分秒的时间对象
        LocalDateTime : 同时含有年月日时分秒的日期对象

三者的区别(LocalDate、LocalTime、 LocalDateTime ):

1.表示时间的区别

代码演示:

LocalDate localDate = LocalDate.of(2018, 1, 13);
LocalTime localTime = LocalTime.of(9, 43, 20);
LocalDateTime localDateTime = LocalDateTime.of(2018, 1, 13, 9, 43, 20);
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);

运行结果:

 2.LocalDateTime对于时间的修改更加准确

兄弟们上代码:

LocalDateTime current = LocalDateTime.now();
        System.out.println("当前时刻 : " + current);
        System.out.println("当前年份 : " + current.getYear());
        System.out.println("当前月份 : " + current.getMonth());
        System.out.println("当前日份 : " + current.getDayOfMonth());
        System.out.println("当前时   : " + current.getHour());
        System.out.println("当前分   : " + current.getMinute());
        System.out.println("当前秒   : " + current.getSecond());

 

 修改具体的值:

LocalDateTime localDateTime = LocalDateTime.now();
//以下方法的参数都是long型,返回值都是LocalDateTime
LocalDateTime plusYearsResult = localDateTime.plusYears(2L);
LocalDateTime plusMonthsResult = localDateTime.plusMonths(3L);
LocalDateTime plusDaysResult = localDateTime.plusDays(7L);
LocalDateTime plusHoursResult = localDateTime.plusHours(2L);
LocalDateTime plusMinutesResult = localDateTime.plusMinutes(10L);
LocalDateTime plusSecondsResult = localDateTime.plusSeconds(10L);
        
System.out.println("当前时间是 : " + localDateTime + "\n"
        + "当前时间加2年后为 : " + plusYearsResult + "\n"
        + "当前时间加3个月后为 : " + plusMonthsResult + "\n"
        + "当前时间加7日后为 : " + plusDaysResult + "\n"
        + "当前时间加2小时后为 : " + plusHoursResult + "\n"
        + "当前时间加10分钟后为 : " + plusMinutesResult + "\n"
        + "当前时间加10秒后为 : " + plusSecondsResult + "\n"
        );
    
//也可以以另一种方式来相加减日期,即plus(long amountToAdd, TemporalUnit unit)
//                  参数1 : 相加的数量, 参数2 : 相加的单位
LocalDateTime nextMonth = localDateTime.plus(1, ChronoUnit.MONTHS);
LocalDateTime nextYear = localDateTime.plus(1, ChronoUnit.YEARS);
LocalDateTime nextWeek = localDateTime.plus(1, ChronoUnit.WEEKS);
        
System.out.println("now : " + localDateTime + "\n"
        + "nextYear : " + nextYear + "\n"
        + "nextMonth : " + nextMonth + "\n"
        + "nextWeek :" + nextWeek + "\n"
        );
        
//日期的减法用法一样,在此不再举例

运行结果:

 在运用中常见的计算时间差值的案例

Date;

/*
 * 算一下你来到这个世界多少天?
 * 
 * 分析:
 * 		A:键盘录入你的出生的年月日
 * 		B:把该字符串转换为一个日期
 * 		C:通过该日期得到一个毫秒值
 * 		D:获取当前时间的毫秒值
 * 		E:用D-C得到一个毫秒值
 * 		F:把E的毫秒值转换为年
 * 			/1000/60/60/24
 */
public class MyYearOldDemo {
	public static void main(String[] args) throws ParseException {
		// 键盘录入你的出生的年月日
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的出生年月日:");
		String line = sc.nextLine();

		// 把该字符串转换为一个日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d = sdf.parse(line);

		// 通过该日期得到一个毫秒值
		long myTime = d.getTime();

		// 获取当前时间的毫秒值
		long nowTime = System.currentTimeMillis();

		// 用D-C得到一个毫秒值
		long time = nowTime - myTime;

		// 把E的毫秒值转换为年
		long day = time / 1000 / 60 / 60 / 24;

		System.out.println("你来到这个世界:" + day + "天");
	}
}

Carlendar:


        Calendar c = Calendar.getInstance();
        int n = 0;
        //public int get(int field):返回给定日历字段的值
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int date = c.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");
        Calendar c1 = Calendar.getInstance();
        c1.set(2021, 10, 25);
        year = c1.get(Calendar.YEAR);
        month = c1.get(Calendar.MONTH) + 1;
        date = c1.get(Calendar.DATE);
        System.out.println(year + "年" + month + "月" + date + "日");
        while (c.before(c1)) {
            n++;
            c.add(Calendar.DATE, 1);
        }
        System.out.println(n);

LocalDateTime:

//计算两个日期的日期间隔-年月日
LocalDate date1 = LocalDate.of(2020, 10, 21);
LocalDate date2 = LocalDate.of(2019, 9, 11);
//内部是用date2-date1,所以得到的结果是负数
Period period = Period.between(date1, date2);
System.out.println("相差年数 : " + period.getYears());
System.out.println("相差月数 : " + period.getMonths());
System.out.println("相差日数 : " + period.getDays());
//另一种计算方式和表现形式
System.out.println("-------------------------------");
long years = period.get(ChronoUnit.YEARS);
long months = period.get(ChronoUnit.MONTHS);
long days = period.get(ChronoUnit.DAYS);
System.out.println("相差的年月日分别为 : " + years + "," + months + "," + days);

总结:

1.出现的差异:

        Date是JDK1.0提供的。

        Calendar是在JDK1.1提供的,它替代了Date中的很多方法。

        LocalDate是在JDK1.8提供的。

2.准确度的差异

        Date和Calendar在实例化时会有偏移量(例如实例化"2000-1-2"),Date的year偏移量是1900,month偏移量是1(0~11月,在指定月份时要-1)。Calendar的月份有偏移量1,和Date一样,年份没有偏移量。

        LocalDate(LocalTime LocalDateTime)没有偏移量。
3.底层原理:

        Date和Calendar如果修改了它们的某个日期值,是直接改变其原本的值,类比对基础数据类型的修改。

        LocalDate(LocalTime LocalDateTime)对它们的修改不会改变其原本的值,而是会返回一个新对象,类比String的不可变性。


猜你喜欢

转载自blog.csdn.net/m0_59619191/article/details/120899237