目录
看到旁边的同学在写图书馆管理系统时,用到了包装类以及日期Api的一些知识,当时他有点忘记了知识点,又重新把相关视频看了一遍,所以我就想来简单的总结一下,也便于我后面自己用到。
一、基本类型包装类
所谓包装类,就是能够直接将简单类型的变量表示为一个类,在执行变量类型的相互转换时,我们会大量使用这些包装类。
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
注意:从表中我么可以看出来,基本数据类型中只有int、char他们的基本类型和包装类是不一样的,记忆时要注意。
1. 1 Integer类
1.1.1概述
在Java中,可能会使用到
int
类型的数据,但可能会有所要求:比如只能使用引用数据类型,但是由于int
类型是基本数据类型,无法直接使用,所以需要进行包装,这就引入了Integer
类,其他基本数据类型的包装类也是这样
1.1.2 nteger类构造方法
方法名 说明 public Integer(int value) 根据 int 值创建 Integer 对象(过时) public Integer(String s) 根据 String 值创建 Integer 对象(过时) public static Integer valueOf(int i) 返回表示指定的 int 值的 Integer 实例 public static Integer valueOf(String s) 返回一个保存指定值的 Integer 对象 String
通过代码理解:
public class IntegerDemo {
public static void main(String[] args) {
//public Integer(int value):根据 int 值创建 Integer 对象(过时)
Integer i1 = new Integer(100);
System.out.println(i1);
//public Integer(String s):根据 String 值创建 Integer 对象(过时)
Integer i2 = new Integer("100");
// Integer i2 = new Integer("abc"); //NumberFormatException[不是数字字符串无法解析]
System.out.println(i2);
System.out.println("--------");
//public static Integer valueOf(int i):返回表示指定的 int 值的 Integer 实例
Integer i3 = Integer.valueOf(100);
System.out.println(i3);
//public static Integer valueOf(String s):返回一个保存指定值的Integer对象 String
Integer i4 = Integer.valueOf("100");
System.out.println(i4);
}
}
注意:1.前两种方法已经过时 ,被后面两种方法取代,但是也可以使用。
2.NumberFormatException:不是数字字符串无法解析,所以在使用Integer构造方法时要注意 Integer i2 = new Integer("abc"),这种写法是错误的,应该写数字字符。
3.这里只介绍这一种,其他包装类差不多,可以自己去了解一下。
1.2int和String类型的相互转换
基本类型包装类的最常见操作就是:用于基本类型和字符串之间的相互转换
1.2.1int转换为String
转换方式
方式一:直接在数字后加一个空字符串
方式二:通过String类静态方法valueOf()
代码理解:
public class IntegerDemo {
public static void main(String[] args) {
//int --- String
int number = 100;
//方式1
String s1 = number + "";
System.out.println(s1);
//方式2
//public static String valueOf(int i)
String s2 = String.valueOf(number);
System.out.println(s2);
System.out.println("--------");
}
}
1.2.2String转换为int
转换方式
方式一:先将字符串数字转成Integer,再调用valueOf()方法
方式二:通过Integer静态方法parseInt()进行转换
代码理解:
public class IntegerDemo {
public static void main(String[] args) {
//String --- int
String s = "100";
//方式1:String --- Integer --- int
Integer i = Integer.valueOf(s);
//public int intValue()
int x = i.intValue();
System.out.println(x);
//方式2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println(y);
}
}
注意:1.String——int不是直接转换的,而是通过Integer这个桥梁。public int intValue()方法是返回Integer的值作为int。
1.3自动拆箱和自动装箱
自动装箱
把基本数据类型转换为对应的包装类类型
自动拆箱
把包装类类型转换为对应的基本数据类型
示例代码
Integer i = 100; // 自动装箱
i += 200; // i = i + 200; i + 200 自动拆箱;i = i + 200; 是自动装箱
代码理解 :
public class IntegerDemo {
public static void main(String[] args) {
//装箱:把基本数据类型转换为对应的包装类类型
Integer i = Integer.valueOf(100);
Integer ii = 100; //Integer. value0f(100);
//拆箱:把包装类类型转换为对应的基本数据类型
//ii+=200;
ii = ii.intValue() + 200;
ii += 200;
System.out.println(ii);
Integer iii = null;
if (iii != null) {
iii += 300; //NullPointerException
}
}
}
二、日期APi
2.1Date类
Date类概述
Date 代表了一个特定的时间,精确到毫
Date类构造方法:
方法名 | 说明 |
---|---|
public Date() | 分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒 |
public Date(long date) | 分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数 |
代码理解:
public class DateDemo01 {
public static void main(String[] args) {
//public Date():分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
Date d1 = new Date();
System.out.println(d1);
//public Date(long date):分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
long date = 1000*60*60;
Date d2 = new Date(date);
System.out.println(d2);
}
}
结果图:
2.1.1Date类常用方法
常用方法:
方法名 | 说明 |
---|---|
public long getTime() | 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值 |
public void setTime(long time) | 设置时间,给的是毫秒值 |
2.2SimpleDateFormat类
2.2.1SimpleDateFormat类概述
2.2.2SimpleDateFormat类构造方法
方法名 | 说明 |
---|---|
public SimpleDateFormat() | 构造一个SimpleDateFormat,使用默认模式和日期格式 |
public SimpleDateFormat(String pattern) | 构造一个SimpleDateFormat使用给定的模式和默认的日期格式 |
2.2.3 SimpleDateFormat类的常用方法
格式化(从Date到String)
public final String format(Date date):将日期格式化成日期/时间字符串
解析(从String到Date)
public Date parse(String source):从给定字符串的开始解析文本以生成日期
代码理解:
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
//格式化:从 Date 到 String
Date d = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);
System.out.println("--------");
//从 String 到 Date
String ss = "2048-08-09 11:11:11";
//ParseException
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(ss);
System.out.println(dd);
}
}
三、Calendar类
3.1Calendar类概述
Calendar 为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
Calendar 提供了一个类方法 getInstance 用于获取这种类型的一般有用的对象。
该方法返回一个Calendar 对象。
其日历字段已使用当前日期和时间初始化:Calendar rightNow = Calendar.getInstance();
3.2Calendar类常用方法
方法名 | 说明 |
---|---|
public int get(int field) | 返回给定日历字段的值 |
public abstract void add(int field, int amount) | 根据日历的规则,将指定的时间量添加或减去给定的日历字段 |
public final void set(int year,int month,int date) | 设置当前日历的年月日 |
代码理解:
public class CalendarDemo {
public static void main(String[] args) {
//获取日历类对象
Calendar c = Calendar.getInstance();
//public int get(int field):返回给定日历字段的值
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;//月份从0开始,所以要加1!!!
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
//public abstract void add(int field, int amount):根据日历的规则,将指定的时间量添加或减去给定的日历字段
//需求1:3年前的今天
// c.add(Calendar.YEAR,-3);
// year = c.get(Calendar.YEAR);
// month = c.get(Calendar.MONTH) + 1;
// date = c.get(Calendar.DATE);
// System.out.println(year + "年" + month + "月" + date + "日");
//需求2:10年后的10天前
// c.add(Calendar.YEAR,10);
// c.add(Calendar.DATE,-10);
// year = c.get(Calendar.YEAR);
// month = c.get(Calendar.MONTH) + 1;
// date = c.get(Calendar.DATE);
// System.out.println(year + "年" + month + "月" + date + "日");
//public final void set(int year,int month,int date):设置当前日历的年月日
c.set(2050,10,10);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH) + 1;
date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
}
}