jdk1.8之前
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类型而出现的,精确到纳秒级别。
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.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);
//设置当月最后一天
calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
//设置当周最后一天
//一周第一天是否为星期天
boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);
if(isFirstSunday){
calendar.set(Calendar.DAY_OF_WEEK,calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
calendar.add(Calendar.DATE, 1);
}else{
calendar.set(Calendar.DAY_OF_WEEK,calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
}
//结合SimpleDateFormat 格式化输出
sdf.format(calendar.getTime())
注: Java中Calendar.DAY_OF_WEEK表示一周中的第几天,所以他会受到 第一天是星期几 的影响。
有些地区以星期日作为一周的第一天,而有些地区以星期一作为一周的第一天,需要加以区分。
从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);