day11 常用类 System Runtime Math Random Date DateFormat SimpleDateFormat Calendar
2017-01-04 23:47
691 查看
System
arraycopy
currentTimeMillis
exit
gc
Runtime
getRuntime
exec
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
Random类,生成随机数 我想求23到123的随机数,其实可以换个思路,23+(int)(Math.random(100)+1)
BigInteger就是比long还要大,BigDecimal确保精度的问题
Date
DateFormat 做日期格式转化
SimpleDateFormat使用自定义的时间格式
DateFormat是SimpleDateFormat的父类
arraycopy
currentTimeMillis
exit
gc
package system_01; import java.util.Arrays; public class SystemDemo { public static void main(String[] args) { int[] arr = {1,2,3,4,5,6,7,8}; int[] arr2 = new int[10]; System.arraycopy(arr, 6, arr2, 0, 2); System.out.println(Arrays.toString(arr2)); System.out.println("----------------------------------------"); long time = System.currentTimeMillis(); System.out.println(time); System.out.println("----------------------------------------"); //System.exit(0); 运行后直接退出了 //System.out.println("111111111"); new SystemDemo(); new SystemDemo(); System.gc(); } @Override protected void finalize() throws Throwable { System.out.println("结束"); } }
Runtime
getRuntime
exec
package _02_Runtime; import java.io.IOException; public class RuntimeDemo { public static void main(String[] args) throws IOException { Runtime run = Runtime.getRuntime(); run.exec("notepad"); } }数学相关的类
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
Random类,生成随机数 我想求23到123的随机数,其实可以换个思路,23+(int)(Math.random(100)+1)
Random random = new Random(); int xy = random.nextInt(100); System.out.println(xy);ThreadLocalRandom类 1.7出来的,提供了更加安全的随机数 是单例模式的
ThreadLocalRandom tlr = ThreadLocalRandom.current(); int wu = tlr.nextInt(0, 11); System.out.println(wu);UUID类:生成通用唯一标识符 一般用于数据库的主键,作为令牌机制的标志.
String str1 = UUID.randomUUID().toString(); String str2 = UUID.randomUUID().toString(); System.out.println(str1); System.out.println(str2);BigInteger和BigDecimal
BigInteger就是比long还要大,BigDecimal确保精度的问题
package _05_BigInteger_BigDecimal; import java.math.BigDecimal; public class BigDecimalDemo { public static void main(String[] args) { double d1 = 2.0; double d2 = 1.9; System.out.println(d1-d2); BigDecimal b1 = new BigDecimal("2.0"); BigDecimal b2 = new BigDecimal("1.9"); System.out.println(b1.subtract(b2)); } }日期相关的类
Date
DateFormat 做日期格式转化
SimpleDateFormat使用自定义的时间格式
DateFormat是SimpleDateFormat的父类
package _06_Date; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Date date = new Date(); System.out.println(date); System.out.println("----------------------------------------"); DateFormat format = DateFormat.getDateInstance(); String time = format.format(date); String time2 = DateFormat.getDateTimeInstance().format(date); System.out.println(time); System.out.println(time2); //String转到date Date date2 = format.parse(time); System.out.println(date2); System.out.println("----------------------------------------"); String pattern = "yyyy年MM月dd日 HH:mm:ss"; SimpleDateFormat sdf = new SimpleDateFormat(pattern); String timesdf = sdf.format(date); System.out.println(timesdf); } }Calendar 表示日历,也是个抽象类
package _06_Date; import java.util.Calendar; public class CalendarDemo { /** * @param args */ public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); getDate(calendar); //设置偏移量 calendar.add(Calendar.YEAR, 1); getDate(calendar); //把Calendar转化为date System.out.println(calendar.getTime()); } public static void getDate(Calendar calendar){ int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int date = calendar.get(Calendar.DATE); int hours = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); System.out.println(year+"年"+month+"月"+date+"日"+" "+hours+":"+minute+":"+second); } }
相关文章推荐
- Java基础之常用类--System、Runtime、Date、Calendar、Math、Random
- Java基础 - 正则,Math,Random,System,Date,SimpleDateFormat,Calendar,BigInteger,BigDecimal
- 几个特殊类Math&Runtime&System&Random&Date&Calendar&DateFormat
- java--jdk api中其他对象(System,Runtime,Calendar,Math,Random,Date)
- Java集合框架-5.【System类】【Runtime】【Date】【Calendar】【Math & Random】
- java常用类库续1(日期操作类Date、Calendar、DateFormat、SimpleDateFormat、Math与Random类)
- JavaSE实战——API(下) System、Runtime、Math、Date、DateFormat、Calendar、日期格式解析
- 几种常见类的使用(System,Runtime,Math,Date,Calendar,Random)
- 学习笔记_JavaSE_14_正则表达式、Math、Random、System、BigInteger、BigDecimal、Date与DateFormat、Calendar
- 14 API-常用类(正则表达式(Pattern,Matcher),Math,Random,System,BigInteger,BigDecimal,Date/DateFormat,Calendar)
- 黑马程序员 Java基础之System,Runtime,Date,Calendar,Math.Random对象
- System_Runtime_Date_Calendar_Math-Random
- Java常用类|数据类型包装类、Date、SimpleDateFormat 、Calendar 、Math
- Java基础 System,Runtime,Date,Calendar,Math-Random,Io流
- System,Runtime,Date,Calendar,Math-Random类
- 黑马程序员------其他类(System、Runtime、Date、Calendar、Math-Random)
- 其他常用类简介(System、Runtime、Date、Calendar、Math、Random)
- JavaDay09--Notes(集合的接口与功能+常用类Math,Random,System,Runtime,Calendar)
- 【18上】System,Runtime,Date,Calendar,Math-Random类的应用
- 黑马程序员_基础_System、Runtime、Date、Calendar和Math-Random等常用对象的学习笔记