美文网首页
4,(了解)date、Comparable、comparator

4,(了解)date、Comparable、comparator

作者: 24_yu | 来源:发表于2017-11-03 12:59 被阅读0次

    一、日期格式化操作类:SimpleDateFormat

    ● 构造方法:public SimpleDateFormat(String pattern)
    ● 将日期格式化为字符串:public final String format(Date date)
    ● 将字符串格式化为日期:public Date parse(String source)

    //将日期变为字符串,格式化显示
    Date date = new Date(); // 当前日期
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    String str = sdf.format(date); // 将日期格式化为字符串
    
    //将字符串变为日期
    String str = "1980-09-19 12:12:12.122";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    Date date = sdf.parse(str);
    

    二、比较器

    ● comparator 是一个外比较器,适用于对象不支持自身比较的类,定义一个类实现comparator接口,覆写compara方法,对象比较的话可以使用comparaTo()方法。

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Iterator;
    
    class Person  {
        private String name ;
        private int age ;
            //省略set,get方法
        public Person(String name,int age) {
            this.name = name ;
            this.age = age ;
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]\n";
        }
        
    }
    class testComparatorMethod implements Comparator<Person>{
        @Override
        public int compare(Person o1, Person o2) {
            
            int num = o1.getName().compareTo(o2.getName());
            if (num != 0){
                return num;
            }
            int numage = o1.getAge() - o2.getAge();
            return numage;
            
        }
    }
    public class testComparator {
        public static void main(String[] args) throws Exception {
            ArrayList<Person> list = new ArrayList<Person>();
            Person per1 = new Person("aaa",25);
            Person per2 = new Person("bbb",21);
            Person per3 = new Person("ccc",27);
            Person per4 = new Person("ddd",23);
            list.add(per3);
            list.add(per2);
            list.add(per4);
            list.add(per1);
            Collections.sort(list, new testComparatorMethod());
            
            Iterator<Person> iter = list.iterator();
            while(iter.hasNext()){
                System.out.println(iter.next());
            }
        }
    
        
    }
    

    ● comparable:内比较器,用于在类内部进行比较
    实例:

    
    public class TestComparable {
        public static void main(String[] args) {
            
            Person2 per = new Person2("aaa");
            Person2 per1 = new Person2("bbb");
            System.out.println(per.compareTo(per1));
        }
    }
    
    class Person2 implements Comparable<Person2> {
        private String name;
    
        public String getName() {
            return name;
        }
    
        public Person2(String name) {
            this.name = name;
        }
    
        @Override
        public int compareTo(Person2 per) {
    
            if (this.name.compareTo(per.name) > 0)
                return 1;
            else if (this.name.compareTo(per.name) == 0)
                return 0;
            else
                return -1;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:4,(了解)date、Comparable、comparator

          本文链接:https://www.haomeiwen.com/subject/zmhtmxtx.html