1用Data还是用Calendar
看Data源码,一开头就说了Data有bug,具体是啥bug我也看不太懂不用关心,反正官方推荐用Calendar。
2SimpleDateFormat
SimpleDateFormat可以设置输出格式(yyyy MM dd HH mm ss)
3时间戳
时间戳简单的来说就是从1970年1月1日0时0分0秒到现在的毫秒数,10位时间戳时精确到秒,13位时间戳是精确到毫秒
4获取时间戳
13位:System.currentTimeMillis();
10位:long timeStampSec = System.currentTimeMillis()/1000;
String timestamp = String.format("%010d", timeStampSec);
5获取今天年月日时分秒
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Calendar calendar = Calendar.getInstance();
Log.e("现在的年月日时分秒",simpleDateFormat.format(calendar.getTime()));
输出:E/现在的年月日时分秒: 2019年01月11日 16时03分11秒
6获取各种类型的时间
yyyy代表年;MM代表月;dd代表日;HH代表小时;mm代表分钟;ss代表秒;
new SimpleDateFormat的时候可以随意组合
7获取昨天年月日
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy年MM月dd日");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -1);
Log.e("昨天的年月日时分秒",simpleDateFormat.format(calendar.getTime()));
输出:E/昨天的年月日: 2019年01月10日
8获取明天的年月日
calendar.add(Calendar.DATE, 1);
9设置任意一个日期,输出这个日期当天,和前一天
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy年MM月dd日");
Calendar calendar = Calendar.getInstance();
calendar.set(1994, 0, 26); //月份从0开始,0代表1月,11代表12月
Log.e("先设置一个日期", simpleDateFormat.format(calendar.getTime()));
calendar.add(Calendar.DATE, -1);
Log.e("这个日期的前一天", simpleDateFormat.format(calendar.getTime()));
输出:E/先设置一个日期: 1994年01月26日
E/这个日期的前一天: 1994年01月25日
10获取本周的第一天
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy年MM月dd日");
Calendar calendar = Calendar.getInstance();
int firstDayOfWeek = calendar.getFirstDayOfWeek();
calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
Log.e("本周的第一天", calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.CHINA)
+ simpleDateFormat.format(calendar.getTime()));
输出:E/本周的第一天: 周日2019年01月06日 //按照圣经来说 神是在第六天(星期六)创造完了时间万物 第七天休息(第七天是新一周的开始 也就是星期天也称礼拜日),所以外国人都以周日为一周之首日....
呵呵,跑题了
11设置周一为一个周开始的第一天
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);//这里随意设置,像设置周几为一个周的开始都行
int firstDayOfWeek = calendar.getFirstDayOfWeek();
calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
Log.e("本周的第一天", calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.CHINA)
+ simpleDateFormat.format(calendar.getTime()));
输出:E/本周的第一天: 周一2019年01月07日
12获取本周7天的信息
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
int firstDayOfWeek = calendar.getFirstDayOfWeek();
for (int i =0; i <7; i++) {
calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + i);
Log.e("本周", calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.CHINA)
+ simpleDateFormat.format(calendar.getTime()));
}
输出:E/本周: 周一2019年01月07日
E/本周: 周二2019年01月08日
E/本周: 周三2019年01月09日
E/本周: 周四2019年01月10日
E/本周: 周五2019年01月11日
E/本周: 周六2019年01月12日
E/本周: 周日2019年01月13日
13获取上一周7天的信息
这个我是百度不到现成的办法了,不过也很简单,先获取本周周1,然后减去八天,就是上上个周日,在for循环7次,每次输出下一天的信息,连起来就是上周7天的信息了,要封装一下也简单,我不在这篇文章写封装。
Calendar calendar = Calendar.getInstance();
calendar.setFirstDayOfWeek(Calendar.MONDAY);
int firstDayOfWeek = calendar.getFirstDayOfWeek();
calendar.set(Calendar.DAY_OF_WEEK,firstDayOfWeek);//这是日期是本周一
calendar.add(Calendar.DATE, -8);//减8天,日期变成上上个周日
for (int i =0; i <7; i++) {
calendar.add(Calendar.DATE, 1);//加1天,日期变成从上周一开始,循环7次
Log.e("上一周 ", calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.CHINA)
+ simpleDateFormat.format(calendar.getTime()));
}
14获取本月第一天
calendar.set(Calendar.DAY_OF_MONTH,1);
15本月全部天数的信息,上,下,月全部天数的信息,都参考周就可以了
16有的地方是add,有的地方是set,要看仔细一点。
网友评论