博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java工具类
阅读量:6076 次
发布时间:2019-06-20

本文共 6315 字,大约阅读时间需要 21 分钟。

目录                                                                                            

  • Math
  • Random
  • Date
  • Carlendar
  • DateFormat
  • SimpleDateFoemat
  • Comparator
  • Clone

Math                                                                                          

System.out.println("PI的值:"+Math.PI);        System.out.println("-10的绝对值:"+Math.abs(-10));        System.out.println("Math.random()="+Math.random());        System.out.println("Math.round(98.56)="+Math.round(98.568977d));        System.out.println("Math.round(88.56789)="+Math.round(88.56789*100)/100.0);        System.out.println("Math.sqrt(2d)="+Math.sqrt(2d));

Random                                                                                   

//随机数        Random r = new Random();        int x = r.nextInt(100);        System.out.println("x="+x);

Date                                                                                          

//创建了一个日期对象        Date date = new Date();        System.out.println("当前时间的毫秒:"+date.getTime());        System.out.println(date);

Calendar                                                                                   

Calendar c = Calendar.getInstance();//        Calendar c = new GregorianCalendar();                int year = c.get(Calendar.YEAR);//年        int month = c.get(Calendar.MONTH);//月        int day = c.get(Calendar.DAY_OF_MONTH);//日        int hour = c.get(Calendar.HOUR_OF_DAY);//时        int minute = c.get(Calendar.MINUTE);//分        int second = c.get(Calendar.SECOND);//秒        int millisecond = c.get(Calendar.MILLISECOND);//毫秒        //星期        String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};        int week = c.get(Calendar.DAY_OF_WEEK);                StringBuilder sb = new StringBuilder();      sb.append(year).append("年").append(month).append("月").append(day).append("日  ")        .append(hour).append(":").append(minute).append(":").append(second).append(" ")        .append(millisecond).append(" ").append(weeks[week]);                System.out.println(sb.toString());

DateFormat                                                                              

//        DateFormat df = DateFormat.getDateInstance();        //参数(指定日期风格,指定语言环境)//        DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINA);//        DateFormat df = DateFormat.getTimeInstance();        DateFormat df = DateFormat.getTimeInstance(DateFormat.LONG, Locale.getDefault());                String nowDate = df.format(new Date());        System.out.println("当前日期为:"+nowDate);

SimpleDateFormat                                                                   

//用默认的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。//        SimpleDateFormat sdf = new SimpleDateFormat();        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");                String s = sdf.format(new Date());        System.out.println(s);

Comparator                                                                              

/** * 人类 * 如果自定义类的对象要实现排序(比较),那么就要实现Comparable接口,并实现接口的 * compareTo方法 */public class Person implements Comparable
{ private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } //比较器的比较方法 //如果当前对象小于参数对象,返回-1 //如果当前对象等于参数对象,返回0 //如果当前对象大于参数对象,返回1 @Override public int compareTo(Person o) { //实现:定义对象之间的比较规则 if(o==null){ throw new NullPointerException(); } if(this.age
o.age){ return 1; } return 0; }}
/** * 人类*/public class Person2{    private String name;    private int age;            public Person2() {    }    public Person2(String name, int age) {        this.name = name;        this.age = age;    }        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public String toString() {        return "Person [name=" + name + ", age=" + age + "]";    }}
/** * 自定义的类比较器 */public class Person2Comparator implements Comparator
{ @Override public int compare(Person2 o1, Person2 o2) { if(o1==null || o2==null){ throw new NullPointerException(); } if(o1.getAge()
o2.getAge()){ return 1; } return 0; }}
//对象的排序public class PersonSortDemo {    public static void main(String[] args) {        String[] names = {"张三","李四","王五","刘备","张飞","关羽"};                Person[] persons = {                 new Person("张三", 27), new Person("李四", 29),                new Person("王五", 41), new Person("刘备", 33),                new Person("张飞", 25), new Person("关羽", 30) };                Person2[] persons2 = {                 new Person2("张三", 27), new Person2("李四", 29),                new Person2("王五", 41), new Person2("刘备", 33),                new Person2("张飞", 25), new Person2("关羽", 30) };                    Arrays.sort(names);        System.out.println(Arrays.toString(names));                //自定义类对象数组排序        Arrays.sort(persons);        System.out.println(Arrays.toString(persons));                //使用自定义的比较器        Arrays.sort(persons2, new Person2Comparator());        System.out.println(Arrays.toString(persons2));                    }}

Clone                                                                                        

/** * 一个类要想实现克隆功能,必须: * 1、实现Cloneable接口,该接口是一个标记接口 * 2、重写Object类中的clone方法 */public class Cat implements Cloneable{    private String name;    private int age;        public Cat(String name, int age) {        super();        this.name = name;        this.age = age;    }        public Cat() {        super();    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    @Override    public String toString() {        return "Cat [name=" + name + ", age=" + age + "]";    }    //重写Object的clone方法    @Override    protected Object clone() throws CloneNotSupportedException {        return super.clone();    }    }
//要创建一组类似的对象,那么从性能上考虑,就可以使用克隆技术public class Test {    public static void main(String[] args) {        Cat c = new Cat("张三",3);        System.out.println("c="+c);        try {            Cat c1 = (Cat)c.clone();            System.out.println("c1="+c1);        } catch (CloneNotSupportedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

我是天王盖地虎的分割线                                                                 

 

转载地址:http://vuxgx.baihongyu.com/

你可能感兴趣的文章
用yum安装mariadb
查看>>
一点IT"边缘化"的人的思考
查看>>
Gallery循环滑动
查看>>
Sql与C#中日期格式转换总结
查看>>
iOS开发流程总结
查看>>
hadoop datanode 启动出错
查看>>
js颜色拾取器
查看>>
IDEA使用(1)intellIJ idea 配置 svn
查看>>
WPF 降低.net framework到4.0
查看>>
数据管理DMS 全量SQL诊断:你的SQL是健康的蓝色,还是危险的红色?
查看>>
搭建一个通用的脚手架
查看>>
开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
查看>>
开源磁盘加密软件VeraCrypt教程
查看>>
本地vs云:大数据厮杀的最终幸存者会是谁?
查看>>
阿里云公共镜像、自定义镜像、共享镜像和镜像市场的区别 ...
查看>>
shadowtunnel v1.7 发布:新增上级负载均衡支持独立密码
查看>>
Java线程:什么是线程
查看>>
mysql5.7 创建一个超级管理员
查看>>
【框架整合】Maven-SpringMVC3.X+Spring3.X+MyBatis3-日志、JSON解析、表关联查询等均已配置好...
查看>>
要想成为高级Java程序员需要具备哪些知识呢?
查看>>