文章目录
day09
StringBuffer的常用方法:
StringBuffer append(xxx):用于字符串拼接
StringBuffer delete(int start,int end):删除指定位置的内容
StringBuffer replace(int start,int end,String str):把[start,end)位置替换为str
StringBuffer insert(int offset,xxx):在指定位置插入xxx
StringBuffer reverse():把当前字符序列逆转
SimpleDateFormat的使用
//按照指定的方式格式化和解析
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String format = sdf.format(new Date());
System.out.println(format);
//解析:要求字符串必须符合simpledateformat识别的格式
Date date = sdf.parse("2022-03-28 01:16:35");
System.out.println(date);
Caldendar类的使用
//获取Calendar的对象
Calendar calendar = Calendar.getInstance();
//get()
System.out.println(calendar.get(Calendar.DAY_OF_YEAR)); //87
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //28
System.out.println(calendar.get(Calendar.DAY_OF_WEEK)); //2
//set()
calendar.set(Calendar.DAY_OF_MONTH, 20);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //20
//add()
calendar.add(Calendar.DAY_OF_MONTH, 5);
System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //25
//getTime() 日历类--->date
System.out.println(calendar.getTime());//Fri Mar 25 13:41:04 CST 2022
//setTime() date--->日历类
calendar.setTime(new Date());
System.out.println(calendar.get(Calendar.DAY_OF_MONTH));//28
LocalDate、LocalTime、LocalDateTime的使用
//now():获取当前的日期,时间,日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate); //2022-03-28
System.out.println(localTime); //14:03:30.772
System.out.println(localDateTime); //2022-03-28T14:03:30.773
//getXxx()
System.out.println(localDateTime.getDayOfMonth());//28
System.out.println(localDateTime.getDayOfYear());// 87
System.out.println(localDateTime.getDayOfWeek());//MONDAY
//体现了不可变性,不会修改原来的日期
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);//2022-03-28
System.out.println(localDate1);//2022-03-22
比较器
Comparable接口的使用
自然排序comparable
- 像String、包装类等实现了Comparable接口,重写了comparaTo(obj)方法
- 重写comparaTo(obj)方法规则:
- 如果当前对象this大于形参对象obj,返回正整数
- 小于,返回负整数
- 等于,放回零
public class CompareTest {
public static void main(String[] args) {
Goods[] goods = new Goods[5];
goods[0] = new Goods("商品1",20);
goods[1] = new Goods("商品2",25);
goods[2] = new Goods("商品3",70);
goods[3] = new Goods("商品4",10);
goods[4] = new Goods("商品5",55);
Arrays.sort(goods);
System.out.println(Arrays.toString(goods));
}
}
class Goods implements Comparable{
private String name;
private int price;
public Goods() {
}
public Goods(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Goods{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
@Override
public int compareTo(Object o) {
if(o instanceof Goods){
Goods goods = (Goods)o;
if(this.price > goods.price){
return 1;
}else if(this.price < goods.price){
return -1;
}else{
return 0;
}
}
throw new RuntimeException("传入的参数数据不符");
}
}
Comparator的使用
Comparator: 自定义排序
重写compare(Object o1,Object o2)方法,比较o1和o2的大小
//从小到大排 使用匿名内部类的方式
Arrays.sort(goods, new Comparator<Goods>() {
@Override
public int compare(Goods o1, Goods o2) {
if(o1.getPrice() > o2.getPrice()){
return -1;
}else if(o1.getPrice() < o2.getPrice()){
return 1;
}else{
return 0;
}
}
});
Comparable接口与Comparator的使用比较:
Comparable接口的方式一旦确定,接口的实现类对象在哪都可以比较,属于一劳永逸。
Comparator属于临时性的使用。
枚举类
- 枚举类的理解:类的对象个数是有限个,确定的。我们称此类为枚举类
- 当需要定义一组常量时,强烈建议使用枚举类。
- 当枚举类中只有一个对象时,可以作为单例模式使用。
使用enum关键字定义枚举类:
默认继承Enum类
enum Season{
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
private final String seasonName;
private final String seasonDesc;
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
}
注解
框架=注解+设计模式+反射
-
生成文档相关的注解
-
在编译时进行格式检查
- @Override:限定重写父类方法,该注解只能用于方法
- @Deprecated:用于表示所修饰的元素(类,方法)过时
- suppersWarnings:抑制编译器警告。
-
跟踪代码依懒性,实现替代配置文件功能
-
jdk提供了4种元注解
元注解:对现有的注解进行解释说明的注解
Retention:指定所修饰的annotation的生命周期:SOURCE\CLASS(默认)\RUNTIME
只有声明为RUNTIME声明周期的注解,才能通过反射获取。
Target:用于指定被修饰的Annotation能用于哪些元素
Documented:表示所修饰的注解将在javacdoc被留下来
Inherited:被它修饰的Annotation将具有继承性