java学习笔记-Date日期

java学习笔记-Date日期

常识

世界标准时间:GMT 格林威治时间

原子钟

中国的标准时间 : 世界的标准时间 + 8小时

计算机中的时间起点:1970年 1月1日 00:00:00 C语言的生日

1.Date

精确到毫秒

  1. 构造方法

不带参数构造:将当前时间封装成一个对象
带参数构造:从时间原点开始 过了指定毫秒的时间 封装成一个Date对象 需要考虑时差的问题

package com.ustc.test9;

import java.util.Date;

public class datedate {
    
    

    public static void main(String[] args) {
    
    

        Date date1 = new Date();
        System.out.println(date1);//表示当前时间

        Date date2 = new Date(0L);
        System.out.println(date2);//表示计算机起点时间  + 八个小时


        //计算机起点时间 + 九小时
        Date date3 = new Date(3600L * 1000);
        System.out.println(date3);

    }
}

  1. 成员方法
    pubLic long getTime() //获取时间对象的毫秒值
    Public void setTime() //设置时间 传递毫秒值
	package com.ustc.test9;
	
	import java.util.Date;
	
	public class date1 {
    
    
	    public static void main(String[] args) {
    
    
	
	
	        //将当前时间封装成一个date对象
	        Date date1 = new Date();
	        //获取当前时间的毫秒值
	        long time = date1.getTime();
	        System.out.println(time);
	        System.out.println(System.currentTimeMillis());
	
	    }
	}

	package com.ustc.test9;
	
	import java.util.Date;
	
	public class date1 {
    
    
	    public static void main(String[] args) {
    
    
	        //将当前时间封装成一个date对象
	        Date date1 = new Date();
	        //获取当前时间的毫秒值
	        date1.setTime(0L);//设置成时间原点
	        System.out.println(date1);
	        System.out.println(System.currentTimeMillis());
	
	    }
	}
	
	

2. SimpleDateFormat

将Date对象进行格式化和解析

  1. 两个构造方法:用于设置日期格式
public SimpleDateFormat()  //默认格式
public SimpleDateFormat(String pattern) //使用指定的格式
  1. 成员方法:这里的日期格式全部都是上面的构造方法中的指定的格式
public final String format(Date date)   //将日期格式转换成日期/时间字符串       Date->字符串
public Date parse(String source)        //从给定的字符串开始解析文本以生成日期  字符串->Date
	package com.ustc.test9;
	
	import org.omg.CORBA.DATA_CONVERSION;
	
	import java.text.ParseException;
	import java.text.SimpleDateFormat;
	import java.util.Date;
	
	public class datedemo3 {
    
    
	    public static void main(String[] args) throws ParseException {
    
    
	
	        Date date1 = new Date();
	        //创建一个日期格式
	        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
	        String s = sdf.format(date1);//根据指定的日期格式生成字符串  date->字符串
	        System.out.println(s);
	
	
	        String s1 = "2021-11-01";
	        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
	        Date date2 = sdf1.parse(s1);
	
	        System.out.println(date2);
	
	    }
	}

	package com.ustc.test9;
	import java.text.ParseException;
	import java.text.SimpleDateFormat;
	import java.util.Date;
	
	public class demo4 {
    
    
	
	    public static void main(String[] args) throws ParseException {
    
    
	        //将字符串变成毫秒值
	        String start = "2020年11月11日 0:0:0";
	        String end  = "2020年11月11日 0:10:0";
	
	        String jia = "2020年11月11日 0:03:47";
	        String pi = "2020年11月11日 0:10:11";
	
	        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:MM:SS");
	        long startTime = sdf1.parse(start).getTime();   //将字符串转换成Date对象  然后获得毫秒值
	        long endTime = sdf1.parse(end).getTime();     //获得结束时间
	
	        System.out.println(startTime);
	        System.out.println(endTime);
	
	        long jiaTime = sdf1.parse(jia).getTime();
	        long piTime = sdf1.parse(pi).getTime();
	
	        if(jiaTime >= startTime && jiaTime <= endTime)
	        {
    
    
	            System.out.println("小家同学参加了上次的秒杀活动");
	        }
	        else
	        {
    
    
	            System.out.println("小家同学没有参加上次的秒杀活动");
	        }
	
	
	        if(piTime >= startTime && piTime <= endTime)
	        {
    
    
	            System.out.println("小家同学参加了上次的秒杀活动");
	        }
	        else
	        {
    
    
	            System.out.println("小家同学没有参加上次的秒杀活动");
	        }
	
	    }
	}

3.JDK8中的日期工具类

需求:给定一个日期字符串 将该日期加上一天 在转换成字符串

使用SimpleDateFormat类 创建格式对象,然后调用该对象方法parse,将字符串解析成Date对象

然后使用getTime获取时间 加上一天 之后再转换成Date对象,然后在使用format转换成格式字符串

  1. LocalDateTime
	package com.ustc.test9;
	
	import java.text.ParseException;
	import java.text.SimpleDateFormat;
	import java.util.Date;
	
	public class demo1 {
    
    
	
	    public static void main(String[] args) throws ParseException {
    
    
	        String s = "2020年11月11日 00:00:00";
	        SimpleDateFormat sdf =new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
	        Date date = sdf.parse(s);  //将字符串转换成一个Date对象   之后获取时间
	        long time = date.getTime();
	        time = time + (1000*60*60*24);
	        Date newDate = new Date(time);//将时间重新封装成一个Date对象
	        String ss = sdf.format(newDate);//再次将新的Date对象转换成一个字符串  按照构造方法中的格式
	        System.out.println(ss);
	    }
	}

简化方法:使用LocalDateTime 先设定指定格式(创建DateTimeFormatter对象),使用LocalDateTime类创建对象 按照指定的格式对象 , 然后将日期加上一天 (使用plusDays()方法)

String s = "2020年11月11日 00:00:00";
	        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
	        LocalDateTime localDateTime = LocalDateTime.parse(s,pattern);
	        LocalDateTime newlocalDateTime1 = localDateTime.plusDays(1);
	        String result = newlocalDateTime1.format(pattern); //按照指定格式转换成字符串
        System.out.println(result);

介绍几个类:localDate 表示日期
localTime 表示时间 时分秒
LocaDateTime 表示全部时间

  1. localDateTime创建 LocalDateTime对象
public static LocalDateTime now()  //获取当前系统时间
public static LocalDateTime of(年,月,日,时,分,秒)   //使用指定的时间初始化成一个事件对象

获取方法:首先创建LocalDateTime对象 然后使用该对象调用成员方法

		Public int getYear()   获取年份
		Public int getMonthValue()  获取月份
		pubLic int getDayOfMonth()   获取月份中的第几天
		Public int getDayOfYear()   获取一年中的第几天
		Public DayOfWeek getDayOfWeek()   获取星期
		Public int getMinute()  获取分钟  
		Public int getHour() 获取小时

		package com.ustc.test9;
		
		import com.sun.prism.shader.AlphaOne_Color_AlphaTest_Loader;
		import javafx.util.converter.LocalDateTimeStringConverter;
		
		import java.text.ParseException;
		import java.text.SimpleDateFormat;
		import java.time.DayOfWeek;
		import java.time.LocalDateTime;
		import java.time.format.DateTimeFormatter;
		import java.util.Date;
		
		public class demo1 {
    
    
		
		    public static void main(String[] args) throws ParseException {
    
    
		        LocalDateTime localDateTime = LocalDateTime.of(2020,11,11,11,11,11);
		        int year = localDateTime.getYear(); //获取年份
		        System.out.println(year);
		
		        int month = localDateTime.getMonthValue();   //获取月份
		        System.out.println(month);
		
		        int day = localDateTime.getDayOfMonth(); // 获取月份中的第几天
		        System.out.println(day);
		
		
		        int dayOfYear = localDateTime.getDayOfYear(); ///获取年份中第几天
		        System.out.println(dayOfYear);
		
		        DayOfWeek dayofweek = localDateTime.getDayOfWeek();//获取星期
		        System.out.println(dayofweek);
		
		    }
		
		}

LocalDateTime 转换方法:
		
		pubLic localDate toLocalDate()  转换成一个LocalDate对象    只保留年月日
		Public localTime toLocalTime()   转换成一个LocalTime    只保留时分秒
		
		LocalDate localdate = localDateTime.toLocalDate();    //只获取年月日
		System.out.println(localdate);
		
		 LocalTime localTime = localDateTime.toLocalTime();  //只获取时分秒
		 System.out.println(localTime);

  1. localDateTime格式化与解析
Public String format(指定格式)     把一个LocalDateTime 格式化成为一个日期字符串
Public LocalDateTime parse(准备解析的字符串,解析格式)   把一个日期字符串解析成为一个LocalDateTime对象

DateTimeFormatter JDK8的日期格式化容器:

Public static DateTimeForMatter ofPattern(String pattern) 使用指定的日期模板 获取一个日期格式化容器 DateTimeFormatter对象 这里产生的pattern 正好是上面两个方法用到的指定格式

		package com.ustc.test9;
		
		import java.time.LocalDateTime;
		import java.time.format.DateTimeFormatter;
		
		public class localdatetime1 {
    
    
		
		    public static void main(String[] args) {
    
    
		
		        //将传入的时间数字   封装成一个LocalDateTime对象   of方法是使用指定的时间日期  包装成一个时间对象
		        LocalDateTime localDateTime = LocalDateTime.of(2020,11,11,11,11,11);
		        System.out.println(localDateTime);
		        
		        //指定一个时间格式 JDK8   创建格式对象
		        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
		
		        //Public String format(指定格式)    将localdatetime对象按照指定格式转换成一个字符串对象
		        String s = localDateTime.format(dateTimeFormatter);// 传递一个pattern
		        System.out.println(s);
		        
		        //解析字符串  按照指定的日期格式 解析一个字符串  返回一个LocalDateTIme对象
		        LocalDateTime localDateTime1 = LocalDateTime.parse(s,dateTimeFormatter);
		        System.out.println(localDateTime1);
		        
		    }
		}

  1. 时间增加

需求:增加一整年

		package com.ustc.test9;
		
		import java.time.LocalDateTime;
		
		public class localDateTime2 {
    
    
		
		    public static void main(String[] args) {
    
    
		        
		        //将给定的数据封装成一个日期对象
		        LocalDateTime localDateTime = LocalDateTime.of(2020,11,11,11,11,1);
		        LocalDateTime localDateTime1 = localDateTime.plusYears(1);
		        System.out.println(localDateTime1);
		    }
		}

		pubLic LocalDateTime minusYears(long years)        减少或者添加年
		Public LocalDateTime withYear(int year)     //直接修改年份
		Public LocalDateTime withMonth(int Month)     //直接修改月份    注意传递的参数的正确性
		LocalDateTime newlocalDateTime = localDateTime.withdateYear(2048)

  1. 时间间隔对象
		Public static Period between(开始时间,结束时间)         //计算两个时间的间隔
		Public int getYears()     //获得这段时间的年数
		Public int getMonths()   //获得这段时间的月数
		Public int getDays()     //获得此期间的天数
		Public long toTotalMonths()   获得此期间的总月数
		package com.ustc.test9;
		
		import java.time.LocalDate;
		import java.time.Period;
		
		public class between {
    
    
		
		    public static void main(String[] args) {
    
    
		
		        LocalDate localDate = LocalDate.of(2020,1,1);
		        LocalDate localDate1 = LocalDate.of(2048,12,11);
		        Period period = Period.between(localDate,localDate1);
		        System.out.println(period);
		        //P28Y11M10D  p表示时间间隔对象
		        System.out.println(period.getYears());
		        System.out.println(period.getMonths());
		    }
		
		}

		Duration  :
public static Duration between(开始时间,结束时间)        //计算两个时间的间隔
		Public long toSeconds()                                                             //获得此时间间隔的秒
		Public int toMills()                                                               //获得此间隔时间的毫秒
		
		
		
		package com.ustc.test9;
		
		import java.time.Duration;
		import java.time.LocalDateTime;
		
		public class duration {
    
    
		
		    public static void main(String[] args) {
    
    
		        
		        //封装成一个LocalDateTime对象
		        LocalDateTime localDateTime = LocalDateTime.of(2020,1,1,11,11,11);
		        LocalDateTime localDateTime1 = LocalDateTime.of(2021,1,1,11,11,11);
		        //创建一个时间间隔对象
		        Duration duration = Duration.between(localDateTime,localDateTime1);
		        System.out.println(duration.toMillis());
		    }
		}

JDK8 时间小结:

LocalDate() 表示日期(年月日)
LocalTime() 表示时间(时分秒)
LocalDateTime() 表示时间+日期(年月日时分秒)

LocalDateTime 可以转换成其他两种类型

以上三个类都可以使用以下两种构造方法:

Now 将当前时间封装成对象
Of 将指定的时间构造成对象

将时间 按照具体格式进行格式化 format
将格式化字符串 解析成时间对象 parse
增加或者减少时间的方法 (plus 开头 minus开头)
计算时间间隔的两个类: Period Duration

猜你喜欢

转载自blog.csdn.net/qq_44653420/article/details/121458895