日期的获取
1. 获取上一周时间(周一的0点___周日的23:59:59)
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
int offset1 = 1 - dayOfWeek;
int offset2 = 7 - dayOfWeek;
calendar1.add(Calendar.DATE, offset1 - 7);
calendar2.add(Calendar.DATE, offset2 - 7);
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String lastBeginDate = sdf.format(calendar1.getTime());
DateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
String lastEndDate = sdf1.format(calendar2.getTime());
System.out.println(lastBeginDate + "," + lastEndDate);
2. 获取本周时间(本周一的0点___本周日的23:59:59)
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.WEEK_OF_MONTH, 0);
cal.set(Calendar.DAY_OF_WEEK, 2);
Date time = cal.getTime();
//本周第一天
String format = new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(time);
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.DAY_OF_WEEK, cal1.getActualMaximum(Calendar.DAY_OF_WEEK));
cal1.add(Calendar.DAY_OF_WEEK, 1);
Date time1 = cal1.getTime();
//本周最后一天
String format1 = new SimpleDateFormat("yyyy-MM-dd 23:59:59").format(time1);
System.out.println(format);
System.out.println(format1);
3. 获取上个月时间(月初的0点___月末的23:59:59)
暂无
4. 获取本月时间(月初的0点___月末的23:59:59)
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, 0);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
//本月第一天
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-01 00:00:00");
String gtimeStart = sdf2.format(c.getTime());
//本月最后一天
c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);
String gtimeEnd = sdf.format(c.getTime());
System.out.println(gtimeStart + "----" + gtimeEnd);
5. 转换unix时间(日期和unix时间相互转换)
//2020-05-25 00:00:00
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(1590336000000L));
System.out.println(date);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//1590940800000
long epoch = df.parse("2020-06-1 00:00:00").getTime();
System.out.println("should be 1590940800000 :" + epoch);
6. 当天(00:00:00—23:59:59)
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//当天0点
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String gtimeStart = sdf2.format(c.getTime());
//当天23:59:59
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
String gtimeEnd = sdf3.format(c.getTime());
long startTime = sdf.parse(gtimeStart).getTime();
long endTime = sdf.parse(gtimeEnd).getTime();
7. 本年(00:00:00—23:59:59)
//获取今年哪一年
Date date = new Date();
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(date);
//获取本年的开始时间
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, Integer.valueOf(gc.get(1)));
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
//获取本年的结束时间
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.YEAR, Integer.valueOf(gc.get(1)));
cal1.set(Calendar.MONTH, Calendar.DECEMBER);
cal1.set(Calendar.DATE, 31);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//本年的开始0点
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
String gtimeStart = sdf2.format(cal.getTime());
//本年的结束23:59:59
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
String gtimeEnd = sdf3.format(cal1.getTime());
long startTime = sdf.parse(gtimeStart).getTime();
long endTime = sdf.parse(gtimeEnd).getTime();
8. 2020-08-11T08:00:04.066Z 时间格式转换
//用来转换格式的
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//把格式转换成(2020-08-11T16:00:04.066+0800)
Date parse = dateFormat.parse("2020-08-11T08:00:04.066Z");
//转换成平常类型(2020-08-11 16:00:04)
String s = df.format(parse);
//变成unix时间(1597132804000)
long time = df.parse(s).getTime();
9. 当前时间的获取(unix时间)
long unixTime = System.currentTimeMillis();
10.获取昨天时间
//昨天零点
LocalDateTime yesterdayBegin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).minusDays(1L);
//昨天23:59:59.99999
LocalDateTime yesterdayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX).minusDays(1L);
long begin = yesterdayBegin.toInstant(ZoneOffset.of("+8")).toEpochMilli();
long end = yesterdayEnd.toInstant(ZoneOffset.of("+8")).toEpochMilli();