文章目录
概述
基本类型包装类的作用:
将基本数据类型封装成对象的好处在对象中定义更多的功能方法操作该数据
常用的操作之一:用于基本数据类型与字符串之间的转换
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Integer类
概述
包装一个对象中的原始类型int的值
构造方法
方法名 | 描述 |
---|---|
public static Integer valueOf(int i) | 返回表示指定的 int 值的 Integer 实例 |
public static Integer valueOf(String s) | 返回一个保存指定值的 Integer 对象 String |
实例:
public class Demo {
public static void main(String[] args) {
Integer s= Integer.valueOf(30);
System.out.println(s);
Integer s1=Integer.valueOf("20");
System.out.println(s1);
}
}
int和String之间的相互转换
int转换为String
转换方式
方式一:直在数字后加一个空字符串
public class Demo {
public static void main(String[] args) {
int s=30;
String s1=s+"";
System.out.println(s1);
}
}
方式二:通过String类静态方法valueOf()
public class Demo {
public static void main(String[] args) {
int s=30;
String s1= String.valueOf(s);
System.out.println(s1);
}
}
String转换为int
转换方式
方式一:先将字符串数字转换成Integer,再调用valueOf()方法
public class Demo {
public static void main(String[] args) {
String s="30";
Integer s1=Integer.valueOf(s);
System.out.println(s1);
}
}
方式二:通过Integer静态方法parselnt()进行转换
public class Demo {
public static void main(String[] args) {
String s="30";
int s1=Integer.parseInt(s);
System.out.println(s1);
}
}
自动装箱和拆箱
自动装箱----------把基本数据类型转换为对应的包装类类型
自动拆箱----------把包装类类型转换为对应的基本数据类型
实例:
public class Demo{
public static void main(String[] args) {
//装箱:把基本数据类型转换为对应的包装类类型
//100是基本类型通过Integer调valueOf方法,就转换成了对应的包装类类型(i)
Integer i = Integer.valueOf(100);//装箱: Integer.valueOf(100);
Integer ii=100;//自动装箱
//拆箱:把包装类类型转换为对应的基本数据类型
//ii+=200;
//拆箱:ii.intValue()
// ii= ii.intValue()+200;//自动装箱
ii+=200;//内部隐含了自动拆箱和自动装箱
System.out.println(ii);
}
}
日期类
Date类
Date代表了一个特定的时间,精确到毫秒
构造方法
方法名 | 描述 |
---|---|
public Date() | 分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒 |
public Date(long date) | 分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数 |
实例:
public class Demo {
public static void main(String[] args) {
Date time=new Date();//分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒
System.out.println(time);
long date=1000*60*60;//分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数
Date time1=new Date(date);
System.out.println(time1);
}
}
运行结果:
Thu Sep 23 19:49:35 CST 2021
Thu Jan 01 09:00:00 CST 1970
常用方法:
方法名 | 描述 |
---|---|
public long getTime() | 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值 |
public void setTime(long time) | 设置时间,给的是毫秒值 |
实例:
import java.util.Date;
public class Demo {
public static void main(String[] args) {
Date time=new Date();
System.out.println("1970年1月1日到现在的毫秒值为:"+time.getTime());
//获得的是自1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为long。
long time1=System.currentTimeMillis();//设置时间,给的是毫秒值
time.setTime(time1);
System.out.println(time);
}
}
运行结果:
1970年1月1日到现在的毫秒值为:1632398241803
Thu Sep 23 19:57:21 CST 2021
SimpleDateFormat类
概述
SimpleDateFormat类是一个具体的类,用于以区域设置敏感的方式格式化和解析日期
构造方法
方法名 | 描述 |
---|---|
public SimpleDateFormat | 构造一个SimpleDateFormat,使用默认模式和日期格式 |
public SimpleDateFormat(String pattern) | 构造一个SimpleDateFormat使用给定的模式和默认的日期格式 |
常用方法
格式化(从Date到String)
public final String format(Date date):将日期格式化成日期/时间字符串
实例:
public class Demo {
public static void main(String[] args) {
Date time=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s=sdf.format(time);
System.out.println(s);
}
}
运行结果:
2021-09-23 20:03:54
解析(从String到Date)
public Date parse(String source):从给定字符串的开始解析文本以生成日期
实例:
public class Demo {
public static void main(String[] args) throws ParseException {
String time="2021-9-23 08:05:44";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(time);
System.out.println(date);
}
}
运行结果:
Thu Sep 23 08:05:44 CST 2021
Calendar类
概述
Calendar为特定瞬间与一组日历字段之间的转换提供了一些方法,并为操作日历字段提供了一些方法
Calendar提供了一个类方法 getInstance 用于获取这种类型的一般有用的对象。该方法返回一个Calendar 对象。
其日历字段已使用当前日期和时间初始化:Calendar rightNow =Calendar.getInstance();
Calendar类是一个抽象类不能实例化,但是可以通过它的子类实例化;可以在其某一个特定的时刻和日历字段之间提供转换方法
常用方法
方法名 | 描述 |
---|---|
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 Demo {
public static void main(String[] args) throws ParseException {
//获取日历类对象
Calendar c = Calendar.getInstance();
//public int get(int field):返回给定日历字段的值
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1;
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + month + "月" + date + "日");
//根据日历的规则,将指定的时间量添加或减去给定的日历字段
c.add(Calendar.YEAR,-2);//减2年
System.out.println(c);
//设置当前日历的年月日
c.set(2021,9,23);
System.out.println(c);
}
}