java基础之包装类、日期类

159 阅读5分钟

「这是我参与2022首次更文挑战的第15天,活动详情查看:2022首次更文挑战

一 基本类型包装类

  • 将基本数据类型封装成对象的好处在于在对象中定义更多的功能方法操作该数据
  • 常用的操作之一:用于基本数据类型与字符串之间的转换
基本数据类型包装类
byteByte
shortShort
intInteger(重点)
longLong
floatFloat
doubleDouble
charCharacter(特殊)
booleanBoolean

1.1 Integer类概述和使用

  • Integer:包装一个对象中原始类型int的值
  • 常用方法
方法名说明
public static Integer vlaueOf(int i)返回表示指定的int值的 Integer 实例
public static Integer vlaueOf(String s)返回一个保存指定的 Integer 对象 String
public class IntegerDemo {
    public static void main(String[] args) {
        //1、public static Integer vlaueOf(int i) 	返回表示指定的int值的 Integer 实例
        Integer i1 = new Integer(100);
        System.out.println(i1); //100

        //2、public static Integer vlaueOf(String s) 	返回一个保存指定的 Integer 对象 String
        Integer i2 = new Integer("100");
//        Integer i2 = new Integer("abc"); 报错,NumberFormatException
        System.out.println(i2); //100
    }
}

1.2 int 和 String 的相互转换

1、int 转换为 String 类型 public static String valueOf(int i):返回 int 参数的字符串表示形式。该方法是 String 类中的方法 2、String 转换为 int 类型 public static int parselnt(String s):将字符串解析为int类型。该方法是 Integer 类中的方法

package ceshi;

public class IntegerDemo {
    public static void main(String[] args) {
        //int --- String
        int number = 10;
        //1、public static String valueOf (int i)
        String s = String.valueOf(number);
        System.out.println(s); //10

        //String --- int
        String s1 = "100";
        //2、public static int parseInt(String s)
        int i = Integer.parseInt(s1);
        System.out.println(i); //100
    }
}

1.3 自动装箱和拆箱

  • 自动装箱: 可以直接把基本数据类型的值或变量赋值给包装类
  • 自动拆箱: 可以把包装类的变量直接赋值给基本数据类型
public class PackageClass {
    public static void main(String[] args) {
    	//基本数据类型的值或变量赋值给包装类
    	Integer i = Integer.valueOf(100); //手动调方法装箱
        Integer ii = 100; //默认调用Integer.valueOf(100);
		//拆箱
		int i1 = ii; //自动拆箱,100
		ii +=200; //
		System.out.println(ii) //300
		
		//开发中如果是引用类型变量最好先做不为null判断
		Integer iii = null;
		if(iii != null) {
			iii += 300; //NullPointerException
		}
		      
    }
}
  • 注意:在使用包装类的时候,如果是操作最好先判断是否为null;推荐只要是对象,再使用前必须进行不为null判断

二 日期类

2.1 Date类

  • 导包java.util.Date
  • 构造方法
方法名说明
public Date()创建当前系统的此刻日期时间对象
public Date(long time)把时间毫秒值转换成日期对象
package ceshi;

import java.util.Date;

public class DateDemo {
    public static void main(String[] args) {
        //1、public Date()
        Date d1 = new Date();
        System.out.println(d1); //Tue Jul 06 22:36:15 CST 2021

        //2、public Date(long time)
        long date = 60;
        Date d2 = new Date(date);
        System.out.println(d2); //Thu Jan 01 08:00:00 CST 1970
    }
}
  • 常用方法
方法名说明
public long getTime()获取日期对象从1970年1月1日00:00:00 到现在的毫秒值
public void setTime(long time)设置时间,给的是毫秒值
package ceshi;

import java.util.Date;

public class DateDemo {
    public static void main(String[] args) {
        //1、public long getTime() 	获取日期对象从1970年1月1日00:00:00 到现在的毫秒值
        Date d = new Date();
        System.out.println(d.getTime()); //1625582796041

        //2、public void setTime(long time) 	设置时间,给的是毫秒值
        long time = 1000*60*60;
        d.setTime(time);
        System.out.println(d); //Thu Jan 01 09:00:00 CST 1970
    }
}

2.2 SimpleDateFormat类 [ˈsɪmpl]

  • 可以对日期格式化和解析
  • 日期和时间格式由日期和时间模式字符串指定,在日期和时间模式字符串中,从 'A' 到 'Z' 以及从 'a' 到'z' 引号的字母被解释为表示日期或时间字符串的组件的模式字母
  • 常用的模式字母对应: y---年;M---月;d---日;H---时;m---分;s---秒;E---星期几;a---上午 / 下午
  • 构造方法
方法名说明
public SimpleDateFormat()使用默认模式和日期模式
public SimpleDateFormat(String pattern)指定时间的格式创建简单日期格式化对象
  • 格式化和解析日期方法
方法名说明
public String format(Date date)将日期格式化为日期 / 时间字符串
public String format(Object time)将时间毫秒格式化为日期 / 时间字符串
public Date parse(String source) [pɑːz]从给定的字符串开始解析文本生成日期
package ceshi;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleDateFormatDemo {
    public static void main(String[] args) throws ParseException {
        //格式化 Date》String
         Date d = new Date();
//1、        SimpleDateFormat sdf = new SimpleDateFormat(); //无参构造
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E a"); //2、带参构造
        String s = sdf.format(d);
        System.out.println(s); //1、21-7-11 上午10:13  2、2021年07月11日 10:16:00 星期日 上午

        //解析 String > date
        String s1 = "2020年1月1日 8:25:12";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date d1 = sdf1.parse(s1); //parse报错了,选中按下Alt+enter,默认选第一个
        System.out.println(d1); //Wed Jan 01 08:25:12 CST 2020
    }
}

2.3 Calendar日历类

  • Calendar 代表了系统此刻日期对应的日历对象
  • Calendar 是一个抽象类,不能直接创建对象
  • 常用方法
方法名说明
public static Calendar getInstance()返回一个日历类的对象
public int get(int field)返回给定日历的值
public void set(int field,int value)修改日历的某个字段信息
public abstract void add(int field,int amount)根据日历的规则,将指定的时间量添加或减去给定的日历字段
public final void set(int year,int month,int date)设置当前日历年月日