Date类
– public long getTime():返回自 1970 年 1 月 1 日 00:00:00 GMT 以来走过的总的毫秒数。
时间记录的两种方式:
a.Date日期对象。
b.时间毫秒值:从1970-01-01 00:00:00开始走到此刻的总的毫秒值。 1s = 1000ms
public class DateDemo01 {
public static void main(String[] args) {
// a.创建一个日期对象代表了系统此刻日期时间对象
Date d = new Date();
System.out.println(d);
// b.拿当前日期对象的时间毫秒值
long time = d.getTime();//获得时间毫秒值,
//一定注意:返回值是long;
System.out.println(time);
}
- 时间毫秒值可以用于做时间的计算:例如代码的执行性能分析。
public class DateDemo02 {
public static void main(String[] args) {
// 1.拿到此刻日期时间对象的毫秒值
long startTime = new Date().getTime();
for(int i = 1; i < 1000000 ; i++ ){
System.out.println("输出:"+i);
}
// 2.拿到此刻日期时间对象的毫秒值
long endTime = new Date().getTime();
System.out.println( (endTime - startTime) / 1000.0 +"s");
}
目标:Date类的有参数构造器的使用。
- 构造器:
– public Date():创建当前系统的此刻日期时间对象。
– public Date(long time):把时间毫秒值转换成日期对象。
流程:
Date日期对象 -> getTime() -> 时间毫秒值
时间毫秒值 -> new Date(时间毫秒值) -> Date日期对象
小结:
public Date(long time):把时间毫秒值转换成日期对象。
public class DateDemo03 {
public static void main(String[] args) {
// 需求:问121s以后的时间是多少。
// 1.拿到此刻日期对象
Date d = new Date();
System.out.println(d);
// 2.拿到此刻日期对象的时间毫秒值 往后走 121 s
long time = d.getTime() + 121*1000;
// 3.把时间毫秒值转换成日期对象。
Date d1 = new Date(time);
System.out.println(d1);
}
}
DateFormat
DateFormat作用:
1.可以把“日期对象”或者“时间毫秒值”格式化成我们喜欢的时间形式。(格式化时间)
2.可以把字符串的时间形式解析成日期对象。(解析字符串时间)
DateFormat是一个抽象类,不能直接使用,要找它的子类:SimpleDateFormat
我们需要用的是简单日期格式化类:SimpleDateFormat
SimpleDateFormat简单日期格式化类:
包:java.text.SimpleDateFormat
构造器:public SimpleDateFormat(String pattern):
指定时间的格式创建简单日期格式化对象。
方法:
– public String format(Date date):可以把日期对象格式化成我们喜欢的时间形式,返回的是字符串!
– public String format(Object time):可以把时间毫秒值格式化成我们喜欢的时间形式,返回的是字符串!
– public Date parse(String date) throws ParseException:把字符串的时间解析成日期对象
小结:
简单日期格式化类SimpleDateFormat可以把日期对象格式化成我们喜欢的时间形式
– public String format(Date date):可以把日期对象格式化成我们喜欢的时间形式,返回的是字符串!
public class SimpleDateFormatDemo01 {
public static void main(String[] args) {
// 需求:把此刻日期对象格式化成我们喜欢的形式。
// 1.得到此刻日期对象
Date d = new Date();
System.out.println(d);
// 2.创建一个简单日期格式化对象负责格式化日期对象
// 注意:参数是之间的格式。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
// 3.开始调用方法格式化时间得到格式化的字符串时间形式
String rs = sdf.format(d);
System.out.println(rs);
}
}
-
简单日期格式化类SimpleDateFormat可以直接格式化时间毫秒值
public String format(Object time)
public class SimpleDateFormatDemo02 { public static void main(String[] args) { // 1.问121s后的时间是多少。格式化输出。 // a.得到此刻日期对象 Date date = new Date(); System.out.println(date); // b.得到当前时间的时间毫秒值 long time = date.getTime(); time += 121 * 1000; // c.格式化时间毫秒值 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a"); System.out.println(sdf.format(time)); } }
-
:
简单日期格式化对象不仅可以把日期对象和时间毫秒值格式化成我们喜欢的字符串时间形式
它还可以把 把字符串的时间解析成日期对象
– public Date parse(String date) throws ParseException:把字符串的时间解析成日期对象
public class SimpleDateFormatDemo03 { public static void main(String[] args) throws ParseException { // 面试题:请问 “2019-11-04 09:30:30” 往后 1天15小时,30分29s后的时间是多少 // a.定义一个字符串时间 String date = "2019-11-04 09:30:30"; // b.把字符串的时间解析成Date日期对象 。(重点) // 1.创建一个简单日期格式化对象负责解析字符串的时间成为日期对象 // 注意:参数必须与被解析的时间的格式完全一致,否则执行报错!! SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 2.开始解析成日期对象 Date newDate = sdf.parse(date); // c.得到日期对象的时间毫秒值 + 往后走 1天15小时,30分29s long time = newDate.getTime() + (24L *60*60 + 15*60*60 + 30*60 + 29) * 1000; // d.把时间毫秒值格式化成喜欢的字符串的时间形式! System.out.println(sdf.format(time)); } }
-
Calendar
Calendar代表了系统此刻日期对应的日历对象。
Calendar是一个抽象类,不能直接创建对象。
Calendar日历类创建日历对象的语法:
Calendar rightNow = Calendar.getInstance();
Calendar的方法:
1.public static Calendar getInstance(): 返回一个日历类的对象。
2.public int get(int field):取日期中的某个字段信息。
3.public void set(int field,int value):修改日历的某个字段信息。
4.public void add(int field,int amount):为某个字段增加/减少指定的值
5.public final Date getTime(): 拿到此刻日期对象。
6.public long getTimeInMillis(): 拿到此刻时间毫秒值
public class CalendarDemo01 {
public static void main(String[] args) {
// 1.通过调用日历类的静态方法getInstance得到一个当前此刻日期对象对应的日历对象。
Calendar rightNow = Calendar.getInstance();
System.out.println(rightNow);
// 2.获取年:
int year = rightNow.get(Calendar.YEAR);
System.out.println(year);
int mm = rightNow.get(Calendar.MONTH) + 1;
System.out.println(mm);
// 3.一年中的第几天: 308
int days = rightNow.get(Calendar.DAY_OF_YEAR);
System.out.println(days);
// 4.修改日历的信息
//rightNow.set(Calendar.YEAR , 2099);
//System.out.println(rightNow.get(Calendar.YEAR));
// 5.日历可以得到此刻日期对象。
Date d = rightNow.getTime();
System.out.println(d);
// 6.此刻时间毫秒值
long time = rightNow.getTimeInMillis();
System.out.println(time);
// 7.请问701天 15小时后是哪个日期
// 让日历的一年中的第几天往后走 701天!
rightNow.add(Calendar.DAY_OF_YEAR , 701);
rightNow.add(Calendar.HOUR , 15);
long time1 = rightNow.getTimeInMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
System.out.println(sdf.format(time1));
}
}
Math
Math用于做数学运算。
Math类中的方法全部是静态方法,直接用类名调用即可。
方法:
方法名 说明
public static int abs(int a) 获取参数a的绝对值:
public static double ceil(double a) 向上取整
public static double floor(double a) 向下取整
public static double pow(double a, double b) 获取a的b次幂
public static long round(double a) 四舍五入取整
public class MathDemo {
public static void main(String[] args) {
// 1.取绝对值:返回正数。
System.out.println(Math.abs(10));
System.out.println(Math.abs(-10.3));
// 2.向上取整: 5
System.out.println(Math.ceil(4.00000001)); // 5.0
// 3.向下取整:4
System.out.println(Math.floor(4.99999999)); // 4.0
// 4.求指数次方
System.out.println(Math.pow(2 , 3)); // 2^3 = 8.0
// 5.四舍五入 至整数
System.out.println(Math.round(4.49999)); // 4
System.out.println(Math.round(4.500001)); // 5
}
}
System
System代表当前系统。
静态方法:
1.public static void exit(int status):终止JVM虚拟机,非0是异常终止。
2.public static long currentTimeMillis():获取当前系统此刻时间毫秒值。
3.可以做数组的拷贝。
arraycopy(Object var0, int var1, Object var2, int var3, int var4);
* 参数一:原数组
* 参数二:从原数组的哪个位置开始赋值。
* 参数三:目标数组
* 参数四:赋值到目标数组的哪个位置
* 参数五:赋值几个。
public class SystemDemo {
public static void main(String[] args) {
System.out.println("程序开始。。。");
// 1.终止当前虚拟机
//System.exit(0); // 0代表正常终止!!
// 2.得到系统当前时间毫秒值
long time = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
System.out.println(sdf.format(time));
// 3.可以做数组的拷贝(了解)
int[] arrs1 = new int[]{10 ,20 ,30 ,40 ,50 ,60 ,70};
int[] arrs2 = new int[6]; // [ 0 , 0 , 0 , 0 , 0 , 0]
// arrs2 = [0 , 30 , 40 , 50 , 0 , 0 ]
/**
arraycopy(Object src,int srcPos ,Object dest, int destPos, int length)
参数一:原数组
参数二:从哪个索引位置开始赋值
参数三:目标数组
参数四:目标数组的开始索引:
参数五:复制几个
*/
System.arraycopy(arrs1,2, arrs2 , 1 , 3);
System.out.println(Arrays.toString(arrs2));
System.out.println("程序结束。。。");
}
}
BigDecimal
浮点型运算的时候直接+ * / 可能会出现数据失真(精度问题)。
出现原因:二进制转换问题
BigDecimal可以解决浮点型运算数据失真的问题。
BigDicimal类:
包:java.math.
创建对象的方式(最好的方式:)
public static BigDecimal valueOf(double val) :包装浮点数成为大数据对象。
方法声明
public BigDecimal add(BigDecimal value) 加法运算
public BigDecimal subtract(BigDecimal value) 减法运算
public BigDecimal multiply(BigDecimal value) 乘法运算
public BigDecimal divide(BigDecimal value) 除法运算
public double doubleValue():把BigDecimal转换成double类型。
public class BigDecimalDemo {
public static void main(String[] args) {
// 浮点型运算的时候直接+ * / 可能会出现数据失真(精度问题)。
System.out.println(0.1 + 0.2);
System.out.println(0.09 + 0.01);
System.out.println(1.0 - 0.32);
System.out.println(1.015 * 100);
System.out.println(1.301 / 100);
System.out.println("-------------------------");
double a = 0.1 ;
double b = 0.2 ;
double c = a + b ;
System.out.println(c);
// 1.把浮点数转换成大数据对象运算
BigDecimal a1 = BigDecimal.valueOf(a);
BigDecimal b1 = BigDecimal.valueOf(b);
//BigDecimal c1 = a1.add(b1); // 加法
BigDecimal c1 = a1.divide(b1); // 除法
System.out.println(c1);
// 结果可能需要继续使用!!!
// BigDecimal只是解决精度问题的手段,double数据才是我们的目的!!
double rs = c1.doubleValue();
System.out.println(rs);
}
}
结果是:
包装类
Java为包装类做了一些特殊功能,以便程序员使用。
包装类作为类首先拥有了Object类的方法。
包装类作为引用类型的变量可以存储null值。
具体来看特殊功能主要有:
1.可以把基本数据类型的值转换成字符串类型的值。(没啥用)
String name=3+"";
只要在结尾加上空字符串,就可以将基本数据类型的值转变成字符串
– 调用toString()方法。
– 调用Integer.toString(基本数据类型的值)得到字符串。
– 直接把基本数据类型+空字符串就得到了字符串。
2.把字符串类型的数值转换成对应的基本数据类型的值。(真的很有用)
– Xxx.parseXxx(“字符串类型的数值”)
– Xxx.valueOf(“字符串类型的数值”):推荐使用!
public class PackageClass02 {
public static void main(String[] args) {
// 1.把基本数据类型的值转成字符串
Integer it = 100 ;
// a.调用toString()方法。
String itStr = it.toString();
System.out.println(itStr+1);
// b.调用Integer.toString(基本数据类型的值)得到字符串。
String itStr1 = Integer.toString(it);
System.out.println(itStr1+1);
// c.直接把基本数据类型+空字符串就得到了字符串。
String itStr2 = it+"";
System.out.println(itStr2+1);
// 2.把字符串类型的数值转换成对应的基本数据类型的值。(真的很有用)
String numStr = "23";
//int numInt = Integer.parseInt(numStr);
int numInt = Integer.valueOf(numStr);
System.out.println(numInt+1);
String doubleStr = "99.9";
//double doubleDb = Double.parseDouble(doubleStr);
double doubleDb = Double.valueOf(doubleStr);
System.out.println(doubleDb+0.1);
}
}
Java认为一切皆对象。引用数据类型就是对象了。
但是在Java中8基本数据类型不是对象,只是表示一种数据的类型形式,这8种数据类型显得很突兀。
Java为了一切皆对象的思想统一,把8种基本数据类型转换成对应的类,这个类称为
基本数据类型的包装类。
基本数据类型 包装类(引用数据类型)
byte Byte
short Short
int Integer(特殊)
long Long
float Float
double Double
char Character(特殊)
boolean Boolean
自动装箱:可以直接把基本数据类型的值或者变量赋值给包装类。
自动拆箱:可以把包装类的变量直接赋值给基本数据类型。
public class PackegeClass {
public static void main(String[] args) {
int a = 12 ;
Integer a1 = 12 ; // 自动装箱
Integer a2 = a ; // 自动装箱
double b = 99.9;
Double b1 = 99.9; // 自动装箱
Double b2 = b ; // 自动装箱
Integer c = 100 ;
int c1 = c ; // 自动拆箱
int d = 12;
Integer d1 = null; // 引用数据类型的默认值可以为null
Integer d2 = 0;
System.out.println("-----------------");
Integer it = Integer.valueOf(12); // 手工装箱!
// Integer it1 = new Integer(12); // 手工装箱!
Integer it2 = 12;
Integer it3 = 111 ;
int it33 = it3.intValue(); // 手工拆箱
int it333 = it3;
}
}
double b = 99.9;
Double b1 = 99.9; // 自动装箱
Double b2 = b ; // 自动装箱
Integer c = 100 ;
int c1 = c ; // 自动拆箱
int d = 12;
Integer d1 = null; // 引用数据类型的默认值可以为null
Integer d2 = 0;
System.out.println("-----------------");
Integer it = Integer.valueOf(12); // 手工装箱!
// Integer it1 = new Integer(12); // 手工装箱!
Integer it2 = 12;
Integer it3 = 111 ;
int it33 = it3.intValue(); // 手工拆箱
int it333 = it3;
}
}