Java中的日期的获取与格式转换

本文详细介绍了Java中处理日期和时间的各种API,包括Java8之前使用的Date、Calendar、SimpleDateFormat等类,以及Java8引入的更为现代的LocalDateTime和DateTimeFormatter类。通过具体的代码示例展示了如何设置、获取及格式化日期时间。

Java8之前的日期相关类

Calendar、SimpleDateFormat、Date
public class DateTest {
    public static void main(String[] args) {
        //1.使用Calendar对象设置具体时间
        //Calendar对象的引用
        Calendar calendar=Calendar.getInstance();
        //设置时间为2008年8月1日 6点10分10秒
        calendar.set(2008,8-1,1,6,10,10);
        //从calendar对象获得Date
        Date d=calendar.getTime();
        System.out.println(d);
        //使用SimpleDateFormat类调整时间格式
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(d);
        System.out.println(format);
    }
}

输出结果:
在这里插入图片描述

Date类

java.util.Date类主要用于描述特定的瞬间,也就是年月日时分秒,可以精确到毫秒。

SimpleDateFormat类

java.text.SimpleDateFormat类主要用于实现日期和文本之间的转换。

方法功能
SimpleDateFormat(String pattern)根据参数指定的模式来构造对象,模式主要有: y-年 M-月 d-日 H-时 m-分 s-秒
final String format(Date date)用于将日期类型转换为文本类型
Date parse(String source)用于将文本类型转换为日期类型
Calendar类

java.util.Calender类主要用于描述特定的瞬间,取代Date类中的过时方法实现全球化。
该类是个抽象类,因此不能实例化对象,其具体子类针对不同国家的日历系统,其中应用最广泛的是GregorianCalendar(格里高利历),对应世界上绝大多数国家/地区使用的标准日历系统。

方法功能
static Calendar getInstance()用于获取Calendar类型的引用
void set(int year, int month, int date, int hourOfDay, int minute, int second)用于设置年月日时分秒信息
Date getTime()用于将Calendar类型转换为Date类型
void set(int field, int value)设置指定字段的数值
void add(int field, int amount)向指定字段增加数值

Java8之后的日期相关类

LocalDateTime、DateTimeFormatter
public class DateTest {
    public static void main(String[] args) {
        //使用LocalDateTime获取当前日期、时间
        LocalDateTime now= LocalDateTime.now();
        System.out.println("当前日期和时间是:"+now);
        //使用LocalDateTime获取设定的时间
        LocalDateTime setTime=LocalDateTime.of(2008,8,1,10,10,10);
        System.out.println("设定的时间是:"+setTime);
        //转换格式为年-月-日 时:分:秒
        DateTimeFormatter sdf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //alt+enter声明返回值,使用format方法将Date转为String
        String formatStr = sdf.format(setTime);
        System.out.println("格式化后的时间:"+formatStr);
    }
}

输出结果:
在这里插入图片描述

LocalDateTime类

java.time.LocalDateTime类主要用于描述ISO-8601日历系统中没有时区的日期时间,如2007-12-03T10:15:30。

方法功能
static LocalDateTime now()从默认时区的系统时间中获取当前日期时间
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)根据参数指定的年月日时分秒信息来设置日期时间
DateTimeFormatter类

java.time.format.DateTimeFormatter类主要用于格式化和解析日期。

方法功能
static DateTimeFormatter ofPattern(String pattern)根据参数指定的模式来获取对象
String format(TemporalAccessor temporal)将参数指定日期时间转换为字符串
TemporalAccessor parse(CharSequence text)将参数指定字符串转换为日期时间

其中
在这里插入图片描述
TemporalAccessor是一个接口,而LocalDateTime类已对其进行了实现,所以通过多态的方法形参可以直接传入LocalDateTime的对象。

评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值