使用java的Calendar对象获得当前日期

 更新时间:2015年07月02日 08:39:03   投稿:hebedich  
本文给大家分享的是使用使用java的Calendar对象获得当前日期的上几个度开始、结束时间,主要思路是先获得当前季度的开始和结束日期,在当前日期的基础上往前推3个月即上个季度的开始和结束日期,十分的实用,小伙伴们可以参考下。

思路:

先获得当前季度的开始和结束日期,在当前日期的基础上往前推3个月即上个季度的开始和结束日期

/**
   * @param flag true:开始日期;false:结束日期
   * @return
   */
  public static String getLastQuarterTime(boolean flag){
    SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     
    String resultDate="";
    Date now = null;
    try {
      Calendar calendar = Calendar.getInstance();
      int currentMonth = calendar.get(Calendar.MONTH) + 1;
      //true:开始日期;false:结束日期
      if(flag){
        if (currentMonth >= 1 && currentMonth <= 3)
          calendar.set(Calendar.MONTH, 0);
        else if (currentMonth >= 4 && currentMonth <= 6)
          calendar.set(Calendar.MONTH, 3);
        else if (currentMonth >= 7 && currentMonth <= 9)
          calendar.set(Calendar.MONTH, 6);
        else if (currentMonth >= 10 && currentMonth <= 12)
          calendar.set(Calendar.MONTH, 9);
        calendar.set(Calendar.DATE, 1);
         
        now = longSdf.parse(shortSdf.format(calendar.getTime()) + " 00:00:00");
      }else{
        if (currentMonth >= 1 && currentMonth <= 3) {
          calendar.set(Calendar.MONTH, 2);
          calendar.set(Calendar.DATE, 31);
        } else if (currentMonth >= 4 && currentMonth <= 6) {
          calendar.set(Calendar.MONTH, 5);
          calendar.set(Calendar.DATE, 30);
        } else if (currentMonth >= 7 && currentMonth <= 9) {
          calendar.set(Calendar.MONTH, 8);
          calendar.set(Calendar.DATE, 30);
        } else if (currentMonth >= 10 && currentMonth <= 12) {
          calendar.set(Calendar.MONTH, 11);
          calendar.set(Calendar.DATE, 31);
        }
        now = longSdf.parse(shortSdf.format(calendar.getTime()) + " 23:59:59");
      }
      calendar.setTime(now);// 设置日期
      calendar.add(Calendar.MONTH, -3);
      resultDate = longSdf.format(calendar.getTime());
       
    } catch (Exception e) {
      ;
    }
    return resultDate;
  }

相关文章

  • 浅析MMAP零拷贝在RocketMQ中的运用

    浅析MMAP零拷贝在RocketMQ中的运用

    零拷贝技术可以减少数据拷贝和共享总线操作的次数,消除传输数据在存储器之间不必要的中间拷贝次数,从而有效地提高数据传输效率,这篇文章主要介绍了MMAP零拷贝在RocketMQ中的运用,需要的朋友可以参考下
    2022-07-07
  • PageHelper插件实现服务器端分页功能

    PageHelper插件实现服务器端分页功能

    这篇文章主要为大家详细介绍了PageHelper插件实现服务器端分页功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • SpringBoot异步方法捕捉异常详解

    SpringBoot异步方法捕捉异常详解

    这篇文章主要为大家详细介绍了SpringBoot异步方法捕捉异常,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • SpringCloud实现文件上传功能的方法详解

    SpringCloud实现文件上传功能的方法详解

    这篇文章主要为大家详细介绍了SpringCloud如何实现文件上传功能,文中的示例代码讲解详细,对我们学习有一定的借鉴价值,需要的可以参考一下
    2022-08-08
  • eclipse如何搭建Springboot项目详解

    eclipse如何搭建Springboot项目详解

    今天带大家学习eclipse如何搭建Spring boot项目,文中有非常详细的图文解说,对正在学习java的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-05-05
  • Java BigDecimal使用及基本运算(推荐)

    Java BigDecimal使用及基本运算(推荐)

    Java在java.math包中提供的API类BigDecimal,用来对超过16位有效位的数进行精确的运算。这篇文章主要介绍了Java BigDecimal使用指南针(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • 详解Spring ApplicationContext加载过程

    详解Spring ApplicationContext加载过程

    这篇文章主要介绍了Spring ApplicationContext加载过程的相关资料,帮助大家更好的理解和学习使用spring框架,感兴趣的朋友可以了解下
    2021-03-03
  • 剑指Offer之Java算法习题精讲链表专项训练

    剑指Offer之Java算法习题精讲链表专项训练

    跟着思路走,之后从简单题入手,反复去看,做过之后可能会忘记,之后再做一次,记不住就反复做,反复寻求思路和规律,慢慢积累就会发现质的变化
    2022-03-03
  • Tree组件实现支持50W数据方法剖析

    Tree组件实现支持50W数据方法剖析

    这篇文章主要为大家介绍了Tree组件实现支持50W数据的方法剖析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • 详解Java中的HashTable

    详解Java中的HashTable

    这篇文章主要介绍了Java中的HashTable的相关资料,帮助大家更好的理解和使用Java,感兴趣的朋友可以了解下
    2020-12-12

最新评论