
圣诞节快乐
在计算机中,应该如何表示日期和时间呢?
我们经常看到的日期和时间表示方式如下:
-
2019-11-20 0:15:00 GMT+00:00
-
2019年11月20日8:15:00
-
11/19/2019 19:15:00 America/New_York
如果直接以字符串的形式存储,那么不同的格式,不同的语言会让表示方式非常繁琐。
在理解日期和时间的表示方式之前,我们先要理解数据的存储和展示。
当我们定义一个整型变量并赋值时:
int n = 123400;
编译器会把上述字符串(程序源码就是一个字符串)编译成字节码。在程序的运行期,变量n指向的内存实际上是一个4字节区域:
│00│01│e2│08│
注意到计算机内存除了二进制的0/1外没有其他任何格式。上述十六机制是为了简化表示。
当我们用System.out.println(n)打印这个整数的时候,实际上println()这个方法在内部把int类型转换成String类型,然后打印出字符串123400。
它们实际上是数据的展示格式,分别按英国时区、中国时区、纽约时区对同一个时刻进行展示。而这个“同一个时刻”在计算机中存储的本质上只是一个整数,我们称它为Epoch Time。
Epoch Time是计算从1970年1月1日零点(格林威治时区/GMT+00:00)到现在所经历的秒数,例如:
1574208900表示从从1970年1月1日零点GMT时区到该时刻一共经历了1574208900秒,换算成伦敦、北京和纽约时间分别是:
1574208900 = 北京时间2019-11-20 8:15:00
= 伦敦时间2019-11-20 0:15:00
= 纽约时间2019-11-19 19:15:00

-
古老的Date类 -
处理年月日的年历类Calendar -
格式化字符串和日期对象的DateFormat格式转换类 -
好用的SimpleDateFormat实现类 -
Joda-Time库
一、古老的Date类Date这个类自jdk1.0开始就被设计出来, 从它的源代码中我们也是可以看出来,Date类曾经扮演过很重要的角色,jdk早期的版本中有关日期和时间的操作几乎都是由Date类完成的,下面我们一起看看它的源码:
private transient long fastTime;
二、处理年月日的年历类Calendar以前我们是可以使用Date来处理日期年月日的,但是由于该类不支持国际化等原因,现在其中大部分方法被注解,不再推荐使用,现在的Date类更像是代表着某一个时刻的对象,而处理年月日的这种转换则完全交给了Calendar类处理。所以Calendar目前是日期时间处理中的核心类,接下来我们看看其中源码:
//和Date一样封装了毫秒属性
protected long time;
protected int fields[];
//封装了十七个静态常量
public final static int ERA = 0;
public final static int YEAR = 1;
public final static int MONTH = 2;
public final static int WEEK_OF_YEAR = 3;
.........
public final static int DST_OFFSET = 16;
三、DateFormat处理格式转换DateFormat是一个抽象类,该类主要用于实现Date对象和字符串之间相互转换, 涉及到两个转换的方法:
//将Date类型转换为String类型
public final String format(Date date)
//将String类型转换Date类型
public Date parse(String source)
四、优秀的实现类SimpleDateFormatSimpleDateFormat是DateFormat的一个优秀的实现类,它增强了一个重要的性质。它允许自定义格式输出模板。构造SimpleDateFormat实例的时候,可以传入一个pattern作为输出模板。看个例子:
public static void main(String[] args) {
Calendar c = Calendar.getInstance();
SimpleDateFormat sm = new SimpleDateFormat("yyyy年MM月dd日 E HH时mm分ss秒");
System.out.println(sm.format(c.getTime()));
输出结果:
2017年05月29日 星期一 20时25分31秒
上述的代码中,字符串yyyy年MM月dd日 E HH时mm分ss秒就是一个模板pattern,其中:
-
yyyy表示使用四位数字输出年份 -
MM表示使用两位数字表示月份 -
dd表示使用两位数字表示日 -
E表示星期几 -
HH表示使用两位数字表示小时(24以内) -
mm和ss分别表示分钟和秒数
五、开源第三方库Joda-TimeJoda-Time库中的内容还是很多的,我们简单了解下基本的使用即可,至于深入学习该库,大家可以自行尝试,此处限于篇幅,不再赘述。在该库中DateTime相当于jdk中Calendar,主要完成对日期年月日的计算操作。首先我们通过简单易理解的方式创建DateTime的实例对象:
//2017-05-29 21:40
DateTime dt = new DateTime(2017,5,29,21,40);
//2017-05-29 21:40 50秒
DateTime dt2 = new DateTime(2017,5,29,21,40,50);
-
Java的常用的时间日期类:
Java8以前在java.util这个包里,主要包括Date、Calendar和TimeZone这几个类;
Java8中推出了全新的日期时间类在java.time这个包里面,使用起来更加简单和安全,常用的有以下几个
LocalDate:本地日期,只包括日期
LocalTime:本地时间,只包括时间
LocalDateTime:本地日期时间,包括日期和时间
ZonedDateTime:完整的本地日期时间,包括日期,时间,和时区
代码片段:
获取当前日期
LocalDate localDate = LocalDate.now();
System.out.println("当前日期为:" + localDate);//当前日期为:2021-12-24
LocalDate提供了获取年、月、日的快捷方法
System.out.println("当前年为:" + localDate.getYear());//当前年为:2021
System.out.println("当前月为:" + localDate.getMonth());//当前月为:JUNE
System.out.println("当前月为:" + localDate.getMonthValue());//当前月为:6
System.out.println("当前日为当月的第:" + localDate.getDayOfMonth() + "天");//当前日为当月的第:19天
System.out.println("当前日为当周的:" + localDate.getDayOfWeek());//当前日为当周的:FRIDAY
System.out.println("当前日为当年的第:" + localDate.getDayOfYear() + "天");//当前日为当年的第:171天
指定日期,这里的月对应的就是实际的月
LocalDate zdDate = LocalDate.of(2020, 06, 18);
System.out.println("指定日期为:" + zdDate);//指定日期为:2021-12-24
判断两个日期是否相等,直接使用equals
boolean flag = localDate.equals(zdDate);
System.out.println("两个日期是否相等" + flag);//两个日期是否相等false
boolean isEqual = localDate.isEqual(zdDate);
System.out.println("两个日期是否相等" + flag);//两个日期是否相等false
判断是否为闰年
System.out.println("是否为闰年:" + localDate.isLeapYear());//是否为闰年:true
只需要年月、月日,获取方式有多种
YearMonth yearMonth = YearMonth.now();
System.out.println("当前年月为:" + yearMonth);//当前年月为:2020-06
YearMonth currentYearMonth1 = YearMonth.from(localDate);
System.out.println("当前年月为:" + currentYearMonth1);//当前年月为:2020-06
YearMonth zdYearMonth = YearMonth.of(zdDate.getYear(), zdDate.getMonth());
System.out.println("指定年月为:" + zdYearMonth);//指定年月为:2020-06
返回当月天数
System.out.println("当月天数为:" + yearMonth.lengthOfMonth());//当月天数为:30
获取月日
MonthDay monthDay = MonthDay.now();
System.out.println("当前月日为:" + monthDay);//当前月日为:--06-19
MonthDay currentMonthDay = MonthDay.from(localDate);
System.out.println("当前月日为:" + currentMonthDay);//当前月日为:--06-19
MonthDay zdMonthDay = MonthDay.of(zdDate.getMonth(), zdDate.getDayOfMonth());
System.out.println("指定月日为:" + zdMonthDay);//指定月日为:--06-18
日期的加减
通过LocalDate的plus方法增加年、月、周、天,也可以使用plus后直接跟年(plusYears)、月(plusMonths)、周(plusWeeks)、日(plusDays);minus方法减少年、月、周、天,也可以使用minus后直接跟年(minusYears)、月(minusMonths)、周(minusWeeks)、日(minusDays)
LocalDate nextWeek = localDate.plus(1, ChronoUnit.WEEKS);
System.out.println("一周后日期为:" + nextWeek);//一周后日期为:2020-06-26
LocalDate newWeek = localDate.plusWeeks(1);
System.out.println("一周后日期为:" + newWeek);//一周后日期为:2020-06-26
LocalDate oldWeek = localDate.minus(1, ChronoUnit.WEEKS);
System.out.println("一周前日期为:" + oldWeek);//一周前日期为:2020-06-12
获取当前时间,默认时间格式为hh:mm:ss:nnn
LocalTime localTime = LocalTime.now();
System.out.println("当前时间为:" + localTime);//当前时间为:16:17:35.717
LocalTime zdLocalTime = LocalTime.of(14, 40, 30);
System.out.println("指定时间为:" + zdLocalTime);//指定时间为:14:40:30
时间的加减
通过LocalTime的plus方法增加时、分、秒,也可以使用plus后直接跟时(plusHours)、分(plusMinutes)、秒(plusSeconds);minus方法减少时、分、秒,也可以使用minus后直接跟时(minusHours)、分(minusMinutes)、秒(minusSeconds)
LocalTime newLocalTimeHour = localTime.plusHours(2);
System.out.println("2个小时后时间为:" + newLocalTimeHour);//2个小时后时间为:18:17:35.717
LocalTime oldLocalTimeHour = localTime.minusHours(2);
System.out.println("2个小时前时间为:" + oldLocalTimeHour);//2个小时前时间为:14:17:35.717
LocalTime newLocalTimeMinute = localTime.plusMinutes(20);
System.out.println("20分钟后时间为:" + newLocalTimeMinute);//20分钟后时间为:16:37:35.717
LocalTime oldLocalTimeMinute = localTime.minusMinutes(20);
System.out.println("20分钟前时间为:" + oldLocalTimeMinute);//20分钟前时间为:15:57:35.717
使用Duration计算两个时间间隔多少小时、分钟、秒
long minutes = Duration.between(zdLocalTime, localTime).toMinutes();
System.out.println("两个时间间隔为:" + minutes);//两个时间间隔为:97
使用Duration计算两个日期间隔天数
LocalDate localDate = LocalDate.now();
LocalDate agoDate = LocalDate.of(2020, 12, 20);
long days = Duration.between(agoDate.atStartOfDay(), localDate.atStartOfDay()).toDays();
使用ChronoUnit计算两个日期间隔天数
LocalDate localDate = LocalDate.now();
LocalDate agoDate = LocalDate.of(2020, 12, 20);
long days = ChronoUnit.DAYS.between(agoDate, localDate);
使用LocalDate计算两个日期间隔天数
LocalDate localDate = LocalDate.now();
LocalDate agoDate = LocalDate.of(2020, 12, 20);
long days = localDate.toEpochDay() - agoDate.toEpochDay();
使用Period计算两个日期间隔多少年、月、天
long days = Period.between(zdDate, localDate).getDays();
System.out.println("两个日期间隔为:" + days);//两个日期间隔为:1
Instant时间戳,默认使用 UTC 时区(世界协调时间)
Instant instant = Instant.now();
System.out.println(instant);//2020-06-19T08:17:35.718Z
System.out.println("时间戳秒" + instant.getEpochSecond());//时间戳秒1592554655
System.out.println("时间戳毫秒" + instant.toEpochMilli());//时间戳毫秒1592554655718
System.out.println("在1970-01-01T00:00:00上加上20秒为:" + Instant.ofEpochSecond(20));//在1970-01-01T00:00:00上加上20秒为:1970-01-01T00:00:20Z
System.out.println("在1970-01-01T00:00:00上加上20毫秒为:" + Instant.ofEpochMilli(20));//在1970-01-01T00:00:00上加上20毫秒为:1970-01-01T00:00:00.020Z
使用LocalDateTime获取时间戳
LocalDateTime localDateTime = LocalDateTime.now();
long second = localDateTime.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
System.out.println("当前日期时间戳(精确到秒)为:" + second);
long milli = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
System.out.println("当前日期时间戳(精确到毫秒)为:" + milli);
将时间戳转化为LocalDateTime
LocalDateTime localDateTime1 = LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
System.out.println("将时间戳(精确到秒)转化为时间为:" + localDateTime1);
LocalDateTime localDateTime2 = LocalDateTime.ofInstant(Instant.ofEpochSecond(second), ZoneId.systemDefault());
System.out.println("将时间戳(精确到秒)转化为时间为:" + localDateTime2);
LocalDateTime localDateTime3 = LocalDateTime.ofInstant(Instant.ofEpochMilli(milli), ZoneId.systemDefault());
System.out.println("将时间戳(精确到毫秒)转化为时间为:" + localDateTime3);
时区偏移量计算
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println("偏移8小时后为:" + offsetDateTime);//偏移8小时后为:2020-06-19T16:30:55.011+08:00
Clock时钟类获取当前时间戳
Clock clock = Clock.systemUTC();
System.out.println("Clock:" + clock);//Clock:SystemClock[Z]
System.out.println("Clock时间戳为" + clock.millis());//Clock时间戳为1592555455013
Clock defaultClock = Clock.systemDefaultZone();
System.out.println(defaultClock);//SystemClock[Asia/Shanghai]
System.out.println("Clock系统时间戳为" + defaultClock.millis());//Clock系统时间戳为1592555455013
判断日期早于或晚于
boolean isBeforeDate = localDate.isBefore(zdDate);
System.out.println("当前日期是否早于指定日期" + isBeforeDate);//当前日期是否早于指定日期false
boolean isAfterDate = localDate.isAfter(zdDate);
System.out.println("当前日期是否晚于指定日期" + isAfterDate);//当前日期是否晚于指定日期true
判断时间早于或晚于
boolean isBeforeTime = localTime.isBefore(zdLocalTime);
System.out.println("当前时间是否早于指定时间" + isBeforeTime);//当前时间是否早于指定时间false
boolean isAfterTime = localTime.isAfter(zdLocalTime);
System.out.println("当前时间是否晚于指定时间" + isAfterTime);//当前时间是否晚于指定时间true
获取当前日期时间
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("当前日期时间为:" + localDateTime);//当前日期时间为:2020-06-19T16:30:55.013
LocalDateTime zdLocalDateTime1 = LocalDateTime.of(2020, 06, 19, 15, 20, 30);
System.out.println("指定日期时间为:" + zdLocalDateTime1);//指定日期时间为:2020-06-19T15:20:30
LocalDateTime zdLocalDateTime2 = LocalDateTime.of(zdDate, zdLocalTime);
System.out.println("指定日期时间为:" + zdLocalDateTime2);//指定日期时间为:2020-06-18T14:40:30
LocalDateTime nextLocalDateTime = localDateTime.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println("下一个周日的日期时间为:" + nextLocalDateTime);//下一个周日的日期时间为:2020-06-21T16:30:55.013
带时区的日期时间
ZoneId america = ZoneId.of("America/New_York");
ZonedDateTime zonedDateTimeNewYork = ZonedDateTime.of(localDateTime, america);
System.out.println("指定时区日期时间为:" + zonedDateTimeNewYork);//指定时区日期时间为:2020-06-19T16:30:55.013-04:00[America/New_York]
格式化日期时间
String formatLocalDate = localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
System.out.println("格式化日期为:" + formatLocalDate);//格式化日期为:20200619
String formatLocalDateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
System.out.println("格式化日期时间为:" + formatLocalDateTime);//格式化日期时间为:20200619163055
将字符串转换为日期
LocalDate parseLocalDate = LocalDate.parse("2020-06-19", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println("将指定的日期字符串转换为日期为:" + parseLocalDate);//将指定的日期字符串转换为日期为:2020-06-19
Date转换为LocalDateTime、LocalDate
Date date = new Date();
Instant instant = date.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
LocalDateTime localDateTime1 = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
LocalDate localDate1 = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate();
LocalDate localDate2 = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
LocalDateTime、LocalDate转换为Date
LocalDateTime localDateTime = LocalDateTime.now();
Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
LocalDate localDate = LocalDate.now();
Date date1 = Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
总结:
1.
Java8以前在java.util这个包里,主要包括Date、Calendar和TimeZone这几个类;
Java8中推出了全新的日期时间类在java.time这个包里面,使用起来更加简单和安全,常用的有以下几个
LocalDate:本地日期,只包括日期
LocalTime:本地时间,只包括时间
LocalDateTime:本地日期时间,包括日期和时间
ZonedDateTime:完整的本地日期时间,包括日期,时间,和时区
2.
古老的Date类
本篇文章来源于微信公众号: 小刘Java之路
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/11197.html