//封装类:八个原始类型的封装类
/*
原始数据类型:是独立存在的,值相等,地址一定相等
引用数据类型:是object子类,值相等,地址不一定相等
int-----Integer
byte-----Byte
short----Short
long----Long
double----Double
float----Float
boolean---Boolean
char---Character
*/
public class DemoD {
//int的封装类是java.lang.Integer
public static void a(){
int a1 = 7;//将7的地址赋予了a1
Integer a2 = 7;//将7的地址赋予了a2
Integer a3 = new Integer(7);//根据7产生了一个对象a3
System.out.println(a1==a2);//true 比较a1和a2的地址是否相等
System.out.println(a1==a3);//true 比较a1和a3的地址是否相等,自动装箱/拆箱转换
System.out.println(a2==a3);//false 比较a2和a3的地址是否相等
}
public static void b(){
Integer i = 8;
byte b = i.byteValue();
double d = i.doubleValue();
int i2 = i.intValue();
int maxValue = Integer.MAX_VALUE;
int minValue = Integer.MIN_VALUE;
int i3 = Integer.parseInt("88");//将String类型强制转换成int类型,默认10进制转换
int i4 = Integer.parseInt("3",16);//以16进制转换
String s1 = Integer.toBinaryString(6);//将int类型按2进制转换成String类型
String s2 = Integer.toOctalString(9);
String s3 = Integer.toHexString(15);
}
//Math
public static void c(){
double e = Math.E;
double pi = Math.PI;
int max = Math.max(3,4);
int min = Math.min(6,7);
double a = Math.random();
double b1 = Math.floor(4.555);
double b2 = Math.round(4.555);
double b3 = Math.ceil(4.555);
}
//时间日期
public static void d2(){
//获取到系统的当前时间
Date date =new Date();
Date date2 = new Date(119,1,2,1,2,3);//1900年计 月:0~11
Date date3 = new Date(119,1,2);
System.out.println(date);
System.out.println(date2);
System.out.println(date3);
long t1 = date.getTime();//求1970年1月1日距当前时间多少毫秒
long t2 = date2.getTime();
long t3 = date3.getTime();
}
//时间格式化操作
public static void d3(){
Date date = new Date(119,0,1,1,2,3);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); // d:星期 dd:天 H:24小时制 h:12小时制
String formatDate = sdf.format(date);
System.out.println(formatDate);
}
//时间日期
public static void d4(){
//获取到Calendar的实例
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DAY_OF_MONTH);//当月的日期(第几号)
int hour24 = calendar.get(Calendar.HOUR_OF_DAY);
int hout12 = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
int day = calendar.get(Calendar.DAY_OF_WEEK);//一周是从星期天开始计算的 out:2
System.out.println(day);
}
public static void main(String[] args) {
d4();
}
}
封装类、Math类、Date类、Calendar类
最新推荐文章于 2024-07-21 19:27:56 发布