包含有年月日时分秒,精确到毫秒级别。
官方解释:java
// 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
包含年月日,时分秒都被设置为0,之因此这样设计是为了适应SQL中的DATE
类型。
官方解释:sql
// 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
包含时分秒,这个也是为了SQL中的TIME
类型而出现的。app
// 语句 Time time = new Time(System.currentTimeMillis()); System.out.println(time); // 输出结果 15:07:35
时间戳,适配于SQL中的TIMESTAMP
类型而出现的,精确到纳秒级别。ide
这个类提供时间的各类格式化输出和将字符串转换为时间类,简单来讲,它拥有date → text
以及text → date
的能力。
例如:将Date格式化输出ui
// 格式化输出 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
例如:将时间字符串转化为Datethis
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
这些字母表明的含义不懂的话,建议使用这个类的时候,看一下源码,源码类上都有对这些字母的解释。线程
日历类,这个类大多被用于获取时间的特殊属性,好比说获取某个时间对象的年份、月份、星期等设计
Calendar calendar = Calendar.getInstance(); // 设置时间,不设置的话,默认是当前时间 calendar.setTime(new Date()); // 获取时间中的年份 int year = calendar.get(Calendar.YEAR);
从JDK1.8开始,Calendar增长新的构造方式code
// 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开始,有了新的操做时间的类。
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());
是否是很赞~
提供时分秒不提供年月日,也是线程安全而且不可变类。它常常被用于展现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
包含年月日时分秒,精确到纳秒级别,一样是线程安全而且不可变类。它能够操做时间中的年月日时分秒而且能够获取其中的属性。
LocalDateTime localDateTime = LocalDateTime.now(); // 获取年 int year = localDateTime.getYear(); // 获取小时 int hour = localDateTime.getHour(); // 增长一年 LocalDateTime addOneYear = localDateTime.plusYears(1);
今天就先这样啦,但愿看到这篇博文的人能有所收获,一样,错误之处还请帮忙指正。