Flutter判断当前月份是第几季度、Android判断当前月份是第几季度 、根据月份判断季度方法

  • Post author:
  • Post category:其他


一年有12个月,分为四个季度,怎样判断当前月份是第几个季度呢

方法一、if else 判断

1.flutter:

    ///当前月份
    int _currentMonth = DateTime.now().month;
    ///季度
    int quarter;
    if (_currentMonth >= 1 && _currentMonth <= 3) {
      quarter = 1;
    } else if (_currentMonth >= 4 && _currentMonth <= 6) {
      quarter = 2;
    } else if (_currentMonth >= 7 && _currentMonth <= 9) {
      quarter = 3;
    } else {
      quarter = 4;
    }

2.Android:

        Calendar c = Calendar.getInstance();
        //当前月份
        int m = c.get(Calendar.MONTH) + 1;
        //季度
        int quarter;
        if (m >= 1 && m <= 3) {
            quarter = 1;
        } else if (m >= 4 && m <= 6) {
            quarter = 2;
        } else if (m >= 7 && m <= 9) {
            quarter = 3;
        } else {
            quarter = 4;
        }

方法一、公式计算  ((月份+2)/3)取整

1.flutter:

    ///当前月份
    int _currentMonth = DateTime.now().month;

    ///当前季度
    int quarter = (_currentMonth + 2) ~/ 3;

2.Android:

        Calendar c = Calendar.getInstance();
        //当前月份
        int m = c.get(Calendar.MONTH) + 1;
        //当前季度
        int quarter = (m + 2) / 3;



版权声明:本文为KingsleyCheng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。