常用类-复习
1.将字符串"2017-08-16"转换为对应的java.sql.Date类的对象。
(使用JDK8之前或JDK8中的API皆可)
SimpleDateFormat	sdf = new SimpleDateFormat("yyyy-MM-dd")
解析:java.util.Date date = sdf.parse("2017-08-16")

DateTimeFormatter.ofPattern dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");

new + 构造器
单例、Calendar.getInstance()

2.解释何为编码?解码?何为日期时间的格式化?解析?
编码:字符串 → 字节
解码:字节 → 字符串

格式化:日期 → 字符串
解析:字符串 → 日期

3.自定义Person类如下,如何实现自然排序(按姓名从小到大排序),
代码说明
class Person implements Comparable{
	private String name;
	private int age;

	public int compareTo(Object obj){
		//... this.name.compareTo(s.name);
	}
}

4.提供定制排序涉及到的接口的实现类对象,并按Person类的年龄从大到小排序
Comparator com = new Comparator(){
	public int compare(Object obj1,Object obj2){
		if(){

		}
	}

}

5.JDK8之前和JDK8中日期、时间相关的类分别有哪些?
java.util.Date、和 java.sql.Date → Instant
SimpleDateForamt → DateTimeFormatter
Calendar → LocalDate、LocalTime、LocalDateTime
package top.qaqaq.java.P495;

import org.junit.jupiter.api.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;

/**
 * @author RichieZhang
 * @create 2022-10-24 下午 1:30
 */
public class DateTimeTest {

    @Test
    public void test1() throws ParseException {
        String str = "2017-08-16";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(str);
        System.out.println(date);
        java.sql.Date date1 = new java.sql.Date(date.getTime());
        System.out.println(date1);


    }

    @Test
    public void test2(){
        Person[] p = new Person[5];
        p[0] = new Person("ZhangSan",18);
        p[1] = new Person("LiSi",23);
        p[2] = new Person("WangWu",18);
        p[3] = new Person("LaoLiu",39);
        p[4] = new Person("QiYe",32);

        Arrays.sort(p);

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

    }

    @Test
    public void test3(){
        Person[] p = new Person[5];

        p[0] = new Person("ZhangSan",18);
        p[1] = new Person("LiSi",23);
        p[2] = new Person("WangWu",18);
        p[3] = new Person("LaoLiu",39);
        p[4] = new Person("QiYe",32);

        Arrays.sort(p,new Comparator(){

            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Person && o2 instanceof Person){
                    Person p1 = (Person) o1;
                    Person p2 = (Person) o2;
                    if (p1.getAge() > p2.getAge()){
                        return 1;
                    }else if (p1.getAge() < p1.getAge()){
                        return -1;
                    }else {
                        return 0;
                    }
                }
                throw new RuntimeException("数据不匹配");
            }
        });

        System.out.println(Arrays.toString(p));
    }
}
package top.qaqaq.java.P495;

/**
 * @author RichieZhang
 * @create 2022-10-24 下午 1:49
 */
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 +
                '}';
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof Person){
            Person p1 = (Person) o;
            return this.name.compareTo(p1.name);
        }
        throw new RuntimeException("数据不匹配");
    }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇