• Java 工具类—日期获得,随机数,系统命令,数据类型转换


      1 package tems;
      2 
      3 import java.text.SimpleDateFormat;
      4 import java.util.Arrays;
      5 import java.util.Calendar;
      6 import java.util.Date;
      7 import java.util.List;
      8 import java.util.Random;
      9 import java.util.Scanner;
     10 
     11 /**
     12  * Math类
     13  * 
     14  * */
     15 class Demo6{
     16     public static void main(String[] args){
     17         //取整数
     18         Math.ceil(34.5);//返回大于这个数的最小整数
     19         Math.floor(34.5);//返回小于这个数的最大整数
     20         //平方或者开方
     21         Math.pow(2,3);
     22         //四舍五入
     23         Math.round(12.54);
     24     }
     25 }
     26 /**
     27  * Date类
     28  * 
     29  * */
     30 class Demo2{
     31     public static void main(String[] args) throws Exception {
     32         //获取当前时间
     33         Date date = new Date();
     34         //按照自己的方式执行日期显示的格式
     35         SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd E HH:mm:ss");
     36         String sss = sdf.format(date);
     37         //字符串转日期(date)
     38         String s="2015-03-08";
     39         SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-dd-MM");
     40         sdf.parse(s);
     41     }
     42 }
     43 /**
     44  * Calendar类
     45  * 
     46  * */
     47 class Demo3{
     48     public static void main(String[] args){
     49         Calendar c=Calendar.getInstance();
     50         //获取年年月日
     51         c.get(Calendar.YEAR);//
     52         c.get(Calendar.MONTH+1);//
     53         c.get(Calendar.DAY_OF_MONTH);//
     54         c.get(Calendar.DAY_OF_WEEK);//星期
     55         //设置日期
     56         //set(int year,int month,int date)
     57         c.set(2012,2,18);
     58         //对当前日期进行增加和删除
     59         c.add(Calendar.DAY_OF_MONTH, -18);
     60     }
     61 }
     62 /**
     63  * Random类
     64  * 
     65  * */
     66 class Demo4{
     67     public static void main(String[] args){
     68         Random r = new Random();
     69         int random = r.nextInt(10)+1;
     70     }
     71 }
     72 /**
     73  * Scanner类
     74  * 
     75  * */
     76 class Demo5{
     77     public static void main(String[] args){
     78         //从键盘输入数据
     79         Scanner scanner = new Scanner(System.in);
     80         int guess = scanner.nextInt();
     81         //在OJ做题时,面对不确定数据用到的输入方法
     82         //while(sc.hasnext()){}
     83     
     84     }
     85 }
     86 /**
     87  * Runtime类
     88  * 
     89  */
     90 
     91 class Demo {
     92     /*获取Runtime对象需要用getRuntime()方法
     93      * Runtime类是一个单利设计模式。
     94      * 注:Runtime只能杀掉java虚拟机启动的进程。不可以杀掉系统进程*/
     95     public static void main(String[] args) throws Exception {
     96         Runtime run=Runtime.getRuntime();//得到当前运行对象
     97         Process p=run.exec("c:\winmine.exe");//开启进程
     98         p.destroy();//关闭进程
     99         Process p1=run.exec("notepad.exe demo.txt");
    100         p1.destroy();
    101         
    102     }
    103     
    104 }
    105 /**
    106  * 数据类型之间的转换
    107  * 
    108  * */
    109 class Demo7{
    110     public static void main(String[] args){
    111 //进制转换
    112         //十进制转成其它进制:
    113         String s1 = Integer.toHexString(60);
    114         //其他进制转成十进制:
    115         Integer.parseInt("3c",16);    
    116 //数组<—>集合
    117         List <String>al=Arrays.asList(s1);
    118         String s[]=al.toArray(new String[10]);
    119     }
    120 }
  • 相关阅读:
    树莓派aria2 init文档
    树莓派aria2 init文档
    HDU 6000 Wash【优先队列优化贪心】【最大值+最小值】
    HDU 6000 Wash【优先队列优化贪心】【最大值+最小值】
    HDU 3264||POJ 3831 Open-air shopping malls【计算机几何】【圆相交面积模板】
    HDU 3264||POJ 3831 Open-air shopping malls【计算机几何】【圆相交面积模板】
    HDU 6178 Monkeys【dfs】【输入外挂模板】
    HDU 6178 Monkeys【dfs】【输入外挂模板】
    HDU 6181 Two Paths【次短路】【模板题】
    返回一个二维整数数组中最大子数组的和
  • 原文地址:https://www.cnblogs.com/zxxiaoxia/p/4263638.html
Copyright © 2020-2023  润新知