fourteen-java Date类型与枚举

1、通过常量获取最大值和最小值

public static void main(String[] args) {
  System.out.println("int的最大值:"+Integer.MAX_VALUE);//int的最大值:2147483647
  System.out.println("int的最小值:"+Integer.MIN_VALUE);//int的最小值:-2147483648
  System.out.println("byte的最大值:"+Byte.MAX_VALUE);//byte的最大值:127
}

2、自动装箱和自动拆箱

public static void main(String[] args) {
      //自动装箱 基本类型-->包装类 i是一个引用 保存的是对象地址
      Integer i=100;
      //自动拆箱 包装类-->基本类型
      long j=i;
      System.out.println(i+1);//i是包装类型 在进行加法运算的时候会自动拆箱 101
      Integer a=1000;
      Integer b=1000;
      System.out.println(a==b);//false a与b保存的是不同的内存地址 ==不会触发自动拆箱机制
  }

3、整数型常量池

public static void main(String[] args) {
  Integer x=128;
  Integer y=128;
  System.out.println(x==y);//false
  /*
  为了代码的便捷,java5以后出现了整数型常量池 范围是-128到127 这些数字一开始就存储于方法区中的整数型常量池
  只要是这些数,都不会出现新的对象
  a与b存储的是127的对象内存地址,故相等
    */
  Integer a=127;
  Integer b=127;
  System.out.println(a.equals(b));//true
}

4、数字格式化异常 NumberFormatException

总结异常:

  • 空指针异常:NullPointerException

  • 数组下标越界异常:ArrayIndexOutOfBoundsException

  • 类型转换异常:ClassCastException

  • 数字格式化异常:NumberFormatException

Integer c=new Integer("中文");//在编译的时候是正常的,在运行时会出现 数字格式化异常NumberFormatException
System.out.println(c);
/*
static int parseInt(String s)
静态方法,返回值是整形,参数是String
*/
int result=Integer.parseInt("123");

5、String,Int,Integer的转换

//int-->String
int num1=12;
String n1=num1+"";//方法1
String num2=String.valueOf(num1);//方法二
//String-->int
String num3="123";
int n2=Integer.parseInt(num3);//方法一
int num4=Integer.valueOf(num3);//方法二
//int-->Integer 自动装箱
Integer num5=23;
//Integer-->int 自动拆箱
int num6=num5;
//Integer-->String
Integer num7=34;
String num8=String.valueOf(num7);
//String-->Integer
Integer num9=new Integer("45");
String num10="56";
Integer num11=Integer.valueOf(num10)

6、Data

要点:

怎样获取当前时间?

String-->Date

Date-->String

public static void main(String[] args) throws Exception {
  /*
  获取系统当前时间精确到毫秒
  直接调用无参构造
    */
  Date nowTime= new Date();
  /*
  java.util.Data已经重写了toString方法
    */
  System.out.println(nowTime);//Tue Jul 06 10:50:07 CST 2021 当前日期时间
  //将日期格式化 在 java.text中SimpleDateFormat 负责格式化
  /*
  yyyy:年 四位
  MM:月 两位
  dd:天
  HH:时
  mm:分
  ss:秒
  SS:毫秒
  除以上字符不可随便写之外 都可以随便写
    */
  //将Date类型-->字符串 格式化输出
  SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SS");
  String nowTimestr=sdf.format(nowTime);
  System.out.println(nowTimestr);//2021-07-06 11-03-59 53
  //将字符串-->Data类型 注意 字符串格式要与对象参数格式一致
  String DataStr="2012-02-03 12:23:45 90";
  Date date=sdf.parse(DataStr);
  System.out.println(date);//Fri Feb 03 12:23:45 CST 2012
}

7、计算方法时长

/*
获取1970年 1月1日 零点 到当前时间的毫秒数
使用静态方法:System.currentTimeMillis()获取
作用:计算一个方法的执行时长
*/
public class Date01 {
  public static void main(String[] args) {
      long nowTimeMillis=System.currentTimeMillis();
      System.out.println(nowTimeMillis);//1625542970078
      long begin=System.currentTimeMillis();//开始计时
      print();
      long end=System.currentTimeMillis();//结束计时
      System.out.println("方法执行时长:"+(end-begin));
  }
  public static void print(){
      for (int i = 0; i < 1000; i++) {
          System.out.println(i);
      }
  }
}

8、使用毫秒来构造Date

Date(long date) 分配一个 Date对象,并将其初始化为表示自称为“时代”的标准基准时间以后的指定毫秒数,即1970年1月1日00:00:00 GMT

public static void main(String[] args) {
  Date date=new Date(1);//通过毫秒构造Date对象 参数是long类型的毫秒数
  SimpleDateFormat adf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
  String nowdate=adf.format(date);
  System.out.println(nowdate);//1970-01-01 08:00:00 001 北京是东八区 差八个小时
  //获取昨天现在的时间
  Date date1=new Date(System.currentTimeMillis()-1000*60*60*24);//从1970年到现在的总毫秒数减去一天的毫秒数
  String yedate=adf.format(date1);
  System.out.println(yedate);//2021-07-05 14:30:16 292

}

9、数字格式化

使用java.text.DecimalFormat

#:代表任意数字

, :代表千分位

. :代表小数

0 :不够补零

public static void main(String[] args) {
  DecimalFormat nums=new DecimalFormat("###,###.##");
  String num=nums.format(12345.123);
  System.out.println(num);//12,345.12
  DecimalFormat nums1=new DecimalFormat(",###.000");
  String num1=nums1.format(123456.23);
  System.out.println(num1);//12,3456.230
}

10、BigDecimal

import java.math.BigDecimal;
/*
BigDecimal 属于大数据,精度极高。不属于基本数据类型,属于引用数据类型
专门用在财务软件当中
在财务软件当中double是不够用的,要使用 java.math.BigDecimal
*/
public class BigDecimalTest {
  public static void main(String[] args) {
      //这是个精度极高的数字
      BigDecimal b1=new BigDecimal(12);
      BigDecimal b2=new BigDecimal(4);
      //b1+b2 这样的写法是错误的 因为它们是引用类型的对象不能直接运算
      //调用方法进行运算
      BigDecimal b=b1.add(b2);
      System.out.println(b);//16

  }
}

11、枚举

枚举是一种引用数据类型

枚举的定义:

enum 枚举类型名称{ 枚举值1,枚举值2... }

若结果只有两种情况,建议使用布尔类型

若结果有三个及三个以上,建议使用枚举类型

例如 颜色,四季,星期等都可以使用枚举类型

public class EnumTest01 {
  public static void main(String[] args) {
    Result r=print(3,7);
      System.out.println(r);//no
  }
  public static Result print(int a,int b){
      if(a>b)return Result.YES;
      else return Result.NO;
  }

}
//枚举的定义
enum Result{
  YES,NO
}

 

posted @ 2021-07-06 18:14  别不开心,过得去  阅读(194)  评论(0)    收藏  举报