文章目录
(一)数学运算
本部分参考:Math常用数学运算
1. 三角函数方法:
- static double sin(double a ) : 返回角的三角正弦
- static double cos(double a) : 返回角的三角余弦
- static double tan(double a) : 返回角的三角正切
- static double asin(double a) : 返回角的反正弦
- static double acos(double a) : 返回角的反余弦
- static double atan(double a) : 返回角的反正切
- static double toRadians(double a) : 将角转换为弧度
- static doueble toDegrees(double a) : 将弧度转化为角
System.out.println("sin三角函数值为:"+Math.sin(1));
//0.8414709848078965
3.指数函数方法:
- static double exp(double a) : 用于获取e的a次方;
- static double log(double a) : 即lna;
- static double log10(double a) : 即log10a;
- static double sqrt(double a ):用于取a的平方根;
- static double cbrt(double a) : 用于取a的立方根;
- static double pow(double a, double b) : 用于求a的b次方;
4.取整函数方法:
- static double ceil(double a):返回大于等于a的整数值,返回值类型为double;
- static double floor(double a) : 返回小于等于a的整数值,返回值类型为double;
- static double rint(double a) :返回与a最接近的整数值,返回值类型为double;(如果两个同为整数且同样接近,选取偶数值的那个)
public static void main(String[] args) {
Double a = 10.12;
System.out.println(Math.ceil(a));
System.out.println(Math.floor(a));
System.out.println(Math.rint(a));
//11.0
//10.0
//10.0
}
- static int random( ):返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
- static int round(double a ): 其值等于Math.floor(a + 0.5),返回值类型为long;
- static long round(float a ): 其值等于Math.floor(a + 0.5),返回值类型为int;
System.out.println(Math.random()*(10-1+1));
//Math.random()*(max-min+1)
// 获得随机数:9.264532940355577
5.求绝对值运算和最值运算:static 类型 abs(类型); 返回对应类型的绝对值
- static 类型 abs(类型); 返回对应类型的绝对值
- static 类型 max(类型1, 类型2) ;返回对应类型的最大值
- static 类型 min(类型1, 类型2) ;返回对应类型的最小值
public static void main(String[] args) {
int a = -10;
double b = 100.103;
System.out.println(Math.max(a, b));
System.out.println(Math.min(a, b));
System.out.println(Math.abs(a));
//100.103
//-10.0
//10
}
(二)时间日期处理
1.简介
JAVA中常用的日期有三种(精确到毫秒):
- long–毫秒数
- Date–日期
- Calendar–日历、抽象类
Java中日期经常使用以下五个方面:
- 创建日期
- 日期格式化显示
- 日期的转换(主要是和字符串之间的相互转换)
- 日期中年.月.日.时.分.秒.星期.月份等获取。
- 日期的大小比较.日期的加减。
2.创建时间:
(1) long:
long now = System.currentTimeMillis();
System.out.println(now);
//1554448628477
(2)Date:
Date now = new Date();
System.out.println(now);
//Fri Apr 05 15:22:56 CST 2019
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowStr = sdf.format(now);
System.out.println(("格式化后的日期: " + nowStr));
//格式化后的日期: 2019-04-05 15:26:57
System.out.println("Date类型日期:" + now);
System.out.println("当前时间毫秒数:" + now.getTime()); //返回Date类型
//Date类型日期:Fri Apr 05 15:28:03 CST 2019
//当前时间毫秒数:1554449283332
(3)Calendar:
Calendar now1 = Calendar.getInstance(); //默认为当前时间
now1.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); //设置时区 北京时间
now1.set(2016, 10, 30, 15, 55, 44); //陷阱:Calendar的月份是0~11
Calendar now2 = new GregorianCalendar(); //默认为当前时间
Calendar now3 = new GregorianCalendar(2016, 10, 30, 15, 55, 44); //陷阱:Calendar的月份是0~11*/
(4)获取时间:
System.out.println("年: " + now.get(Calendar.YEAR)); //返回int类型
System.out.println("月: " + (now.get(Calendar.MONTH) + 1)); //返回int类型,注意:月份从零开始
System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH)); //返回int类型
System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY)); //返回int类型
System.out.println("分: " + now.get(Calendar.MINUTE)); //返回int类型
System.out.println("秒: " + now.get(Calendar.SECOND)); //返回int类型
System.out.println("Date类型日期:" + now.getTime()); //返回Date类型
System.out.println("当前时间毫秒数:" + now.getTimeInMillis()); //返回long类型
注意:
- Calendar的星期是从周日开始的,常量值为1。
- Calendar的月份是从一月开始的,常量值为0。
3.格式转换
- long转换成Date:
Date date = new Date();
date.setTime(long);
System.out.println(date);
//Thu Jan 01 08:00:00 CST 1970
- Date转换成Calendar
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
- Date转换成long
long l = date.getTime();
- Calendar转换成Date
Date date = cal.getTime()
- Date转换成String
Date now = new Date();
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowStr = sdf.format(now);
- String转换成Date
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2018-4-5 18:20:50");
4.日期比较大小
(1)Date:
- before.after.equals
- date1.compareTo(date2)
(2) Calendar:
- before.after.equals
- date1.compareTo(date2)
(3)计算时间差:
(Date和Calendar都转换成long来进行比较)
Date end = new Date();
Date begin = new Date();
long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
long day=between/(24*3600);
long hour=between%(24*3600)/3600;
long minute=between%3600/60;
long second=between%60/60;
System.out.println(""+day+"天"+hour+"小时"+minute+"分"+second+"秒");
5.日期加减
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR,-1);
System.out.println("去年是" + cal.get(Calendar.YEAR )+"年");
//去年是2018年
- 输出实际中每月最后一天
Calendar cal = Calendar.getInstance();
int maxDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
DateFormat formatter3=new SimpleDateFormat("yyyy-MM-"+maxDay);//固定格式
System.out.println(formatter3.format(cal.getTime()));
(三)异常处理
1.教程及案例
2. 自定义异常实例练习
InsufficientFundsException.java代码
public class InsufficientFundsException extends Exception {
//注意要继承Exception类
private double amount;
//amount 为取款时账户所缺乏的钱
public InsufficientFundsException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
CheackAccount .java代码实现
public class CheackAccount {
private double balance;//余额
private int number;//卡号
public CheackAccount(int number) {
this.number = number;
}
//存钱
public void deposit(double amount){
balance += amount;
}
//取钱
public void withdrow(double amount) throws InsufficientFundsException{
if (amount <= balance){
balance -= amount;
}
else {
double need = amount - balance;
throw new InsufficientFundsException(need);
}
}
//返余额与卡号
public double getBalance() {
return balance;
}
public int getNumber() {
return number;
}
CheackAccount:
public static void main(String[] args) {
CheackAccount c = new CheackAccount(250);
System.out.println("I want Depositint $500 ");
c.deposit(500);
try{
System.out.println("I'm Withdrawing $100");
c.withdraw(100);
System.out.println("I'm Withdrawing $600");
c.withdraw(600);
}catch (InsufficientFundsException e){
System.out.println("Sorry, but you are short $"
+ e.getAmount());
e.printStackTrace();
}
/*输出
Depositint $500
Withdrawing $100
Withdrawing $600
Sorry, but you are short $200.0
com.demo4_6.InsufficientFundsException
at com.demo4_6.CheackAccount.withdraw(CheackAccount.java:24)
at com.demo4_6.BankDemo.main(BankDemo.java:12)
*/
}
3.补充
try{
//待捕获代码
}catch(Exception e){
System.out.println("catch is begin");
return 1 ;
}finally{
System.out.println("finally is begin");
return 2 ;
}
在此程序中,第一条输出语句执行后会直接执行finally中输出语句,然后执行return 2,就是说catch 中的return 都会在finally 执行完后执行;