1.获取指定年月日及设置指定时间

int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(time));
int month = Integer.parseInt(new SimpleDateFormat("MM").format(time));
int day = Integer.parseInt(new SimpleDateFormat("dd").format(time));
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day,17,30);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(year, month - 1, day,18,30);

//时间插入
calendar.getTime();

2.获取当天最大时间(yyyyMMdd 23:59:59)

  private static Date getEndOfDay(Date date) {
    LocalDateTime localDateTime = LocalDateTime
        .ofInstant(Instant.ofEpochMilli(date.getTime()), ZoneId.systemDefault());;
    LocalDateTime endOfDay = localDateTime.with(LocalTime.MAX);
    return Date.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());
  }

3.日期格式化

SimpleDateFormat Format = new SimpleDateFormat("yyyy-MM-dd");
Date datetime = Format.parse(date);

4.获取指定日期月份第一天和最后一天

//获取本月第一天 
SimpleDateFormat sf=new SimpleDateFormat("MM-dd");
GregorianCalendar gcLast=(GregorianCalendar)Calendar.getInstance();
gcLast.setTime(time);
gcLast.set(Calendar.DAY_OF_MONTH, 1);
String firstDay = sf.format(gcLast.getTime());

//获取本月最后一天
Calendar calendar=Calendar.getInstance();
calendar.setTime(time);
calendar.add(Calendar.DATE, -1);
calendar.set(Calendar.DATE, calendar.getActualMaximum(calendar.DATE));
String endDay = sf.format(calendar.getTime());

5.获取指定日期的周一日期与周五日期

Calendar calendar = Calendar.getInstance();
calendar.setTime(createDate);
//判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了 int dayWeek = calendar.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 if(1 == dayWeek) { calendar.add(Calendar.DAY_OF_MONTH, -1); } calendar.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一 int day = calendar.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天 calendar.add(Calendar.DATE, calendar.getFirstDayOfWeek()-day);//根据日历的规则,给当前日期减去星期几与一个星期第一天的差值 String starDay = sdf.format(calendar.getTime());
calendar.add(Calendar.DATE,
4); String endDay = sdf.format(calendar.getTime());

6.获取当前年份,月份

    String month = null;
    Calendar cale = Calendar.getInstance();
    int year = cale.get(Calendar.YEAR);
    int nowMonth = cale.get(Calendar.MONTH) + 1;
    month = String.valueOf(year);
12-17 09:06