• Java基础——常用类(Date、File)以及包装类


    本文要点:

    • 基本数据类型的包装类
    • 字符串相关类:
      • 不可变字符序列:String
      • 可变字符序列:StringBuffer、StringBuilder
    • 时间处理相关类:
      • Date
      • DateFormat、SimpleDateFormat
      • Calendar
    • Math类
    • File类
    • 枚举类

    一、基本数据类型的包装类(Wrapper Class)

    • 为什么需要包装类?

      Java并不是纯面向对象的语言。Java语言是一个面向对象的语言,但是Java中的基本数据类型却是不面向对象的。但是我们在实际使用中经常需要将基本数据转化成对象,便于操作。比如:集合的操作中,这时,我们就需要将基本类型数据转化成对象

    • 包装类均位于java.lang包,包装类和基本数据类型的对应关系如下:

    • 包装类的作用:
      • 提供:字符串、基本类型数据、对象之间互相转化的方式
      • 包含每种基本数据类型的相关属性,如最大值、最小值等。
    • 所有的包装类(Wrapper Class)都有类似的方法,掌握一个其他都类似。下面例子就是以Integer为例。

    代码示例:

     1 public class test01 {
     2 
     3     public static void main(String[] args) {
     4 
     5         Integer i=new Integer(1000);
     6         System.out.println(Integer.MAX_VALUE);//最大值
     7         System.out.println(Integer.toHexString(i));//把i变量转成十六进制的字符串
     8         
     9         //把字符串转换成有符号十进制整数
    10         Integer i2=Integer.parseInt("234");//方式1
    11         System.out.println(i2);
    12         System.out.println(i2+10);
    13         Integer i3=new Integer("333");//方式2
    14         System.out.println(i3);
    15         
    16         System.out.println(i2.intValue());//如i2.intValue();intValue可以把当前对象转换成真正的数字
    17         
    18         //整数转字符串
    19         String str=234+"";
    20         
    21     }
    22 
    23 }

    二、自动装箱和自动拆箱(auto-boxing & unboxing)

    • 自动装箱

    基本类型就自动地封装到与它相同类型的包装中,如:

    Integer i=100;

    本质上是,编译器编译时为我们添加了:

    Integer  i=new Integer(100);

    • 自动拆箱

    包装类对象自动转换成基本类型数据,如:

     int i=new Integer(1500);

    代码示例:

     1 /**@Description: 测试自动装箱和自动拆箱    
     2  * @date 2016-2-17 下午3:50:21    
     3  */
     4 public class test02 {
     5 
     6     public static void main(String[] args) {
     7         Integer i=new Integer(1000);//JDK1.5之前
     8         //自动装箱
     9         Integer i1=1000;//JDK1.5之后出现自动装箱:编译器帮我们改进代码Integer i=new Integer(1000);
    10         Integer i2=2000;
    11         
    12         //自动拆箱
    13         int i3=new Integer(1500);//自动拆箱,编译器改进:new Integer(1500).intValue();
    14         int i4=i2;//i2.intValue();
    15         
    16         /*假设Integer i2=null;
    17          * int i4=i2;
    18          * 则程序运行起来会报空指针异常NullPointException*/
    19         
    20         Integer d=1234;
    21         Integer d1=1234;
    22         System.out.println(d==d1);//false:属性值相同,但是两个是不同的对象
    23         System.out.println(d.equals(d1));//true:equals()比较的是两个的值intValue()

    注意下面:

    再者:

     1 public static void main(String[] args) {
     2         Integer a1 = Integer.valueOf(60);  //danielinbiti
     3         Integer b1 = 60;  
     4         System.out.println("1:="+(a1 == b1));   
     5         
     6         Integer a2 = 60;  
     7         Integer b2 = 60;  
     8         System.out.println("2:="+(a2 == b2));  
     9         
    10         Integer a3 = new Integer(60);  
    11         Integer b3 = 60;  
    12         System.out.println("3:="+(a3 == b3));  
    13         
    14         Integer a4 = 129;  
    15         Integer b4 = 129;  
    16         System.out.println("4:="+(a4 == b4));  
    17     }

    这段代码的比较结果,如果没有执行不知道各位心中的答案都是什么。

    要知道这个答案,就涉及到Java缓冲区和堆的问题。

    java中Integer类型对于-128-127之间的数是缓冲区取的,所以用等号比较是一致的。但对于不在这区间的数字是在堆中new出来的。所以地址空间不一样,也就不相等。

    Integer b3=60,这是一个装箱过程也就是Integer b3=Integer.valueOf(60)

    所以,以后碰到Integer比较值是否相等需要用intValue()

    对于Double没有缓冲区。

    1:=true
    2:=true
    3:=false
    4:=false
    View Code

    三、时间处理相关类

    核心类:Java.util.Date

    java.text.DateFormat:日期/时间格式化子类的抽象类

    java.text.SimpleDateFormat是一个以与语言环境相关的方式来格式化和分析日期的具体类。它允许进行格式化(日期 -> 文本)、分析(文本 -> 日期)和规范化。

    java.util.Calendar:日历

    java.util.GregorianCalendar (阳历)是 Calendar 的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。

    1. Date时间类(Java.util.Date

    •  在标准Java类库中包含一个Date类。它的对象表示一个特定的瞬间,精确到毫秒
    • Java中时间的表示,说白了也是数字,是从:标准纪元1970.1.1 0点开始的某个时刻的毫秒数,类型是long.
     1 /**@Description: 测试时间类的用法  
     2  * @date 2016-2-17 下午4:34:41    
     3  */
     4 public class Test01 {
     5 
     6     public static void main(String[] args) {
     7         //Date的构造器
     8         /* 调用的是Date的无参构造函数:
     9          * public Date() {this(System.currentTimeMillis()); }
    10          * 此处代表系统当前时间*/
    11         Date d=new Date();
    12         long t=System.currentTimeMillis();
    13         System.out.println(t);
    14         
    15         Date d2=new Date(1000);
    16         System.out.println(d2.toGMTString());//不建议使用,已经被遗弃了。
    17         
    18         //getTime():获取毫秒数
    19         System.out.println(d2.getTime());//getTime()返回自1970年1月1日 00:00:00 GMT开始到现在所表示的毫秒数
    20 
    21     }
    22 }

    2. DateFormat和SimpleDateFormat

    它们的工作:完成字符串和时间对象的转化!

    是一个抽象类。只有一个子类

    主要有两个方法:

    • parse() 是把字符串转换成时间
    • format()是把时间对象转换成字符串

    代码示例:时间与字符串相互转换

     1 public class TestDateFormat {
     2 
     3     public static void main(String[] args) {
     4         DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss,属于本年的第w周");
     5         Date d=new Date(12342213234L);
     6         String str=df.format(d);//format()是将时间对象按照格式化字符串,转换成字符串
     7         System.out.println(str);
     8         
     9         System.out.println("我是华丽丽的分割线~~~~");
    10         
    11         String str2="1977-7-7 13:14:15";
    12 //        String str2="1977,7,7 13:14:15";//不匹配
    13         DateFormat df2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//此处格式化字符串必须要和上面“1977-7-7 13:14:15”匹配
    14         //如果不匹配,则会报错:Unparseable date: "1977,7,7 13:14:15"不可解析的时间
    15         try {
    16             Date d2=df2.parse(str2);//parse()是把字符串转换成时间
    17             System.out.println(d2);
    18         } catch (ParseException e) {
    19             
    20             e.printStackTrace();
    21         }
    22     }
    23 
    24 }

    3. Calendar日历类和GregorianCalendar公历

    • 人们对于时间的认识是:某年某月某日,这样的日期概念。计算机是long类型的数字。通过Calendar在二者之间搭起桥梁!
    • GregorianCalendar是Calendar的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统。

    • 注意
      • 月份:一月是0,二月是1,以此类推,12月是11
      • 星期:周日是1,周一是2,以此类推,周六是7

    代码示例:

     1 /**@Description:  测试日期类    
     2  * @date 2016-2-17 下午5:25:01    
     3  */
     4 public class TestCalendar {
     5 
     6     public static void main(String[] args) {
     7         Calendar c=new GregorianCalendar();//Calendar类是抽象类
     8 //        c.set(2011,3, 14, 12, 2, 34);
     9         c.set(2011,Calendar.MARCH, 14, 12, 2, 34);
    10 //        c.set(Calendar.YEAR, 2011);
    11         
    12 //        c.setTime(new Date());
    13         
    14         Date d=c.getTime();
    15 //        c.getTimeInMillis();
    16         System.out.println(d);
    17         System.out.println(c.get(Calendar.YEAR));
    18     
    19         /*c.add(Calendar.MONTH, 3);//加3个月
    20         c.add(Calendar.MONTH, -3);//减3个月
    21         System.out.println(c);*/
    22         
    23     }
    24 
    25 }

     【小实例】可视化日历

  • 相关阅读:
    mysql笔记--基础知识
    安全杂乱笔记整理1---常用服务端口总结
    ent orm笔记4---Code Generation
    ent orm笔记3---schema使用(下)
    ent orm笔记2---schema使用(上)
    ent orm笔记1---快速尝鲜
    Linux 更新yum源
    Lua 获取毫秒精度时间
    Linux docker镜像制作
    Linux 中文字符集安装
  • 原文地址:https://www.cnblogs.com/Qian123/p/5194803.html
Copyright © 2020-2023  润新知