java基础十一
基础API与常见算法
1,Math:数字类
abs:返绝对值,将数字永远变成正值、
int a = -123; System.out.println(Math.abs(a));
ceil:向上取整,向上查找
double b = 12.3456789; System.out.println(Math.ceil(b));
floor:向下取整,向下查找;
double c = 23.3456; System.out.println(Math.floor(c));
round:四舍五入,取整,返回值为long
double d = 12.2345; System.out.println(Math.round(d));
*pow(a,b):a的b次方
int a = 2; int b = 5; System.out.println(Math.pow(a,b));
sqrt(a):开平方;
int a = 5; System.out.println(Math.sqrt(a));
PI:圆周率;
Math.Pi
random:随机数[0,1)取值范围包含0但不包含1
System.out.println(Math.sqrt(a));
for (int i = 0;i<100;i++){
System.out.println(Math.random());
}
Math.random()*(最大值-最小值)+最小值
for (int i =0;i<100;i++){
System.out.println((int) (Math.random()*(78-1)+1));
}
max:返回最大值;
min:返回最小值
max,min只能两位数
2,BigInteger:
不可变的任意精度整数
3,BigDecimal:
不可变的任意精度的有符号的小数
和:add()
BigInteger big1 = new BigInteger("123456789");
BigInteger big2 = new BigInteger("123456789");
System.out.println(big2.add(big2));
减:subtra()
BigInteger big1 = new BigInteger("123456789");
BigInteger big2 = new BigInteger("123456789");
System.out.println(big2.subtract(big1));
乘:multiply()
BigInteger big1 = new BigInteger("123456789");
BigInteger big2 = new BigInteger("123456789");
System.out.println(big2.multiply(big2));
除:divid()
BigInteger big1 = new BigInteger("123456789");
BigInteger big2 = new BigInteger("123456789");
System.out.println(big2.divide(big2));
余:remainder()
BigInteger big1 = new BigInteger("123456789");
BigInteger big2 = new BigInteger("123456789");
System.out.println(big2.remainder(big2));
4,Random:用于产生随机数
-
boolean nextBoolean():返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 boolean 值。
-
void nextBytes(byte[] bytes):生成随机字节并将其置于用户提供的 byte 数组中。
-
double nextDouble():返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 double 值。
-
float nextFloat():返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 float 值。
-
double nextGaussian():返回下一个伪随机数,它是取自此随机数生成器序列的、呈高斯(“正态”)分布的 double 值,其平均值是 0.0,标准差是 1.0。
-
int nextInt():返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
-
int nextInt(int n):返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
-
long nextLong():返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值。
-
Random ra = new Random(); System.out.println(ra.nextInt()); System.out.println(ra.nextDouble()); System.out.println(ra.nextGaussian());
日期时间API
JDK1.8之前
1、java.util.Date
new Date():当前系统时间
Date date = new Date(); System.out.println(date);
long getTime():返回该日期时间对象距离1970-1-1 0.0.0 0毫秒之间的毫秒值
System.out.println(date.getTime());
new Date(long 毫秒):把该毫秒值换算成日期时间对象
Date date1 = new Date(15520); System.out.println(date1);
2、java.text.SimpleDateFormat
SimpleDateFormat用于日期时间的格式化。
y 年 M月 d日 H时 m分 s秒 S毫秒 E星期几
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒 E");
System.out.println(sdf.format(date));
java.util.TimeZone
获取设备时区
TimeZone t = TimeZone.getTimeZone("America/Los_Angeles");
// System.out.println(t);
JDK1.8之后
1、本地日期时间:LocalDate、LocalTime、LocalDateTime
| 方法 | 描述 |
|---|---|
| now() / now(ZoneId zone) | 静态方法,根据当前时间创建对象/指定时区的对象 |
| of() | 静态方法,根据指定日期/时间创建对象 |
| getDayOfMonth()/getDayOfYear() | 获得月份天数(1-31) /获得年份天数(1-366) |
| getDayOfWeek() | 获得星期几(返回一个 DayOfWeek 枚举值) |
| getMonth() | 获得月份, 返回一个 Month 枚举值 |
| getMonthValue() / getYear() | 获得月份(1-12) /获得年份 |
| getHours()/getMinute()/getSecond() | 获得当前对象对应的小时、分钟、秒 |
| withDayOfMonth()/withDayOfYear()/withMonth()/withYear() | 将月份天数、年份天数、月份、年份修改为指定的值并返回新的对象 |
| with(TemporalAdjuster t) | 将当前日期时间设置为校对器指定的日期时间 |
| plusDays(), plusWeeks(), plusMonths(), plusYears(),plusHours() | 向当前对象添加几天、几周、几个月、几年、几小时 |
| minusMonths() / minusWeeks()/minusDays()/minusYears()/minusHours() | 从当前对象减去几月、几周、几天、几年、几小时 |
| plus(TemporalAmount t)/minus(TemporalAmount t) | 添加或减少一个 Duration 或 Period |
| isBefore()/isAfter() | 比较两个 LocalDate |
| isLeapYear() | 判断是否是闰年(在LocalDate类中声明) |
| format(DateTimeFormatter t) | 格式化本地日期、时间,返回一个字符串 |
| parse(Charsequence text) | 将指定格式的字符串解析为日期、时间 |
now 当前的本地时间 // LocalDate now = LocalDate.now(); //// of 获取指定的日期 时间对象 // LocalDate now1 = LocalDate.of(2026,11,4); //// LocalDate now2 = LocalDate.getDayOfMonth();
2、持续日期/时间:Period和Duration
Period:用于计算两个“日期”间隔
Duration:用于计算两个“时间”间隔
Date date1 = new Date();
// 获取本地时间
Date date2 = new Date("2025/12/31 23:59:59");
// 获取指定时间
Long time1 = date1.getTime();
// 获取从1970到本地时间的毫秒
Long time2 = date2.getTime();
// 获取从1970到指定时间的毫秒
Long time3 = time2-time1;
DateTimeFormatter:日期时间格式化
DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒 SSS毫秒 E 是这一年的D天");
// System.out.println(now.format(sdf));
系统相关类
java.lang.System类
-
static long currentTimeMillis() :返回当前系统时间距离1970-1-1 0:0:0的毫秒值
-
System sys = new System(); // System.out.println(System.currentTimeMillis());
-
static void exit(int status) :退出当前系统
-
结束当前的运行进程 // System.exit(5201314);
-
static void gc() :运行垃圾回收器。
-
System.gc()
-
static String getProperty(String key):获取某个系统属性,例如:java.version、user.language、user.country、file.encoding、user.name、os.version、os.name等等
-
System.out.println(System.getProperty("java.version")); System.out.println(System.getProperty("user.language")); System.out.println(System.getProperty("user.country")); System.out.println(System.getProperty("file.encoding")); System.out.println(System.getProperty("user.name")); System.out.println(System.getProperty("os.version")); System.out.println(System.getProperty("os.name"));
Runtime类
Runtime runtime = Runtime.getRuntime();
// long totalMemory = runtime.totalMemory();
// String str = "";
// for (int i = 0; i < args.length; i++) {
// str += i;
// }
// long freeMemory = runtime.freeMemory();
// System.out.println("总内存:" + totalMemory);
System.out.println("空闲内存:" + freeMemory);
System.out.println("已用内存:" + (totalMemory-freeMemory));
}
更多推荐




所有评论(0)