前言:本篇文章的主要目的是为了把工作中常用的关于时间上的操作统一整理一下,梳理一下他们之间的转换关系,
来方便自己和广大同胞复习和快速开发使用。
目录
- Calendar官方简介
- Calendar、Date、SimpleDateFormat 初始化部分
- Calendar、Date、时间戳转化
- Calendar和时间戳 转成 Date
- Date和时间戳 转成 Calendar
- Calendar和Date 转成 时间戳
- Calendar设置日期时间和获取日期时间
- 通过Caledar提供的方法直接设置年月日时分秒
- 通过set方法单独设置年月日等各个参数
- Calendar其他部分常用参数设置
- 从Calendar获取日期时间等参数
- Calendar、Date、SimpleDateFormat部分
- SimpleDateFormat的简单使用
- 将Calendar、Date、TimeMills(时间戳)转化为指定格式日期字符串
- 将SimpleDateFormat格式化后的日期转化成Calendar、Date对象
- 判断指定时间是否在某个时间区间内
- 判断某个日期是否在指定区间(包括区间边界)内?
Calendar官方简介
The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).
The class also provides additional fields and methods for implementing a concrete calendar system outside the package. Those fields and methods are defined as protected.
Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar’s getInstance method returns a Calendar object whose calendar fields have been initialized with the current date and time:
有道翻译:Calendar类是一个抽象类,它提供了一些方法,用于在特定的时间瞬间和一组日历字段(如YEAR、MONTH、DAY_OF_MONTH、HOUR等)之间进行转换,以及操作日历字段(如获取下一周的日期)。时间上的一个瞬间可以用毫秒值来表示,这个毫秒值是从格林威治时间1970年1月1日00:00:00.000(格里高利安)开始的偏移量。
该类还提供了用于在包之外实现具体日历系统的其他字段和方法。这些字段和方法被定义为受保护的。
与其他对区域设置敏感的类一样,Calendar提供了一个类方法getInstance,用于获取这种类型的一般有用对象。Calendar的getInstance方法返回一个日历对象,该对象的日历字段已经用当前日期和时间初始化
Calendar、Date、SimpleDateFormat 初始化部分
Calendar calendar = Calendar.getInstance();
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar和Date进行初始化后,都会获得一个默认初始时间,即系统时间(时间戳)。
对应时间转换为毫秒值等于当前时间距离 January 1, 1970 00:00:00.000 GMT之间的毫秒数。
Calendar、Date、时间戳转化
Calendar和时间戳 转成 Date
/**
* Calendar 转 Date
* */
public Date transCalendarToDate(Calendar calendar){
Date date = calendar.getTime();
return date;
}
/**
* 时间戳 转 Date
* */
public Date transMillsToDate(long timeMills) {
Date date = new Date(timeMills);
return date;
}
Date和时间戳 转成 Calendar
/**
* Date 转 Calendar
* */
public Calendar transDateToCalendar(Date date) {
Calendar calendar = Calendar.getInstance();//获取Calendar实例,默认拥有当前系统时间
calendar.setTime(date);
return calendar;
}
/**
* 通过毫秒级时间戳获得对应的Calendar对象
* @param mills An instant in time can be represented by a millisecond value that is an offset from the Epoch,
* January 1, 1970 00:00:00.000 GMT (Gregorian).
* */
public Calendar transDateToCalendar(long timeMills) {
Calendar calendar = Calendar.getInstance();//获取Calendar实例,默认拥有当前系统时间
calendar.setTimeInMillis(timeMills);
return calendar;
}
Calendar和Date 转成 时间戳
/**
* Calendar 转 时间戳
* */
public long transCalendarToTimeMills(Calendar calendar) {
long timeMills = calendar.getTimeInMillis();
return timeMills;
}
/**
* Date 转 时间戳
* */
public long transCalendarToTimeMills(Date date) {
long timeMills = date.getTime()