@Testpublicvoidtest10(){// 获取开始的时间节点long start =System.currentTimeMillis();for(int i =1; i <=10000; i++){System.out.println(i);}// 获取代码运行结束后的时间节点long end =System.currentTimeMillis();System.out.println("共耗时:"+(end - start)+"毫秒");}
@Testpublicvoidtest11(){StringBuffer str =newStringBuffer("HelloWorld");
str.insert(5,"java");System.out.println(str);//HellojavaWorld
str.delete(5,9);System.out.println(str);//HelloWorld
str.replace(5,10,"java");System.out.println(str);//HellojavaString s = str.substring(5,9);System.out.println(s);//java}
@Testpublicvoidtest2(){//public Date():分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒Date d1 =newDate();System.out.println(d1);//Thu Aug 19 15:40:36 CST 2021//public Date(long date):分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数long date =1000*60*60;Date d2 =newDate(date);System.out.println(d2);//Thu Jan 01 09:00:00 CST 1970}
@Testpublicvoidtest3(){//创建日期对象Date d =newDate();//public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值System.out.println(d.getTime());//1629359232841System.out.println(d.getTime()*1.0/1000/60/60/24/365+"年");//51.666642340214366年//public void setTime(long time):设置时间,给的是毫秒值// long time = 1000*60*60;long time =System.currentTimeMillis();
d.setTime(time);//Thu Aug 19 15:47:12 CST 2021System.out.println(d);}
@Testpublicvoidtest4()throwsException{//格式化:从 Date 到 StringDate d =newDate();// SimpleDateFormat sdf = new SimpleDateFormat();SimpleDateFormat sdf =newSimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");String s = sdf.format(d);System.out.println(s);//2021年08月19日 16:08:06System.out.println("--------");//从 String 到 DateString ss ="2048-08-09 11:11:11";//ParseExceptionSimpleDateFormat sdf2 =newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");Date dd = sdf2.parse(ss);System.out.println(dd);//Sun Aug 09 11:11:11 CST 2048}
@Testpublicvoidtest5(){//获取日历类对象Calendar c =Calendar.getInstance();//public int get(int field):返回给定日历字段的值int year = c.get(Calendar.YEAR);int month = c.get(Calendar.MONTH)+1;int date = c.get(Calendar.DATE);System.out.println(year +"年"+ month +"月"+ date +"日");//2021年8月19日//public abstract void add(int field, int amount):根据日历的规则,将指定的时间量添加或减去给定的日历字段//需求1:3年前的今天// c.add(Calendar.YEAR,-3);// year = c.get(Calendar.YEAR);// month = c.get(Calendar.MONTH) + 1;// date = c.get(Calendar.DATE);// System.out.println(year + "年" + month + "月" + date + "日");//需求2:10年后的10天前// c.add(Calendar.YEAR,10);// c.add(Calendar.DATE,-10);// year = c.get(Calendar.YEAR);// month = c.get(Calendar.MONTH) + 1;// date = c.get(Calendar.DATE);// System.out.println(year + "年" + month + "月" + date + "日");//public final void set(int year,int month,int date):设置当前日历的年月日
c.set(2050,10,10);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH)+1;
date = c.get(Calendar.DATE);System.out.println(year +"年"+ month +"月"+ date +"日");//2050年11月10日}
@Testpublicvoidtest1(){Random r =newRandom();for(int i=0;i<3;i++){int num = r.nextInt(10);System.out.println("number:"+num);}}