String类 时间类 与格式化时间

String和StringBuilder和StringBuffer的区别

  • 数据修改:

    • String是不可变的,即一旦创建就不能修改

    • StringBuffer、StringBuilder是可变,可以对字符串进行修改

  • 线程安全:

    • 由于String中的数据是不可变的,线程安全

    • StringBuffer由于方法上加了synchronize锁,那么就是线程安全的

    • StringBuilder线程不安全

  • 性能:

    • 由于 String 类的不可变性,每次操作都会创建新的字符串对象,因此在频繁的字符串操作场景中,性能较差

    • StringBuffer 的效率相对较低,因为它保证了线程安全,所以在多线程环境下使用

    • StringBuilder 比 String 效率更高,适合在单线程环境下进行大量字符串操作。

  • 适用场景:

    • 如果需要频繁修改或拼接字符串,并且在单线程环境下操作,可以选择 StringBuilder

    • 如果在多线程环境下操作,可以选择 StringBuffer 来保证线程安全

    • 如果字符串不需要修改,可以选择 String 类,由于其不可变性可以提供更好的安全性和性能

  1. String类的方法及其描述

    • indexof() 指定字符串第一次出现索引的位置

    • Lengt() 长度

    • concat() 拼接字符串末尾

    • charAt() 返回指定索引的值

    • subString() 截取

    • toCharArray() 转换为字符数组

    • getBytes() 转化为字节数组

    • replace() 替换

    • split() 分割

    • toUpperCase() toLowerCase() 转换为大小写

  2. StringBuilder、StringBuffer类的方法及其描述

    • append() 追加

    • setCharAt() 指定索引设置值

    • insert() 指定索引插入值

    • delete() 指定索引删除值

    • toString()

    • subString()

    • indexOf() 指定字符串第一次出现索引的位置

    • replace() 替换

时间类

Date 类是用于表示日期和时间的类。它位于 java.util 包中,提供了一系列方法用于处理日期和时间的操作

  • Date 类的概念:Date 类是一个包含时间戳的对象,用于表示特定的时刻,即从标准基准时间(称为“纪元”)开始计算的毫秒数。可通过创建 Date 对象来表示一个具体的日期和时间

时间格式化

public static void main(String[] args) throws Exception{
    Date date = new Date();
    System.out.println("date = " + date);
    DateFormat dateInstance = DateFormat.getDateInstance();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    String format = simpleDateFormat.format(new Date());
    System.out.println("format = " + format);
    
    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format1 = simpleDateFormat1.format(new Date());
    System.out.println("format1 = " + format1);
    
    SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat();
    Date parse = simpleDateFormat2.parse(format1);
    System.out.println("parse = " + parse);
}

日历

  • calendar

    public static void main(Sting[] args){
        Calendar instance = Calendar.getInstance();
        Date time = instance.getTime();
        System.out.println("time = " + time);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼粮爱编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值