脚本宝典收集整理的这篇文章主要介绍了Java时间类总结,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
java.util.Date
// The class Date rePResents a sPEcific instant in time, wITh millisecond precision.
// 语句 Date date = new Date(); System.out.println(date); //输出结果 Sat Feb 03 14:48:47 CST 2018
java.SQL.Date
包含年月日,时分秒都被设置为0,之所以这样设计是为了适应SQL中的DATE
类型。
官方解释:
// A thin wrapper around a millisecond value that allows JDBC to identify this as an SQL DATE value. A milliseconds value represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT. // To conform with the definition of SQL DATE, the millisecond values wrapped by a java.sql.Date instance must be 'normalized' by setting the hours, minutes, seconds, and milliseconds to zero in the particular time zone with which the instance is associated.
注意,虽然说这个类是使用年月日的,但是初始化的时候,需要一个long类型的参数,这个参数代表着January 1, 1970, 00:00:00 GMT
到某个时间的毫秒数。如果是当前时间的话,可以用System.currentTimeMillis()
或者new Date().getTime()
获取。
// 语句 java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis()); System.out.println(sqlDate); // 输出结果 2018-02-03
java.sql.Time
包含时分秒,这个也是为了SQL中的TIME
类型而出现的。
// 语句 Time time = new Time(System.currentTimeMillis()); System.out.println(time); // 输出结果 15:07:35
java.sql.Timestamp
时间戳,适配于SQL中的TIMESTAMP
类型而出现的,精确到纳秒级别。
格式化输出:java.text.SimpleDateFormat
这个类提供时间的各种格式化输出和将字符串转换为时间类,简单来说,它拥有date → text
以及text → date
的能力。
例如:将Date格式化输出
// 格式化输出 SimpleDateFormat sDF = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); String datestr = sdf.format(new Date()); System.out.println(dateStr); // 结果 2018年02月03日 15:20:58
例如:将时间字符串转化为Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); Date date = sdf.parse("2018年02月03日 15:20:58");
注意,SimpleDateFormat.parse(String source)
中的source
格式一定得是SimpleDateFormat
当前使用的格式。如这个例子中使用了yyyy年MM月dd日 HH:mm:ss
,所以传入了2018年02月03日 15:20:58
时间字符串。
PS:有些同学对yyyy
或者MM
这些字母代表的含义不懂的话,建议使用这个类的时候,看一下源码,源码类上都有对这些字母的解释。
java.util.Calendar
日历类,这个类大多被用于获取时间的特殊属性,比如说获取某个时间对象的年份、月份、星期等
Calendar calendar = Calendar.getInstance(); // 设置时间,不设置的话,默认是当前时间 calendar.setTime(new Date()); // 获取时间中的年份 int year = calendar.get(Calendar.YEAR);
从JDK1.8开始,Calendar增加新的构造方式
// since jdk 1.8 Calendar calendar = new Calendar.Builder().setDate(2018, 3, 25).build(); int year = calendar.get(Calendar.YEAR); System.out.println(year);
阶段小结
以上大概就是jdk1.8之前的操作时间方式了。然后,从jdk1.8开始,有了新的操作时间的类。
java.time.LocalDate
LocalDate提供年月日而不提供时分秒信息,它是不可变类且线程安全的。它经常被用于展示year-month-day,day-of-year,day-of-week,week-of-year
等格式的信息。
LocalDate localDate = LocalDate.now(); // 获取当天是几号 int dayOfMonth = localDate.getDayOfMonth(); // 获取当天是星期几 DayOfWeek dayOfWeek = localDate.getDayOfWeek(); // 获取本月的第一天 LocalDate FirstDayOfMonth = localDate.with(TemporalAdjusters.firstDayOfMonth()); // 取本月最后一天 LocalDate lastDayOfThisMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
是不是很赞~
java.time.Time
提供时分秒不提供年月日,也是线程安全并且不可变类。它经常被用于展示hour-minute-second
格式的信息。可以对时间进行加减等操作。
// 样例 LocalTime localTime = LocalTime.now(); // 获取当前的小时 int hour = localTime.getHour(); System.out.println(hour); // 小时数加1 LocalTime addTwoHours = localTime.plusHours(2L); System.out.println(addTwoHours.getHour()); // 结果 16 18
java.time.LocalDateTime
包含年月日时分秒,精确到纳秒级别,同样是线程安全并且不可变类。它可以操作时间中的年月日时分秒并且可以获取其中的属性。
LocalDateTime localDateTime = LocalDateTime.now(); // 获取年 int year = localDateTime.getYear(); // 获取小时 int hour = localDateTime.getHour(); // 增加一年 LocalDateTime addOneYear = localDateTime.plusYears(1);
结语
今天就先这样啦,希望看到这篇博文的人能有所收获,同样,错误之处还请帮忙指正。
以上是脚本宝典为你收集整理的Java时间类总结全部内容,希望文章能够帮你解决Java时间类总结所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。