Date相关操作

查询某月第一天
/**
     * 获取某月第一天
     *
     * @param dateyyyyMM
     * @return
     */
    private String getFirstDateByMonth(String dateyyyyMM) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM");
        //获取日期
        Date date = format.parseDateTime(dateyyyyMM).toDate();
        //初始化
        Calendar calendar = Calendar.getInstance();
        //设置日期
        calendar.setTime(date);
        //设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        //获取当月第一天
        return sdf.format(calendar.getTime());

    }
查询某月最后一天
    /**
     * 获取某月最后一天
     *
     * @param dateyyyyMM
     * @return
     */
    private String getLastDateByMonth(String dateyyyyMM) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM");
        //获取日期
        Date date = format.parseDateTime(dateyyyyMM).toDate();
        //初始化
        Calendar calendar = Calendar.getInstance();
        //设置日期
        calendar.setTime(date);
        //设置为最后一号,当前日期即为本月的最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        //获取当月最后一天
        return sdf.format(calendar.getTime());
    }
比较两个日期大小
  1. date1.getTime()>date2.getTime()
    
  2. date1.compareTo(date2)
    
    相等返回0,date1>date2返回1,date1<date2返回-1
字符串转Date
//声明日期格式
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(dateStr);
Date转字符串
//申明时间格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(date)
日期加一天
//日期加一天
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.DATE, 1);
date = c.getTime();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值