获取当前手机系统时间
Calendar current = Calendar.getInstance();
时间设置
//获取手机当前系统时间
Calendar current = Calendar.getInstance();
//假设当前时间为某一具体时间,如workDate
//方式一:long类型 : workDate
current.setTimeInMillis(workDate);
//方式二:Date类型 : workDate
current.setTime(workDate);
//方式三:也可以通过设置年月日时分秒
current.set(Calendar.YEAR, year); //年
current.set(Calendar.MONTH, month); //月
current.set(Calendar.DATE, date); //日
current.set(Calendar.HOUR_OF_DAY, hour); //时
current.set(Calendar.MINUTE, minute); //分
current.set(Calendar.SECOND, second); //秒
字段意义:
- ERA :纪元 。只能为0 或 1。0表示公元前,1表示公元
- YEAR:年
- MONTH :月 。第一个月为 0
- WEEK_OF_YEAR:当前日期在本年中对应第几个星期,一年中第一个星期的值为 1
- WEEK_OF_MONTH:当前日期在本月中对应第几个星期,一个月中第一个星期的值为 1
- DATE :日。一个月中第一天的值为 1
- DAY_OF_MONTH:同“DATE”,表示“日”
- DAY_OF_YEAR:当前日期在本年中对应第几天。一年中第一天的值为 1
- DAY_OF_WEEK:星期几。周日为1
- DAY_OF_WEEK_IN_MONTH:当前月中的第几个星期。DAY_OF_MONTH 1 到 7 总是对应于 DAY_OF_WEEK_IN_MONTH 1;8 到 14 总是对应于 DAY_OF_WEEK_IN_MONTH 2,依此类推
- AM_PM:可以是AM 或 PM。AM为0,表示上午;PM为1,表示下午
- HOUR : HOUR 用于 12 小时制时钟 (0 - 11)。中午和午夜用 0 表示,不用 12 表示
- HOUR_OF_DAY:用于 24 小时制时钟
- MINUTE :分
- SECOND :秒
- MILLISECOND :毫秒
- ZONE_OFFSET:毫秒为单位指示距 GMT 的大致偏移量
时间推迟或者提前
//beginTimeLimit相对于beginTime,提前joinLimit分钟
Calendar beginTimeLimit = (Calendar) beginTime.clone();
beginTimeLimit.add(Calendar.MINUTE, -joinLimit);
//endTimeLimit相对于endTime,推迟endLimit分钟
Calendar endTimeLimit = (Calendar) endTime.clone();
endTimeLimit.add(Calendar.MINUTE, endLimit);
也可以提前或推迟年,月,日,星期,小时,设置的参数参照字段意义
时间比较
if (current.before(retroactiveTimeLimit)) {
before = true;
} else {
before = false;
}
Calendar 时间,通过 before 和 after比较,两者都不包含相等关系
如需判断相等关系,可通过非语句判断,如
// beginTimeLimit<= current <beginTime
if (!current.before(beginTimeLimit) && current.before(beginTime)){
}
时间数据类型转换
String 转 Date
// systemTime:"2018-01-11 15:53:29"
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date systemDate = sf.parse(systemTime);
Date 转 String
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String systemTime=sf.format(systemDate);
Long 与 Date
Date systemDate=new Date(timeLong); //Long转Date
long timeLong = systemDate.getTime(); //Date转Long
SimpleDateFormat 的格式化方式根据需求决定:
其中要注意占位符所表示的含义:
y 年
M 年中的月份
D 年中的天数
d 月份中的天数
举例,2月1日。假如用D表示,则显示2月32日,因为2月1日,在一年中属于第32天;用d表示,则显示2月1日,因为2月1日在一年中属于第1天
H 一天中的小时数(0-23)
h am/pm 中的小时数(1-12)
m 小时中的分钟数
s 分钟中的秒数
S 毫秒数
拓展
判断是不是今天
/**
* 判断时间是不是今天
*
* @return 是返回true,不是返回false
*/
public static boolean isToday(long date) {
//当前时间
Date now = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
//获取今天的日期
String nowDay = sf.format(now);
Date dateLesson = new Date(date);
//对比的时间
String day = sf.format(dateLesson);
return day.equals(nowDay);
}
时区
设置时区的方式:
方式一:
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
sf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
方式二:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
format.format(date);
方式三:
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
关于时间的判断:
建议使用 long 类型判断,避免存在时区的差误
如果需求需要考虑到时区:
建议:Calendar 、SimpleDateFormat 都设置时区