基于Android一些时间创建的基本概念
获取当前时间
方式一:
Date date = newDate();
Log.e(TAG, "当前时间="+date);
结果:
E/TimeActivity: 当前时间=Wed Sep 12 07:03:25 GMT+00:00 2018
方式二:
long date = System.currentTimeMillis(); //获得系统时间,得到是从1970年到现在的毫秒级long值
Log.e(TAG, "毫秒级time值=:"+date);
结果:
E/TimeActivity: 毫秒级time值=:1536736055737
方法三:
Calendar calendar =Calendar.getInstance();
calendar.getTimeInMillis();
获取设备开机至此时的相对时间
long time = SystemClock.elapsedRealtime();
获取休眠唤醒后到此时的相对时间
SystemClock.uptimeMillis();
SimpleDateFormat 格式化时间
格式化字母表
SymbolMeaningPresentationExample
G
era designator 时代
Text
AD
y
year 年
Number
2009
M
month in year 月
Text & Number
July & 07
d
day in month 日
Number
10
h
hour in am/pm (1-12) 12小时制的时
Number
12
H
hour in day (0-23) 24小时制的时
Number
0
m
minute in hour 分钟
Number
30
s
second in minute 秒
Number
55
S
millisecond 毫秒
Number
978
E
day in week 星期
Text
Tuesday
D
day in year 一年中的第几天
Number
189
F
day of week in month 一个月中的第几周的星期几
Number
2 (2nd Wed in July)
w
week in year 一年中的第几周
Number
27
W
week in month 一月中的第几周
Number
2
a
am/pm marker 上午/下午
Text
PM
k
hour in day (1-24) 一天中的第几个小时
Number
24
K
hour in am/pm (0-11)
Number
0
z
time zone 时区
Text
Pacific Standard Time
'
escape for text
Delimiter
(none)
'
single quote
Literal
'
字符串转时间戳
String str = "2019-03-13 13:54:00";
SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date=simpleDateFormat.parse(str);long ts = date.getTime(); System.out.println(ts);
时间戳转字符串
格式化出全部的时间:
long time =System.currentTimeMillis();
//格式化 日-月(3个M等于文本形式的月份)-年 小时:分钟:秒:毫秒
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss:SSS");
String timeText =formatter.format(time);
Log.e(TAG, "格式化后time值=:"+timeText);
结果:
E/TimeActivity: 格式化后time值=:12-Sep-2018 07:22:38:596
只格式化时-分-秒-毫秒:
SimpleDateFormat formatter = new SimpleDateFormat("HH-mm-ss-SSS");//分隔符是可以自己设定的
Log.e(TAG, "格式化后time值=:"+formatter.format(System.currentTimeMillis()));
结果:
E/TimeActivity: 格式化后time值=:07-29-18-317
格式化一个月中第几周:
SimpleDateFormat formatter = new SimpleDateFormat("M-W");//月份-月份中的第几周
//如果是"MM-WW" 则可以格式化出 09-03
Log.e(TAG, "格式化后time值=:"+formatter.format(System.currentTimeMillis()));
结果:
E/TimeActivity: 格式化后time值=:9-3
Calendar 历法类
例一:
mFormat = new SimpleDateFormat("yyyy-MM-dd:HH-mm-ss");
Calendar calendar = Calendar.getInstance();//得到单例
calendar.set(Calendar.YEAR,2018);//年
calendar.set(Calendar.MONTH,8);//月 注意月份是从0开始计算的,所以这里的8,在格式化后等于9
calendar.set(Calendar.DAY_OF_MONTH,12);//日
calendar.set(Calendar.HOUR_OF_DAY,15);//HOUR_OF_DAY 24小时制的时
calendar.set(Calendar.MINUTE,52);//分
calendar.set(Calendar.SECOND,20);//秒
Log.e(TAG, "未格式化毫秒级time="+calendar.getTimeInMillis());
Log.e(TAG, "手动格式化time="+mFormat.format(calendar.getTimeInMillis()));
Log.e(TAG, "系统自带格式化time="+calendar.getTime());
效果:
E/TimeActivity: 未格式化毫秒级time=1536767540133E/TimeActivity: 手动格式化time=2018-09-12:15-52-20E/TimeActivity: 系统自带格式化time=Wed Sep 12 15:52:20 GMT+00:00 2018
例二:
calendar.set(2017,9,13,14,24,30);
Log.e(TAG, "另一种方式输入方式time="+mFormat.format(calendar.getTimeInMillis()));
效果 :
E/TimeActivity: 另一种方式输入方式time=2017-10-13:14-24-30
例三:
calendar.set(Calendar.AM_PM,0);//0=上午 1=下午
calendar.set(Calendar.HOUR,6);//12小时值
Log.e(TAG, "上午time="+mFormat.format(calendar.getTimeInMillis()));
calendar.set(Calendar.AM_PM,1);
calendar.set(Calendar.HOUR,7);
Log.e(TAG, "下午time="+mFormat.format(calendar.getTimeInMillis()));
效果:
E/TimeActivity: 上午time=2017-10-13:06-24-30E/TimeActivity: 下午time=2017-10-13:19-24-30
使用Calendar向上或向下跳转年/月/日/时/分/秒
日期的切换还相当简单只需要当前时间戳减去24小时时间戳,年份或月份的切换是需要相当复杂的计算的,需要计算大小月/平润年.好在Calendar类已经帮我们实现了这个方法
add()方法:
说明一下add()方法,使用add()方法切换时间,会影响到其他区域的时间(增加减少日期的同时会改变月份或者年份),例如:2018-11-31(这里是11等于12),我们切换增加一天时间就会变成2019-0-1
Calendar calendar =Calendar.getInstance();
calendar.add(Calendar.YEAR,-1);//减少一年
calendar.add(Calendar.YEAR,1);//增加一年
calendar.add(Calendar.MONTH,-1);//减少一个月
calendar.add(Calendar.MONTH,1);//增加一个月
calendar.add(Calendar.DAY_OF_MONTH,-1);//减少一天
calendar.add(Calendar.DAY_OF_MONTH,1);//增加一天
roll()方法:
roll()方法正好与add()相反,使用roll()方法切换时间,不会影响到其他区域的时间,例如:2018-11-31,我们切换增加一天时间就会变成2018-11-1
Calendar calendar =Calendar.getInstance();
calendar.roll(Calendar.YEAR,-1);//减少一年
calendar.roll(Calendar.YEAR,1);//增加一年
calendar.roll(Calendar.MONTH,-1);//减少一个月
calendar.roll(Calendar.MONTH,1);//增加一个月
calendar.roll(Calendar.DAY_OF_MONTH,-2);//减少2天
calendar.roll(Calendar.DAY_OF_MONTH,2);//增加2天
//当然也可以使用布尔值
calendar.roll(Calendar.YEAR,true);//减少一年
calendar.roll(Calendar.YEAR,false);//增加一年
calendar.roll(Calendar.MONTH,true);//减少一个月
calendar.roll(Calendar.MONTH,false);//增加一个月
calendar.roll(Calendar.DAY_OF_MONTH,true);//减少一天
calendar.roll(Calendar.DAY_OF_MONTH,false);//增加一天
使用Calendar得到时间
Calendar cal =Calendar.getInstance();
//当前年
int year =cal.get(Calendar.YEAR);
Log.e(TAG, "当前年:"+year);
//当前月
int month = (cal.get(Calendar.MONTH))+1;
Log.e(TAG, "当前月:"+month);
//当前月的第几天:即当前日
int day_of_month =cal.get(Calendar.DAY_OF_MONTH);
Log.e(TAG, "当前月:"+day_of_month);
//当前时:HOUR_OF_DAY-24小时制;HOUR-12小时制
int hour =cal.get(Calendar.HOUR_OF_DAY);
Log.e(TAG, "当前时:"+hour);
//当前分
int minute =cal.get(Calendar.MINUTE);
Log.e(TAG, "当前分:"+minute);
//当前秒
int second =cal.get(Calendar.SECOND);
Log.e(TAG, "当前秒:"+second);
//当前毫秒
int millisecond =calendar.get(Calendar.MILLISECOND);
Log.e(TAG, "当前毫秒:"+millisecond);
//0-上午;1-下午
int ampm =cal.get(Calendar.AM_PM);
Log.e(TAG, "上午/下午:"+ampm);
//当前年的第几周
int week_of_year =cal.get(Calendar.WEEK_OF_YEAR);
Log.e(TAG, "当前年的第几周:"+week_of_year);
//当前月的第几周
int week_of_month =cal.get(Calendar.WEEK_OF_MONTH);
Log.e(TAG, "当前月的第几周:"+week_of_month);
//当前年的第几天
int day_of_year =cal.get(Calendar.DAY_OF_YEAR);
Log.e(TAG, "当前年的第几天:"+day_of_year);
另外解释为什么int month = (cal.get(Calendar.MONTH))+1; 需要加+1,因为这个获取的月份值是从0开始的
注意!如果你发现你获取的年份、月份、日期、小时等等不准确时请确定你的时区是否设置正确。