JDK1.8之前的时间API

System类

   java.util.Date类
       |---java.sql.Date类 对应数据库中的日期类型的变量
  • toString()方法:显示当前的年 月 日 时 分 秒
  • getTime()方法: 获取当前Date对象对应的毫秒数(时间戳)
public class Test {

    public static void main(String[] args) {
        // System类中的currentTimeMillis(),返回的值是当前时间与1970年1月1日到现在的毫秒数
        System.out.println(System.currentTimeMillis());

        Date date1= new Date();
        System.out.println(date1);
        System.out.println(date1.getTime());


        java.sql.Date date2 = new java.sql.Date(date1.getTime());
        System.out.println(date2);
    }

}

JDK 1.8后

public class Test {

    public static void main(String[] args) throws ParseException {

        // SimpleDateFromat
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        // 1. 时间格式化 日期 ---> 字符串
        Date date = new Date();
        System.out.println(date);

        String datestr = simpleDateFormat.format(date);
        System.out.println(datestr);

        // 2. 时间解析   字符串 ---> 日期(使用默认构造器 str必须是21-2-7 上午10:53这种格式)
        String str = "21-2-7 上午10:53";
        Date date1 = simpleDateFormat.parse(str);
        System.out.println(date1);
        // 2.1 使用指定格式来解析
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy.mm.dd");
        Date date2 = simpleDateFormat1.parse("2020.03.17");
        System.out.println(date2);

        System.out.println("-----------------------------");
        // Calender(抽象类)日历类
        Calendar calendar = Calendar.getInstance();
        // 1.get day_of_month当月的第几天
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        // 2.set()
        calendar.set(Calendar.DAY_OF_MONTH,4);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        // 3.add()
        calendar.add(Calendar.DAY_OF_MONTH, 3);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
        // 4.getTime()  返回Date对象
        Date date3 =calendar.getTime();
        System.out.println(date3);
        // 5.setTime()

        System.out.println("-----------------------------");

        // 偏移量
        Date date4 = new Date(2020, 9,8);
        System.out.println(date4);
        Date date5 = new Date(2020 - 1900, 9 - 1,8);
        System.out.println(date5);


        System.out.println("-----------------------------");
        // LocalDate LocalTime LocalDateTime的使用
        // now获取当前的日期,时间 ,日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);
        // of()
        LocalDateTime localDateTime1 = LocalDateTime.of(2020,3,1,16,45,12,2);
        System.out.println(localDateTime1);
        // get()
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());


        System.out.println("-----------------------------");

        // 瞬间 Instant
        Instant instant = Instant.now();
        System.out.println(instant);
        // 按照本初子午线,补八个小时 + 8:00
        OffsetDateTime offsetDateTime= instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
    }




}

比较器

public class Demo {
    public static void main(String[] args) {
        Integer[] arr = new Integer[]{1,23,45,-12,32,3445,35,653,134,897,23,414,24,56};
        Arrays.sort(arr,new Comparator(){
            /**
             * Compares its two arguments for order.  Returns a negative integer,
             * zero, or a positive integer as the first argument is less than, equal
             * to, or greater than the second.<p>
             * <p>
             * In the foregoing description, the notation
             * <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
             * <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
             * <tt>0</tt>, or <tt>1</tt> according to whether the value of
             * <i>expression</i> is negative, zero or positive.<p>
             * <p>
             * The implementor must ensure that <tt>sgn(compare(x, y)) ==
             * -sgn(compare(y, x))</tt> for all <tt>x</tt> and <tt>y</tt>.  (This
             * implies that <tt>compare(x, y)</tt> must throw an exception if and only
             * if <tt>compare(y, x)</tt> throws an exception.)<p>
             * <p>
             * The implementor must also ensure that the relation is transitive:
             * <tt>((compare(x, y)&gt;0) &amp;&amp; (compare(y, z)&gt;0))</tt> implies
             * <tt>compare(x, z)&gt;0</tt>.<p>
             * <p>
             * Finally, the implementor must ensure that <tt>compare(x, y)==0</tt>
             * implies that <tt>sgn(compare(x, z))==sgn(compare(y, z))</tt> for all
             * <tt>z</tt>.<p>
             * <p>
             * It is generally the case, but <i>not</i> strictly required that
             * <tt>(compare(x, y)==0) == (x.equals(y))</tt>.  Generally speaking,
             * any comparator that violates this condition should clearly indicate
             * this fact.  The recommended language is "Note: this comparator
             * imposes orderings that are inconsistent with equals."
             *
             * @param o1 the first object to be compared.
             * @param o2 the second object to be compared.
             * @return a negative integer, zero, or a positive integer as the
             * first argument is less than, equal to, or greater than the
             * second.
             * @throws NullPointerException if an argument is null and this
             *                              comparator does not permit null arguments
             * @throws ClassCastException   if the arguments' types prevent them from
             *                              being compared by this comparator.
             */
            @Override
            public int compare(Object o1, Object o2) {
                if ( o1 instanceof Integer && o2 instanceof Integer){
                    int i1 = (int) o1;
                    int i2 = (int) o2;
                    if (i1 > i2){
                        return 1;
                    }else if (i1 < i2){
                        return -1;
                    }else {
                        return 0;
                    }
                }else {
                    throw new RuntimeException("转换异常!");
                }
            }
        });

        System.out.println(Arrays.toString(arr));

    }


}

public class Person implements Comparable {

    private String name;


    private int 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;
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }


    public Person() {
    }

    @Override
    public int compareTo(Object o) {

        if (o instanceof Person){
            Person person = (Person) o;
            if (this.age > person.age){
                return 1;
            }else if (this.age < person.age){
                return -1;
            }else {
                return 0;
            }
        }else{
            throw new RuntimeException("类型不一致!");
        }
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


class Test3{
    public static void main(String[] args) {

        Person[] peoples = new Person[3];
        Person p1 = new Person("xi",2);
        Person p2 = new Person("xi",8);
        Person p3 = new Person("xi",3);
        peoples[0] = p1;
        peoples[1] = p2;
        peoples[2] = p3;

        System.out.println(p1.compareTo(p2));
        System.out.println(p2.compareTo(p1));
        System.out.println(p1.compareTo(p3));

        Arrays.sort(peoples);
        System.out.println(Arrays.toString(peoples));
    }

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐