JAVA基础知识点(时间,日期,异常,常用API)

时间日期类

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

时间换算公式:

 1秒=1000毫秒

1毫秒=1000微秒

1微秒=1000纳秒

//public Date()   创建一个Date对象,表示默认时间
//public Date(long date)   创建一个Date对象,表示指定时间
 
//电脑中的当前时间
Date date1=new Date();

//从计算机的时间原点开始,过了指定毫秒的那个时间
Date date2=new Date(OL);//从时间原点开始,过了0毫秒

小结

Date date1=new Date();         把当前时间封装成一个Date对象

Date date2=new Date(OL);     把从时间原点开始,过了指定毫秒的时间,封装成一个Date对象

                                                需要考虑时差问题

//public long getTime() 获取时间对象的毫秒值
//public void setTime(long time) 设置时间,传递毫秒值
Date date1 =new Date();
date1.setTime(0L);
//获取这个date对象的毫秒值---获取当前时间的毫秒值
long time=date1.getTime();
long time2=System.currentTimeMillis();

SimpleDateFormat类概述

SimpleDateFormat可以对Date对象,进行格式化和解析

SimpleDateFormat的构造方法

public SimpleDateFormat() 构造的默认格式

public SimpleDateFormat(String pattern) 构造指定的格式

SimpleDateFormat格式化和解析日期

1.格式化(从Date到String)

public final String format(Date date):将日期格式化成日期/时间字符串

2.解析(从String到Date)

public Date parse(String source):从给定字符串的开始解析文本已生成日期

//当前时间的Date对象
Date date=new Date();
//创建一个日期格式
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
String result=sdf.format(date);
String s="2099-01-01";
SimpleDataFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Data date =sdf.parse(s);
sout(date)

SimpleDateFormat小结

1.格式化(从Date到String)

public final String format(Date date):把时间按照固定格式进行展示

2.解析(从String到Date)

public Date parse(String source):需要对时间进行计算

//把时间换算成毫秒
String time="2024年11月25日 0:10:16";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
long starTime=sdf.parse(time).getTime();

DataTimeFormatter (jdk8)

String s="2099年11月30日 00:00:00";
DataTimeFormatter pattern=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
LocalDateTime  localTime=LocalDateTime.parse(s,pattern);
LocalDateTime  newLocalDateTime=localTime.plushDays(1);
String result=newLocalDateTime.format(pattern);
sout(result);

DataTimeFormatter (jdk7)

LocalDateTime now =LocalDateTime.now();
sout(now);

LocalDateTime localTime=LocalDateTime.of(2099,11,11,11,11);
sout(localTime);

LocalDateTime获取方法

LocalDateTime localDateTime=LocalDateTime.of(2099,11,11,11,11,11);
int year=localDateTime.getYear();

LocalDateTime转换方法

LocalDateTime localDateTime=LocalDateTime.now();
//转换称为一个LocalDate对象
LocalDate localDate=localDateTime.toLocalDate();
sout(localDate);//2024-11-13
LocalTime localTime=localDateTime.toLocalTime();
sout(localTime);//08:01:13

 LocalDateTime格式化和解析

public String format(指定格式)   把一个LocalDateTime格式化为一个字符串

public LocalDateTime parse(准备解析得字符串,解析格式)    把一个日期字符串解析成为一个LocalDateTime对象

JDK8的日期格式化器:DateTimeFormatter

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

LocalDateTime localDateTime=LocalDateTime.of(2099,11,11,11,11,11);
//把一个LocalDateTime格式化为一个字符串
DateTimeFormatter pattern =DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String s=localDateTime.format(pattern);
sout(s);

String s="2099年11月12日 13:14:15";
DateTimeFormatter pattern =DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
LocalDateTime result=LocalDateTime.parse(s,pattern);
sout(result);

 LocalDateTime增加或减少时间的方法

public LocalDateTime plusYears(long years) 添加或者减去年

参数为正,就是往后加

参数为负,就是往前减

LocalDateTime localDateTime=LocalDateTime.of(2099,11,11,11,11,11);
LocalDateTime newTime=LocalDateTime.plusYears(1);

 

JDK8时间类修改时间

LocalDateTime localDateTime=LocalDateTime.of(2099,11,11,11,11,11);

LocalDateTime newLocalDateTime=localDate.withYear(2100);

sout(newLocalDateTime);

Period 

//计算两个时间的间隔
LocalDate localDate1=LocalDate.now();
LocalDate localDate2=LocalDate.now();
Period period=Period.between(localDate1,localDate2);
sout(period);
sout(period.getYears());//获得这段时间的年数
sout(period.getMonths());//获得此期间的月数

Duration 

 

异常

 

异常处理方式
异常处理方式——throws

 throws 异常类名;

注意:这个格式是写在方法的定义处,表示声明一个异常

          编译时异常因为在编译时就会检查,所以必须要写在方法后面进行显示声明

          运行时异常因为在运行时才会发生,所以在方法后面可以不写

method1();//此时调用者也没处理,还是会交给虚拟机处理
//告诉调用者,你调用我,有可能出现这样的异常
//如果方法中没有出现异常,那么正常执行
//如果方法中真的出现了异常,其实也是将这个异常交给了调用者处理
private static void method1() throws NullPointerException
{
  int [] arr=null;
  for(int i=0;i<arr.length;i++)
   {
    sout(arr[i]);
}}
public static void main(String[] args) throws ParseException{
   method2();//还是继续交给调用者处理,而main方法的调用者是虚拟机还是会采取虚拟机默认处理异常的方法
}
//告诉调用者,你调用我,有可能出现这样的异常
//如果方法中没有出现异常,那么正常执行
//如果方法中真的出现了异常,其实也是将这个异常交给了调用者处理
//如果声明的异常时一个编译时异常,那么声明的代码必须要手动写
private static void method2() throws ParseException
{
  SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
  sdf.parse("2099-11-16");
}
 抛出异常对象——throw

  格式:  throw new 异常();

  注意:这个格式是在方法内的,表示当前代码手动抛出一个异常,下面代码不用执行了

sout("好好学习");
throw new RuntimeException();//当代码执行到这里,就创建一个异常对象
                              //该异常创建之后,暂时没有手动处理,抛给了调用者处理
                              //下面的代码不会再执行了
sout("想打游戏");

 throws和throw的区别

 throws:

                   用在方法声明后面,跟的是异常类名

                   表示声明异常,调用该方法有可能会出现这样的异常

异常处理方式

 抛出处理异常的意义

在方法中,当传递的参数有误,没有继续运行下去的意义了,则采取抛出处理,表示让该方法结束运行

告诉调用者方法中出现了问题

public static void main(String[] args)
{
  int [] arr=null;
}
private static void printArr(int [] arr)
{
 if(arr==null)
{
 throw new NullPointerException();
}
else
{
 for(int i=0;i<arr.length;i++)
   sout(arr[i]);
}}

异常处理方式——try...catch 

  格式: try{ 可能出现异常的代码 }

  catch(异常类名 变量名){ 异常的处理代码; }

好处:可以让程序继续往下执行

int []arr=null;
try{//有可能发现异常的代码
printArr(arr);
}catch(NullPointerException e){
//如果出现了这样的异常,那么我们进行的操作
sout("参数不能为null");
}

Throwable的成员方法

try{
int [] arr={1,2,3,4,5};
}catch(ArrayIndexOutOfBoundsException e)
{
  String message=e.getMessage();
  sout(message);
  String s=e.toString();
  sout(s);
  e.printStacktrace();
}
sout("hahah"); //hahah

两种处理异常方式的小结

 抛出 throw,throws

1.在方法中,当传递的参数有误,没有继续运行下去的意义,则采取抛出处理,表示该方法结束运行

2.告诉调用者出现了问题

捕获try....catch

捕获:能让代码继续往下运行

public void setAge(int age)
{ if(age>=18&&age<=25)
  {
   this.age=age;
  }else
  { throw new RuntimeException("年龄超出了范围") }
}

 while(true){
  try{int age=Integer.parseInt(ageStr);
     s.setAge(age);
     break; }
catch(RuntimeException e)
{ sout("请输入一个复合范围的年龄"); 
  continue;
}
catch(NumberFormatException e)
{ sout("请输入一个整数");
  continue;
}
}

自定义异常

步骤:
1.定义异常类  2.写继承关系 3.空参构造 4.带参构造

public class AgeOutOfBoundsException extends RuntimeException
{
  public AgeOutOfBoundsException()
  {
  }
  public AgeOutOfBoundsException(String message){
  super(message);
 }
}

public void setAge(int age)
{ if(age>=18&&age<=25)
  {
   this.age=age;
  }else
  { //throw new RuntimeException("年龄超出了范围") 
    throw new AgeOutOfBoundsException("年龄超出了范围");
  }
}

API:应用程序编程接口

 Math类的常用方法

double ceil=Math.ceil(10.1);
int abs=Math.abs(10);

 System类的常用方法

System.exit(0);//当代码执行到这个方法的时候,表示虚拟机已经停止了
sout(222);
long start=System.currentTimeMillis();//获取当前时间

int [] arr1={1,2,3,4,5};
int [] arr2=new int[10];
//arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数)
System.arraycopy(arr1,0,arr2,0,arr1.length);
//把arr1最后两个元素拷贝到arr2的最后两个索引上
System.arraycopy(arr1,3,arr2,8,2);

Object类的常用方法

Student s=new Student("晓彤",22);
String result=Objects.toString(s);//返回参数中对象的字符串表示形式
Student s=null;
String result=Objects.toString(s,"随便写一个啊");
sout(result);//随便写一个啊
Student s=new Student();
boolean result=Objects.isNull(s);//判断对象是否为空
sout(result);
//  nonNull 判断对象是否不为空

BigDecimal 

 可以用来进行精确计算

BigDecimal  bd1=new BigDecimal("0.1");
BigDecimal  bd2=new BigDecimal("0.2");
BigDecimal add=bd1.add(bd2);//加
BigDecimal subtract=bd1.substract(bd2);//减
BigDecimal multiply=bd1.multiply(bd2);//乘
BigDecimal divide=bd1.divide(bd2);//除

基本类型包装类概述

 

Integer i1=Integer.valueOf(200);
Integer i2=Integer.valueOf("200");
Integer类的使用 
Integer i1=100;
//装箱:把一个基本数据类型 变量对应的包装类
//自动:java底层会帮我们自动的调用valueof方法
//拆箱:把一个包装类型,变成对应的基本数据类型
int i2=i1;

Integer i3=100;//自动装箱机制
i3+=200;//i3=i3+200;
        //会把i3这个对象变成基本数据类型100;
        //100+200=300;
        //把基本数据类型300再次自动装箱变成Integer对象赋值给i3
自动装箱和自动拆箱 

装箱:把基本数据类型转换为对应的包装类类型

拆箱:把包装类类型转换为对应的基本数据类型

Integer i=100;//自动装箱

i+=200;  //i=i+200; i+200 自动拆箱;i=i+200; 自动装箱

注意:在使用包装类类型的时候,如果做操作,最好先判断是否为null

只要是对象,在使用前就必须进行不为null的判断

 int转String 

方式一:+

int i3=100;

String s2=i3+" ";

方式二:可以调用String类中valueof方法

String s3=String.valueOf(i3);

String s="91 92 34 56 67 50";
String[] strArr=s.split(" ");
int [] numberArr=new int[strArr.length];
for(int i=0;i<strArr.length;i++)
{int number=Integer.parseInt(strArr[i]);
 numberArr[i]=number;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值