Java日期与时间类原理解析
这篇文章主要介绍了Java日期与时间类原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
基础知识
- 日期:类似于2018-12-12
- 时间:类似于2018-12-1212:12:12
- 时刻:类似于2018-12-1212:12:12
- 地区:计算机中的Locale,如zh_CN,en_US等,影响着对于日期,时间,货币等格式的显示
- EpochTime:从1970年1月1日UTC+00:00到现在的秒数(不是毫秒数),大致可认为与TimeStamp等价
- 时区:全球共24个,伦敦(GMT+00:00)的是标准时区
- GMT与UTC大致可以认为是等价的,GMT与UTC表示的时间不受夏令时影响,而以地区(如America/New_York)表示的时间受夏令时影响
- 日期在计算机中都存储为EpochTime(即TimeStamp),所以全球的计算机都是相同的,但是因为计算机地区等设置的不同,所以显示为不同的时间
- Date与Calendar
Date的用法
//获取一个Date对象,即表示现在的时刻
Datedate=newDate();
//获取TimeStamp
longtimestamp=date.getTime();
//timestamp生成Date
date=newDate(timestamp);
//转换为默认格式的字符串
StringdateString=date.toString();
//按照指定格式转换
SimpleDateFormatsimpleDateFormat=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
dateString=simpleDateFormat.format(date);
//按照指定格式字符串解析为Date
date=simpleDateFormat.parse(dateString);
Calendar的用法
//获取一个Calendar Calendarcalendar=Calendar.getInstance(); //获取今天是本月的第几天 intday=calendar.get(Calendar.DAY_OF_MONTH); //设置为指定时间 calendar.set(Calendar.DAY_OF_MONTH,1); //转换为Date Datedate=calendar.getTime(); //转换为timestamp longtimestamp=calendar.getTimeInMillis();
LocalDateTime的用法
//获取一个LocalDateTime
LocalDateTimelocalDateTime=LocalDateTime.now();
//获取一个指定的LocalDateTime,需要注意月份从1开始而非0,所以12月用12
localDateTime=LocalDateTime.of(2018,12,12,21,36,36);
//LocalDateTime格式化为指定格式
DateTimeFormatterdateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-ddHH:mm:ss");
StringlocalDateTimeString=dateTimeFormatter.format(localDateTime);
//按照指定格式解析LocalDateTime,需要注意这里DateTimeFormatter是作为参数参入
localDateTime=LocalDateTime.parse(localDateTimeString,dateTimeFormatter);
//减去一天,需要注意该对象是不可变的
localDateTime=localDateTime.minusDays(1);
//小时设置为12点
localDateTime=localDateTime.withHour(12);
//设置为本月的第一天
localDateTime=localDateTime.withDayOfMonth(1);
//设置为本月的最后一天,注意这里是TemporalAdjusters而不是TemporalAdjuster
localDateTime=localDateTime.with(TemporalAdjusters.lastDayOfMonth());
//判断两个LocalDateTime的先后
booleanbefore=localDateTime.isBefore(LocalDateTime.now());
//计算两个LocalDate相差的天数
Periodperiod=LocalDate.now().until(LocalDate.of(2019,1,1));
longdays=period.getDays();
//计算两个LocalDateTime相差的天数,可能为负数
days=localDateTime.until(LocalDateTime.now(),ChronoUnit.DAYS);
//距离EpochTime的天数
days=LocalDate.now().toEpochDay();
//LocalDateTime无法与timestamp进行转换,因为它没有时区的概念
ZonedDateTime相当于LocalDateTime加上了一个ZoneId
Instant可以理解为TimeStamp,也就是说相当于一个Long的封装类
//从LocalDateTime获取ZonedDateTime
ZonedDateTimezonedDateTime=LocalDateTime.now().atZone(ZoneId.of("Asia/Shanghai"));
//从Instant获取ZonedDateTime
zonedDateTime=Instant.now().atZone(ZoneId.systemDefault());
//获取指定的Instant,需要注意System.currentTimeMillis()返回的是毫秒,而TimeStamp是秒
Instantinstant=Instant.ofEpochSecond(System.currentTimeMillis()/1000);
//TimeStamp获取LocalDateTime
LocalDateTimelocalDateTime=LocalDateTime.ofEpochSecond(
//获取当前的时间戳(timestamp)
instant.getEpochSecond(),
//毫秒
0,
//获取系统的ZoneOffset,即时区偏移
OffsetDateTime.now().getOffset());
//星期相关的操作
LocalDatenow=LocalDate.now();
LocalDatesundayDate=now.with(DayOfWeek.SUNDAY);
intsundayValue=sundayDate.getDayOfWeek().getValue();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。