Java中Calendar的方法的日常使用

1、set()方法

Calendar类的set()方法用于通过指定值设置指定的日历字段。

/**
 * 获取缺省时间格式
 * @return
 */
private Calendar getDefaultDate(){
    Calendar resultCal=Calendar.getInstance();//获取当前日期
    resultCal.set(Calendar.HOUR_OF_DAY,0);// 设置时为0点
    resultCal.set(Calendar.MINUTE,0);// 设置分为0分
    resultCal.set(Calendar.SECOND,0);// 设置秒为0秒
    resultCal.set(Calendar.MILLISECOND,0);// 设置毫秒为0毫秒
    return resultCal;
}

2、获取年月

private String getPiefixForYearMonth(){
    Calendar resultCal = this.getDefaultDate();
    int year = resultCal.get(Calendar.YEAR);
    int month = resultCal.get(Calendar.MONTH) + 1;
    return year + String.format("%02d",month);
}
public static void main(String[] args) {
   Demo23 demo23 = new Demo23();
    String yearMonth = demo23.getPiefixForYearMonth();
    System.out.println("年月:"+yearMonth);
}

在这里插入图片描述

3、获取某一天的开始

private Date getOneDayForStart(int addDay){
  Calendar resultCal = this.getDefaultDate();
    resultCal.add(Calendar.DATE, addDay);
    return resultCal.getTime();
}
public static void main(String[] args) {
   Demo23 demo23 = new Demo23();
    //获取某一天的开始
    Date nowDay = demo23.getOneDayForStart(0);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format2 = sdf.format(nowDay);
    System.out.println("获取某一天的开始:"+format2);
}

在这里插入图片描述

4、获取本周的周几

private Date getDayForWeek(int addDay){
  Calendar resultCal = this.getDefaultDate();
    resultCal.set(Calendar.DAY_OF_WEEK,addDay);// 设置为本周第几天,周日是第一天
    return resultCal.getTime();
}
public static void main(String[] args) {
   Demo23 demo23 = new Demo23();
    //获取本周的周几 周日是第一天
    Date dayForWeek = demo23.getDayForWeek(3);//周二
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format = sdf.format(dayForWeek);
    System.out.println("获取到本周的周几:"+format);
}

在这里插入图片描述

5、获取某个月第一天

private Date getDayForMonth(int addMonth,int needDay){
    Calendar resultCal = this.getDefaultDate();
    resultCal.add(Calendar.MONTH, addMonth);
    resultCal.set(Calendar.DAY_OF_MONTH,needDay);// 设置为1号,当前日期既为本月第一天
    return resultCal.getTime();
}
public static void main(String[] args) {
    Demo23 demo23 = new Demo23();
    Date nowMonthFirstDay = demo23.getDayForMonth(0,1);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String format1 = sdf.format(nowMonthFirstDay);
    System.out.println("获取某个月第一天:"+format1);
}

在这里插入图片描述

6、获取当天0点时间

public static String getDate1(){
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 0);//控制时
    cal.set(Calendar.MINUTE, 0);//控制分
    cal.set(Calendar.SECOND, 0);//控制秒
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(cal.getTime());
}

7、获取当天12点时间

public static String getDate2(){
    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.HOUR_OF_DAY, 12);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(cal.getTime());
}

8、获取本周一0点时间

public static String getDate3(){
    Calendar cal = Calendar.getInstance();
    cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(cal.getTime());
}

9、获取本月第一天0点时间

public static String getDate4(){
   Calendar cal = Calendar.getInstance();
    cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
    cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(cal.getTime());
}

10、获得本月最后一天24点时间

public static String getDate5(){
   Calendar cal = Calendar.getInstance();
    cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
    cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));
    cal.set(Calendar.HOUR_OF_DAY, 24);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.format(cal.getTime());
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java亮小白1997

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值