简述三种时间关系
data类,和已通过gettime()方法获取毫秒值,然后需要我们通过simpleDataFormat来实现DataFormat类,要注意时间格式。而Calendor则是可以直接获取年月日,可以方便我们实现对时间的操作,尤其是闰年二月问题。
Date和System获取毫秒值
Date通过gettime()方法获取毫秒值和System获取毫秒值获取日期对象从19700年1月1日 00:00:00到现在的毫秒值。long l = System.currentTimeMillis();
System也提供了currentTimeMillis()方法获取毫秒值
/**
* 时间date simpledateformat实现dateformat
*/
Date date = new Date();
//获取日期对象从19700年1月1日 00:00:00到现在的毫秒值
long time = date.getTime();//setTimee设置毫秒值
//通过系统获取时间
long l = System.currentTimeMillis();
System.out.println(l+"和"+time);//毫秒值
时间格式(DataFormat)
G - 年代标志;y - 年;M - 月;d - 日;时(h表示在上午或下午1~12点,而H表示0-23);m - 分;s - 秒;S - 毫秒;
我们可以通过上述的年月日标志,通过format()
方法对data获取的毫秒值进行格式规范。当然也可以通过prase()
将其转化成毫秒值
/**
* SimpleDateFormat对时间毫秒信息设置格式yyyy-MM-dd HH:mm:ss
*/
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss a");
String format = dateFormat.format(time);
//上述情况反转
String ss = "2048-08-09 11:11:11";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = simpleDateFormat.parse(ss);
System.out.println(date1);
Calender
通过创建对象调用get()方法,根据传入的参数进行获取相关时间信息。注意Calender.Month获得的是0-11需要对获取月份+1;而通过set方法可以对获取的时间进行更改,而二月天数就是c.add(Calendar.DATE,-1);
直接获取最后一天。
/**
* Calendar
*/
Calendar c = Calendar.getInstance();
int i = c.get(Calendar.YEAR);
int i1 = c.get(Calendar.MONTH)+1;
int i2 = c.get(Calendar.DATE);
//2022年6月15日
System.out.println(i+"年"+i1+"月"+i2+"日");
/*c.add(Calendar.YEAR,2);//将年份向后推2年
int i = c.get(Calendar.YEAR);
int i1 = c.get(Calendar.MONTH)+1;
int i2 = c.get(Calendar.DATE);
//2024年6月15日
System.out.println(i+"年"+i1+"月"+i2+"日");
*/
//求二月时间
Scanner scanner = new Scanner(System.in);
System.out.println("你要查的年份:");
int i3 = scanner.nextInt();
Calendar calendar = Calendar.getInstance();
//获取查询年分的二月第一天
c.set(i3,2,1);
//使月份推一天,获取最后一天
c.add(Calendar.DATE,-1);
int i4 = c.get(Calendar.DATE);
System.out.println(i3+"年二月有"+i4+"天");
常用API
简述 Math(数字计算) System 大数据加减乘除BigInteger,浮点BigDecimal(double的长度在idea中不会完全实现,且是运算存在精度问题)
Math,常用于数字计算,
System
exit(0)停止程序
大数据计算
add() subtract() multiply() divide()
/*大数据计算+-/**/
// add substract multiply divide
//大数据封装为BigInteger对象
/* BigInteger big1 = new BigInteger("12345678909876543210");
BigInteger big2 = new BigInteger("98765432101234567890");
//add实现加法运算
BigInteger bigAdd = big1.add(big2);
System.out.println(bigAdd);
//subtract实现减法运算
BigInteger bigSub = big1.subtract(big2);
System.out.println(bigSub);
//multiply实现乘法运算
BigInteger bigMul = big1.multiply(big2);
System.out.println(bigMul);
//divide实现除法运算
BigInteger bigDiv = big2.divide(big1);
System.out.println(bigDiv);*/
//大数据封装为BigDecimal对象
BigDecimal big1 = new BigDecimal("0.09");
BigDecimal big2 = new BigDecimal("0.01");
//add实现加法运算
BigDecimal bigAdd = big1.add(big2);
System.out.println(bigAdd );
BigDecimal big3 = new BigDecimal("1.0");
BigDecimal big4 = new BigDecimal("0.32");
//subtract实现减法运算
BigDecimal bigSub = big3.subtract(big4);
System.out.println(bigSub);
BigDecimal big5 = new BigDecimal("1.10 5");
BigDecimal big6 = new BigDecimal("100");
//multiply实现乘法运算
BigDecimal bigMul = big5.multiply(big6);
System.out.println(bigMul);
//divide实现除法
BigDecimal bigDiv = big5.divide(big6,2,BigDecimal.ROUND_UP);
}