java进阶六(时间类和异常的处理)

JavaSE进阶第六天

课程大纲:

  • 时间日期类、
    • JDK8以前
    • JDK8以后
  • 异常

一.JDK8以前的时间日期类

1.Date类

1.1注意事项

我们是一定是java.util包下的Date
    不要导错包,因为JDK提供一个同名类但是是位于java.sql包下
    
表示时间,可以表示过去、现在和将来,可以表示任意时间,时间非常精确,精确到毫秒值。
    
中国的标准时间:世界标准时间+8小时
    
1=1000毫秒
    
计算机中的时间原点:19701100:00:00

1.2构造方法

Date() :表示当前时间(现在)
Date(long date) :表示任意时间,这个时间是基准时间+传递的毫秒值,落到哪个时间就是哪个时间

//      创建一个Date对象,表示默认时间,把当前时间封装成一个Date对象
Date date1 = new Date();
System.out.println(date1);

//      创建一个Date对象,表示指定时间,把时间原点开始,过了指定毫秒的时间,封装成一个Date对象需要考虑时差问题
Date date2 = new Date(0L);
System.out.println(date2);

Date date3 = new Date(1000L * 60 * 60 *24 * 365 *60);
System.out.println(date3);

image-20210912091020393

1.3成员方法

​ void setTime(long time):设置毫秒值
​ long getTime():获取date对象所表示的毫秒值
​ boolean after(Date when) 测试此日期是否在指定日期之后。
​ boolean before(Date when) 测试此日期是否在指定日期之前。

Date date = new Date();
//        获取这个date对象的毫秒值---获取当前时间的毫秒值
long time = date.getTime();
System.out.println(time);

System.out.println("---------------");

long time02 = System.currentTimeMillis();
System.out.println(time02);

image-20210912091525675

//        设置时间,传递毫秒值
date.setTime(0L);
System.out.println(date);
System.out.println(date.getTime());

image-20210912091702043

 Date d1 = new Date(1000L*60*60);
 Date d2 = new Date(0L);
 Date d3 = new Date();
 //判断d1的时间是否在d2之后  ---- after
 boolean after = d1.after(d2);
 System.out.println(after);
 //判断d3的时间是否在d2之前  ---- before
 boolean before = d3.before(d2);
 System.out.println(before);
 
 
   //自己比较
   boolean b1 = d1.before(d1);
   System.out.println(b1);     //false
   boolean b2 = d1.after(d1);
   System.out.println(b2);     //false

image-20210912091854681

2. SimpleDateFormat类

2.1构造方法

SimpleDateFormat() :使用空参构造创建对象,格式化和解析的时候使用的是默认模式
SimpleDateFormat(String pattern) :使用有参构造创建对象,格式化和解析的时候使用的是指定模式
2021-8-18 15:03:50 yyyy-MM-dd HH:mm:ss
2021/8/18 15:03:50 yyyy/MM/dd HH:mm:ss
2021年8月18日 15时03分50秒 yyyy年MM月dd日 HH时mm分ss秒

//        格式化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String format = sdf.format(now);
System.out.println(format);

image-20210912092231971

//        解析
String dateStr = "2000/12/12";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date parse = sdf.parse(dateStr);
System.out.println(parse);
System.out.println(parse.getTime());

image-20210912092307649

3.Calendar类(日历类)

日历类,可以操作日期时间中的单独的每一段(年、月、日、时、分、秒、星期)

3.1构造方法

获取对象
         static Calender getInstance()

3.2成员方法

3.2.1设置
void set(int field, int value)
    将给定的日历字段设置为给定值。
void set(int year, int month, int date)
    设置日历字段 YEAR、MONTH 和 DAY_OF_MONTH 的值。
void set(int year, int month, int date, int hourOfDay, int minute)
    设置日历字段 YEAR、MONTH、DAY_OF_MONTH、HOUR_OF_DAY 和 MINUTE 的值。
void set(int year, int month, int date, int hourOfDay, int minute, int second)
    设置字段 YEAR、MONTH、DAY_OF_MONTH、HOUR、MINUTE 和 SECOND 的值。

Calendar c = Calendar.getInstance();
c.set(2000,11,11,11,11,11);
System.out.println(c.getTime());

image-20210912093634096

3.2.2获取
int get(int field)
int field:日期时间字段,标记
Date date = new Date();
c.setTime(date);
Date time = c.getTime();
System.out.println(time);
int year = c.get(Calendar.YEAR);


System.out.println("年份是:"+year);
System.out.println("--------------");
int month = c.get(Calendar.MONTH)+1;
System.out.println("月份是:"+month);
System.out.println("--------------");
int day = c.get(Calendar.DATE);
System.out.println("天数是:"+day);
System.out.println("--------------");
//        int hour = c.get(Calendar.HOUR);    //0-12
int hour = c.get(Calendar.HOUR_OF_DAY);
System.out.println("小时是:"+hour);
System.out.println("--------------");
int minute = c.get(Calendar.MINUTE);
System.out.println("分钟是:"+minute);
System.out.println("--------------");
int second = c.get(Calendar.SECOND);
System.out.println("秒钟是:"+second);

int week = c.get(Calendar.DAY_OF_WEEK)+1;
System.out.println("第几个周末:"+week);

image-20210912094357345

3.2.3 增减
 void add(int field, int amount)
c.set(2000,11,11,11,11,11);
System.out.println(c.getTime());

c.add(Calendar.YEAR,1);
System.out.println(c.getTime());

image-20210912094608545

3.2.4 转换
	Date getTime():Calendar转换为Date,得到的Date对象所表示的时间与Calendar对象的时间一致
    void setTime(Date date):将Date转换为CalendarCalendar对象所表示的时间与Date对象的时间一致
Date date = new Date();
c.setTime(date);
System.out.println(c);
Date time = c.getTime();

image-20210912094918415

3.3注意事项

如果一个类是抽象类,要使用其中的方法的两种方式:
    1、创建子类对象调用方法
    2、直接使用抽象类,但是抽象类不能创建对象,这时如果抽象类中有静态方法,就可以直接调用
    或者有一些静态方法可以直接返回子类对象,也可以继续调用抽象类中的非静态方法

二. JDK8以后的时间日期类

时间日期对象:

LocalDateTime 封装了年月日时分秒完整时间
LocalDate 封装了年月日,没有时分秒
LocalTime 封装了时分秒,没有年月日

这三个类的功能基本相似,以LocalDateTime为代表进行学习

1.LocalDateTime类

1.1获取对象

LocalDateTime虽然是个类但不是抽象类,它却也不能创建对象,因为该类的唯一的构造方法是private修饰的
    
static LocalDateTime now() :当前系统时间
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)
	 这两个方法都是静态的,直接使用类名.进行调用
LocalDateTime now = LocalDateTime.now();
LocalDate nowDate = LocalDate.now();
LocalTime nowTime = LocalTime.now();
System.out.println(now);
System.out.println(nowDate);
System.out.println(nowTime);

image-20210912100108194

LocalDateTime time = LocalDateTime.of(2020, 11, 11, 11, 34, 30);
System.out.println(time);

image-20210912100159781

1.2获取日期时间的每一段的方法

public int getYear()        获取年
public int getMonthValue()  获取月份(1-12public int getDayOfMonth()  获取月份中的第几天(1-31public int getDayOfYear()   获取一年中的第几天(1-366public DayOfWeek getDayOfWeek() 获取星期
public int getMinute()      获取分钟
public int getHour()        获取小时
LocalDateTime time = LocalDateTime.now();
int year = time.getYear();
System.out.println("年为"+year);
int month = time.getMonthValue();
System.out.println("月份为"+month);
int dayOfMonth = time.getDayOfMonth();
System.out.println("日期为"+dayOfMonth);
int dayOfYear = time.getDayOfYear();
System.out.println("这是一年中的第"+dayOfYear+"天");
int dayOfWeek = time.getDayOfWeek().getValue();
System.out.println("星期为"+dayOfWeek);
int hour = time.getHour();
//        获取小时
int minute = time.getMinute();
//        获取分钟
int second = time.getSecond();
//        获取秒钟
System.out.println(hour+"-"+minute+"-"+second);

image-20210912100414902

1.3增减方法

	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 plusSeconds(long seconds) 添加或者减去秒
    public LocalDateTime plusWeeks(long weeks) 添加或者减去周

    public LocalDateTime minusYears (long years) 减去或者添加年
    public LocalDateTime minusMonths(long months) 减去或者添加月
    public LocalDateTime minusDays(long days) 减去或者添加日
    public LocalDateTime minusHours(long hours) 减去或者添加时
    public LocalDateTime minusMinutes(long minutes) 减去或者添加分
    public LocalDateTime minusSeconds(long seconds) 减去或者添加秒
    public LocalDateTime minusWeeks(long weeks) 减去或者添加周
        
    public LocalDateTime withYear(int year) 直接修改年
    public LocalDateTime withMonth(int month) 直接修改月
    public LocalDateTime withDayOfMonth(int dayofmonth) 直接修改日期(一个月中的第几天)
    public LocalDateTime withDayOfYear(int dayOfYear) 直接修改日期(一年中的第几天)
    public LocalDateTime withHour(int hour) 直接修改小时
    public LocalDateTime withMinute(int minute) 直接修改分钟
    public LocalDateTime withSecond(int second) 直接修改秒

LocalDateTime localDatetime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
LocalDateTime newLacalDatetime = localDatetime.plusDays(1); //增加1天时间    -1表示减少1天时间
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String format = newLacalDatetime.format(pattern);
System.out.println(localDatetime.format(pattern));
System.out.println("修改后:");
System.out.println(format);

image-20210912101043047

LocalDateTime newlocaldatetime = localDatetime.minusDays(1);    //减少1天的时间  -1表示增加1天时间
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
System.out.println(localDatetime.format(pattern));
System.out.println("修改后");
System.out.println(newlocaldatetime.format(pattern));

image-20210912101140126

LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 12, 12, 12);
LocalDateTime newlocaldatetime = localDateTime.withYear(2048);
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String s = pattern.format(newlocaldatetime);
String s1 = pattern.format(localDateTime);
System.out.println(s1);
System.out.println("修改后:");
System.out.println(s);

image-20210912101638258

小结:

	一个方法搞定所有时间段的的操作
    int get​(TemporalField field) :获取
    LocalDateTime minus​(long amountToSubtract, TemporalUnit unit):增减
    LocalDateTime plus​(long amountToAdd, TemporalUnit unit) :增减
    LocalDateTime with(TemporalField field, long newValue) :修改

1.4转换方法

public LocalDate toLocalDate () 转换成为一个LocalDate对象
public LocalTime toLocalTime () 转换成为一个LocalTime对象
public  static LocalDateTime LocalDateTime.of(date1, time1) 拼接
LocalDateTime time = LocalDateTime.now();
LocalDate localDate = time.toLocalDate();
LocalTime localTime = time.toLocalTime();
System.out.println(localDate);
System.out.println(localTime);

image-20210912102028938

LocalDate date1 = LocalDate.now();
LocalTime time1 = LocalTime.of(12, 12, 12);
LocalDateTime localDateTime = LocalDateTime.of(date1, time1);
System.out.println(localDateTime);

image-20210912102103932

1.5格式化和解析

public String format (模式) 把一个LocalDateTime格式化成为一个字符串
public static LocalDateTime parse​(准备解析的字符串, 模式) 把一个日期字符串解析成为一个LocalDateTime对象

DateTimeFormatter类中的方法
static DateTimeFormatter ofPattern​(String pattern)
这个方法专门用于指定解析和格式化使用的模式
解析(parse):

String s = "2020年12月12日 13:14:15";
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");  //不确定月份写一个M
LocalDateTime localDateTime = LocalDateTime.parse(s, pattern);   //parse解析---->字符串变为LocalDateTime类型
System.out.println(localDateTime);

image-20210912102430933

格式化(format):
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
System.out.println("格式化后:");
//        public String format(指定格式) 把一个LocalDateTime格式转换成一个字符串
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String s = pattern.format(now);
System.out.println(s);
System.out.println("-------------");
String s1 = now.format(pattern);
System.out.println(s1);

image-20210912103346349

2.Period类

构造方法:
static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
    
成员方法:
public int getYears() 获得这段时间的年数
public int getMonths() 获得此期间的总月数
public int getDays() 获得此期间的天数
public long toTotalMonths() 获取此期间的总月数
LocalDate localDate01 = LocalDate.of(2020, 1, 1);
LocalDate localDate02 = LocalDate.of(2048, 12, 12);
Period period = Period.between(localDate01, localDate02);
//        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
System.out.println(period);
System.out.println("相差的年份:"+period.getYears());
System.out.println("相差的月份:"+period.getMonths());
System.out.println("相差的天数:"+period.getDays());
System.out.println("此间的总月数:"+period.toTotalMonths());
System.out.println("间隔的总天数:"+ChronoUnit.DAYS.between(localDate01, localDate02));
 System.out.println("间隔"+ChronoUnit.YEARS.between(localDate01, localDate02)+"年");

image-20210912104247837

3.Duration类

构造方法:
static Duration between​(Temporal startInclusive, Temporal endExclusive)
成员方法:
	long toDays​()
    long toHours​()
    public long toSeconds() 获得此时间间隔的秒
    public int toMillis() 获得此时间间隔的毫秒
    public int toNanos() 获得此时间间隔的纳秒
//      public static Duration between(开始时间,结束时间)  计算两个“时间”的间隔
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 1, 1, 13, 14, 15);
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 1, 2, 11, 12, 13);
Duration duration = Duration.between(localDateTime1, localDateTime2);
//        public long toSeconds()  获得此间间隔的秒
long seconds = duration.toSeconds();
System.out.println("间隔"+seconds+"秒");
//        public int toMillis()  获得此时间间隔的毫秒
long millis = duration.toMillis();
System.out.println("间隔"+millis+"毫秒");
//        public int  toNanos() 获得此时间间隔的纳秒
long nanos = duration.toNanos();
System.out.println("间隔"+nanos+"纳秒");

eTime localDateTime1 = LocalDateTime.of(2020, 1, 1, 13, 14, 15);
LocalDateTime localDateTime2 = LocalDateTime.of(2020, 1, 2, 11, 12, 13);
Duration duration = Duration.between(localDateTime1, localDateTime2);
// public long toSeconds() 获得此间间隔的秒
long seconds = duration.toSeconds();
System.out.println(“间隔”+seconds+“秒”);
// public int toMillis() 获得此时间间隔的毫秒
long millis = duration.toMillis();
System.out.println(“间隔”+millis+“毫秒”);
// public int toNanos() 获得此时间间隔的纳秒
long nanos = duration.toNanos();
System.out.println(“间隔”+nanos+“纳秒”);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值