java日期时间

java日期时间

1.日期时间类

  1. Date

    在Java 1.0就开始支持,Date类年份的起始选择是1900年,月份的起始从0 开始。如果只是使用日期时间转换还可以使用,其他比较时间大小不方便。

  2. Calendar

    java.util.Calendar类是为了替代Date类而出现的。很不幸的是,Calendar类中也有许多缺点,许多设计缺陷问题并未彻底解决。缺点如下:

    • 月份依旧是从0开始计算(不过,至少Calendar 类拿掉了由1900年开始计算年份这一设计)。
    • Calendar类也是可变的,使用起来不安全。
    • 同时存在Date和Calendar这两个类,容易使程序员产生困惑。到底该使用哪一个类呢?此外,有的特性只在某一个类有提供,比如用于以语言无关方式格式化和解析日期或时间的DateFormat方法就只在Date类里有。
  3. LocalDate

    在java8中用于表示时间的类。LocalDate类的实例是一个不 可变对象,它只提供了简单的日期,并不含当天的时间信息。另外,它也不附带任何与时区相关的信息。

  4. LocalTime

    java8中用于保存时间。

  5. LocalDateTime

    用于保存日期时间

  6. DateFormat

    用户日期格式转换,SimpleDateFormat是其子类

  7. Instant 时间戳

    java.time.Instant类对时间建模的方式,基本上它是以Unix元年时间(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的 秒数进行计算。

  8. ZoneId

    表示时区,ZoneId.systemDefault获取系统的时区

2.日期格式转换

使用SimpleDateFormat进行时间格式的转换

//日期转化为字符串格式
public static void dateToString() {
    Date date = new Date();
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String str = format.format(date);
}
//字符串转化为Date格式
public static void stringToDate() throws ParseException {
    String strDate = "2020-06-08 11:47:02";
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date = format.parse(strDate);
}

3.Calendar与Date转换

//Calender转化为Date
public static void calendarToDate(){
    Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    //重新设置时间
    calendar.setTime(date);
}
//Date转化为Calendar
public static void dateToCalender(){
    Calendar cal = Calendar.getInstance();
    Date time = cal.getTime();
}

4. 时间修改

在原来时间的基础上修改时间。如添加3天,减少一年等。可以使用Calender的add函数,第一个参数为类型,第二个参数为大小,可以为负。如果修改的是天数,DAY_OF_*都可以使用。

public static void changeDate() {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd ");
    Calendar calendar = Calendar.getInstance();
    System.out.println(format.format(calendar.getTime()));//2020-06-07
    calendar.add(Calendar.DAY_OF_WEEK, 11);
    System.out.println(format.format(calendar.getTime()));//2020-06-18
    calendar.add(Calendar.DAY_OF_MONTH, 41);
    System.out.println(format.format(calendar.getTime()));//2020-07-29
    calendar.add(Calendar.YEAR, -1);
    System.out.println(format.format(calendar.getTime()));//2019-07-29
}

5. 时间大小比较

  1. 直接比较时间戳

  2. 使用Date的before或者after比较

6. LocalDateTime与Date

都是使用Instant时间戳类来进行转换,这里需要指定时区

//LocalDateTime转换为Date
public static void localDateTime2Date() {
    LocalDateTime dateTime = LocalDateTime.now();
    Date date = Date.from(dateTime.atZone(ZoneId.systemDefault()).toInstant());
}
//Date转换为LocalDateTime
public static void date2localDateTime(){
    Date date = new Date();
    LocalDateTime dateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    System.out.println(dateTime.toString());
}

参考资料:

Java中的时间和日期处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值