Java日期相关类

System类

方法声明功能介绍
static long currentTimeMillis()返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差
		long l = System.currentTimeMillis();
		System.out.println(l);

Date类

方法声明功能介绍
Date()使用无参的方式构造对象,也就是当前系统时间
Date(long date)根据参数指定毫秒数构造对象, 参数为距离1970年1月1日0时0分0秒的毫秒数
long getTime()获取调用对象距离1970年1月1日0时0分0秒的毫秒数
void setTime(longtime)设置调用对象为距离基准时间time毫秒的时间点
        // 使用无参的方式构造对象,也就是当前系统时间
        Date date = new Date();
        System.out.println(date); // Sat Feb 19 14:18:09 GMT+08:00 2022
        // 根据参数指定毫秒数构造对象
        Date date1 = new Date(14562586);
        System.out.println(date1); // Thu Jan 01 12:02:42 GMT+08:00 1970
        // 获取调用对象距离1970年1月1日0时0分0秒的毫秒数
        System.out.println(date.getTime()); // 1645251534339
        // 设置调用对象为距离基准时间time毫秒的时间点
        date.setTime(164525);
        System.out.println(date); // Thu Jan 01 08:02:44 GMT+08:00 1970

SimpleDateFormat类

方法声明功能介绍
SimpleDateFormat()使用无参方式构造对象
SimpleDateFormat(Stringpattern)根据参数指定的模式来构造对象,模式主要有: y-年 M-月 d-日 H-时 m-分 s-秒
final String format(Date date)用于将日期类型转换为文本类型
Date parse(String source)用于将文本类型转换为日期类型
public class SimpleDateFormatTest {
    public static void main(String[] args) {
        // 获取当前时间
        Date date = new Date();
        // 使用指定格式构造对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年-MM月-dd日 HH时:mm分:ss秒");
        // 将日期转换为文本类型
        String format = simpleDateFormat.format(date);
        // 打印转换后的文本日期
        System.out.println(format); // 2022年-02月-19日 14时:33分:18秒
        // 将文本类型转换为日期类型
        try {
            Date parse = simpleDateFormat.parse(format);
            System.out.println(parse); // Sat Feb 19 14:31:39 GMT+08:00 2022
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Calendar类

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

方法声明功能介绍
static Calendar getInstance()用于获取Calendar类型的引用
void set(int year, int month, int date, int hourOfDay, intminute, int second)用于设置年月日时分秒信息
Date getTime()用于将Calendar类型转换为Date类型
void set(int field, int value)设置指定字段的数值
void add(int field, int amount)向指定字段增加数值
public class CalendarTest {
    public static void main(String[] args) {
        // 使用过时的方法按照指定的年月日时分秒来构造对象
        Date date = new Date(2008 - 1900, Calendar.AUGUST, 8, 20, 8, 8);
        // 设置日期格式并打印
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年 MM月 dd日 HH时 mm分 ss秒");
        String format = simpleDateFormat.format(date);
        System.out.println(format); // 2008年 08月 08日 20时 08分 08秒

        // 使用取代的方法来设置指定的年月日时分秒来构造对象
        // 考点:Calendar是抽象类,为什么可以获取他的对象?
        // 由源码可知,返回的是其子类的对象。(多态)
        Calendar instance = Calendar.getInstance();
        instance.set(2008, Calendar.AUGUST, 8, 20, 8, 8);
        // 用于将Calendar类型转换为Date类型
        Date time = instance.getTime();
        String format1 = simpleDateFormat.format(time);
        System.out.println(format1); // 2008年 08月 08日 20时 08分 08秒
        
        // 设置指定字段的数值
        instance.set(Calendar.YEAR, 2022);
        Date time1 = instance.getTime();
        System.out.println(simpleDateFormat.format(time1)); // 2022年 08月 08日 20时 08分 08秒
        // 向指定字段增加数值
        instance.add(Calendar.MONTH, 4);
        Date time2 = instance.getTime();
        System.out.println(simpleDateFormat.format(time2)); // 2022年 12月 08日 20时 08分 08秒
    }
}

多态的使用场合:
通过方法的参数传递形成多态;

public static void draw(Shape s){
 s.show();
}
draw(new Rect(1, 2, 3, 4));

在方法体中直接使用多态的语法格式

Account acc = new FixedAccount();

通过方法的返回值类型形成多态

Calender getInstance(){
 return new GregorianCalendar(zone, aLocale);
 }

Java8日期类

1、LocalDate类

java.time.LocalDate类主要用于描述年-月-日格式的日期信息,该类不表示时间和时区信息。

方法声明功能介绍
static LocalDate now()在默认时区中从系统时钟获取当前日期
		// 在默认时区中从系统时钟获取当前日期
        LocalDate dateNow = LocalDate.now();
        System.out.println(dateNow); // 2022-02-19
        System.out.println(LocalDate.of(2008, 8, 8)); // 2008-08-08
2、LocalTime类

java.time.LocalTime 类主要用于描述时间信息,可以描述时分秒以及纳秒。

方法声明功能介绍
static LocalTime now()从默认时区的系统时间中获取当前时间
static LocalTime now(ZoneId zone)获取指定时区的当前时间
        // 从默认时区的系统时间中获取当前时间
        LocalTime timeNow = LocalTime.now();
        System.out.println(timeNow); // 15:46:19.560200900
3、 LocalDateTime类

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

方法声明功能介绍
static LocalDateTime now()从默认时区的系统时间中获取当前日期时间
static LocalDateTime of(int year, int month, intdayOfMonth, int hour, int minute, int second)根据参数指定的年月日时秒信息来设置日期时间
int getYear()获取年份字段的数值
int getMonthValue()获取1到12之间的月份字段
int getDayOfMonth()获取日期字段
int getHour()获取小时数
int getMinute()获取分钟数
int getSecond()获取秒数
LocalDateTime withYear(int year)设置为参数指定的年
LocalDateTime withMonth(int month)设置为参数指定的月
LocalDateTime withDayOfMonth(int dayOfMonth)设置为参数指定的日
LocalDateTime withHour(int hour)设置为参数指定的时
LocalDateTime withMinute(int minute)设置为参数指定的分
LocalDateTime withSecond(int second)设置为参数指定的秒
LocalDateTime plusYears(long years)加上参数指定的年
LocalDateTime plusMonths(long months)加上参数指定的月
LocalDateTime plusDays(long days)加上参数指定的日
LocalDateTime plusHours(long hours)加上参数指定的时
LocalDateTime plusMinutes(long minutes)加上参数指定的分
LocalDateTime plusSeconds(long seconds)加上参数指定的秒
LocalDateTime minusYears(long years)减去参数指定的年
LocalDateTime minusMonths(long months)减去参数指定的月
LocalDateTime minusDays(long days)减去参数指定的日
LocalDateTime minusHours(long hours)减去参数指定的时
LocalDateTime minusMinutes(long minutes)减去参数指定的分
LocalDateTime minusSeconds(long seconds)减去参数指定的秒
        // 从默认时区的系统时间中获取当前日期时间
        LocalDateTime dateTImeNow = LocalDateTime.now();
        System.out.println(dateTImeNow); // 2022-02-19T15:48:23.495659
        // 根据参数指定的年月日时秒信息来设置日期时间
        LocalDateTime of = LocalDateTime.of(2008, 8, 8, 20, 8, 8);
        System.out.println(of); // 2008-08-08T20:08:08
4、Instant类

java.time.Instant类主要用于描述瞬间的时间点信息。

方法声明功能介绍
static Instant now()从系统时钟上获取当前时间
static Instant now()从系统时钟上获取当前时间
OffsetDateTime atOffset(ZoneOffset offset)将此瞬间与偏移量组合以创建偏移日期时间
static Instant ofEpochMilli(long epochMilli)根据参数指定的毫秒数来构造对象,参数为距离1970年1月1 日0时0分0秒的毫秒数
long toEpochMilli()获取距离1970年1月1日0时0分0秒的毫秒数
5、DateTimeFormatter类

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

方法声明功能介绍
static DateTimeFormatter ofPattern(String pattern)根据参数指定的模式来获取对象
String format(TemporalAccessor temporal)将参数指定日期时间转换为字符串
TemporalAccessor parse(CharSequence text)将参数指定字符串转换为日期时间
public class DateTimeFormatterTest {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        // 根据参数指定的模式来获取对象
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
        // 将参数指定日期时间转换为字符串
        String format = dateTimeFormatter.format(now);
        System.out.println(format); // 2022年02月19日 16时14分14秒
        // 将参数指定字符串转换为日期时间
        // TemporalAccessor是接口,LocalDateTime是其实现类
        TemporalAccessor parse = dateTimeFormatter.parse(format);
        System.out.println(parse); // {},ISO resolved to 2022-02-19T16:15:21
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值