guyanqing 10 ماه پیش
والد
کامیت
8f34ccfcc6

+ 5 - 1
src/main/java/com/sf/javase/day11/api/T.java

@@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.Date;
+import java.util.*;
 
 /**
  * 常用Api练习
@@ -106,6 +106,10 @@ public class T {
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
         Date parse = sdf.parse(str);
         System.out.println(parse);
+    }
+
+    @Test
+    public void t6(){
 
     }
 }

+ 93 - 0
src/main/java/com/sf/javase/day12/T.java

@@ -0,0 +1,93 @@
+package com.sf.javase.day12;
+
+import com.sun.org.apache.xpath.internal.SourceTree;
+import org.junit.jupiter.api.Test;
+import org.omg.CORBA.PUBLIC_MEMBER;
+
+import java.net.SocketTimeoutException;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.TemporalAccessor;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.TimeZone;
+
+/**
+ * Api
+ */
+public class T {
+    @Test
+    public void t1(){
+        Calendar calendar = Calendar.getInstance();
+        System.out.println(calendar);
+        System.out.println(calendar.get(Calendar.YEAR));
+        System.out.println(calendar.get(Calendar.MONTH));
+        System.out.println(calendar.get(Calendar.DATE));
+        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
+        System.out.println(calendar.get(Calendar.WEEK_OF_MONTH));
+        System.out.println(calendar.get(Calendar.DAY_OF_MONTH));
+    }
+
+    @Test
+    public void t2(){
+        //获取一个时区
+        TimeZone timeZone = TimeZone.getTimeZone("GMT+8:00");
+        Calendar c = Calendar.getInstance(timeZone);
+        int year = c.get(Calendar.YEAR);
+        int month = c.get(Calendar.MONTH)+1;
+        int day = c.get(Calendar.DATE);
+        int hour = c.get(Calendar.HOUR_OF_DAY);
+        int minute = c.get(Calendar.MINUTE);
+        System.out.println(year + "-" + month + "-" + day + " " + hour + ":" + minute);
+    }
+
+
+    @Test
+    public void t3(){
+        Calendar calendar = Calendar.getInstance();
+        Date date = calendar.getTime();
+        long timeMillis = System.currentTimeMillis();
+        Date date1 = new Date(timeMillis);
+        calendar.setTime(date1);
+        calendar.set(Calendar.DAY_OF_MONTH, 8);
+        System.out.println("当前时间日设置为8后,时间是:" + calendar.getTime());
+        calendar.add(Calendar.HOUR, 2);
+        System.out.println("当前时间加2小时后,时间是:" + calendar.getTime());
+        calendar.add(Calendar.MONTH, -2);
+        System.out.println("当前日期减2个月后,时间是:" + calendar.getTime());
+    }
+
+    @Test
+    public void t4(){
+        LocalDate now = LocalDate.now();
+        System.out.println(now);
+        LocalTime now1 = LocalTime.now();
+        System.out.println(now1);
+        LocalDateTime now2 = LocalDateTime.now();
+        System.out.println(now2);
+        LocalDate of = LocalDate.of(2024, 07, 31);
+        System.out.println(of.getDayOfYear());
+        boolean after = of.isAfter(now);
+        System.out.println(after);
+        boolean before = of.isBefore(now);
+        System.out.println(before);
+    }
+
+    @Test
+    public void t5(){
+//        String property = System.getProperty("java.version");
+//        System.out.println(property);
+        //方式三:自定义的方式(关注、重点)
+        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
+        //格式化
+        String strDateTime = dateTimeFormatter.format(LocalDateTime.now());
+        System.out.println(strDateTime); //2022/12/04 21:05:42
+        //解析
+        TemporalAccessor accessor = dateTimeFormatter.parse("2022/12/04 21:05:42");
+        LocalDateTime localDateTime = LocalDateTime.from(accessor);
+        System.out.println(localDateTime); //2022-12-04T21:05:42
+    }
+}
+

+ 44 - 0
src/main/java/com/sf/javase/day12/comparable/Goods.java

@@ -0,0 +1,44 @@
+package com.sf.javase.day12.comparable;
+
+/**
+ * java 比较器:
+ * 选择:   可以在实体类中进行重写compareTo();
+ * 代码不变  ,  之应用一次   定制排序    匿名内部类  重写compare(o1,o2)
+ */
+
+public class Goods {
+    private String name;
+    private double price;
+
+    public Goods() {
+    }
+
+    public Goods(String name, double price) {
+        this.name = name;
+        this.price = price;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public double getPrice() {
+        return price;
+    }
+
+    public void setPrice(double price) {
+        this.price = price;
+    }
+
+    @Override
+    public String toString() {
+        return "Goods{" +
+                "name='" + name + '\'' +
+                ", price=" + price +
+                '}';
+    }
+}

+ 29 - 0
src/main/java/com/sf/javase/day12/comparable/GoodsTest.java

@@ -0,0 +1,29 @@
+package com.sf.javase.day12.comparable;
+
+import java.util.Arrays;
+import java.util.Comparator;
+
+public class GoodsTest {
+    public static void main(String[] args) {
+        Goods[] goods = new Goods[]{
+                new Goods("a",1.1),
+                new Goods("b",1.2),
+                new Goods("b",1.3),
+                new Goods("ac",1.1),
+                new Goods("aqq",1.4),
+                new Goods("aw",1.3),
+        };
+        Arrays.sort(goods, new Comparator<Goods>() {
+            @Override
+            public int compare(Goods o1, Goods o2) {
+                if(o1.getPrice() != o2.getPrice()){
+                    int compare = Double.compare(o1.getPrice(), o2.getPrice());
+                    return (int) (o1.getPrice()*10 - o2.getPrice()*10);
+                }else {
+                    return o1.getName().compareTo(o2.getName());
+                }
+            }
+        });
+        System.out.println(Arrays.toString(goods));
+    }
+}

+ 67 - 0
src/main/java/com/sf/javase/day12/comparable/Student.java

@@ -0,0 +1,67 @@
+package com.sf.javase.day12.comparable;
+
+import java.util.Arrays;
+
+public class Student implements Comparable{
+
+    private String name;
+    private int score;
+
+    public Student() {
+    }
+
+    public Student(String name, int score) {
+        this.name = name;
+        this.score = score;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public int getScore() {
+        return score;
+    }
+
+    public void setScore(int score) {
+        this.score = score;
+    }
+
+    @Override
+    public String toString() {
+        return "Student{" +
+                "name='" + name + '\'' +
+                ", score=" + score +
+                '}';
+    }
+
+    @Override
+    public int compareTo(Object o) {
+        if(o instanceof Student){
+            Student student = (Student) o;
+            if(this.score != student.score){
+                int compare = Double.compare(this.score, student.score);
+                return compare;
+            }else {
+                return this.name.compareTo(student.name);
+            }
+        }
+        return 0;
+    }
+
+    public static void main(String[] args) {
+        Student[] students  = new Student[]{
+          new Student("zs",100),
+          new Student("zs",90),
+          new Student("zs",80),
+          new Student("ls",90),
+        };
+        System.out.println(Arrays.toString(students));
+        Arrays.sort(students);
+        System.out.println(Arrays.toString(students));
+    }
+}

BIN
target/classes/com/sf/javase/day11/api/T.class


BIN
target/classes/com/sf/javase/day12/T.class


BIN
target/classes/com/sf/javase/day12/comparable/Goods.class


BIN
target/classes/com/sf/javase/day12/comparable/GoodsTest$1.class


BIN
target/classes/com/sf/javase/day12/comparable/GoodsTest.class


BIN
target/classes/com/sf/javase/day12/comparable/Student.class