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

本文介绍了如何在Flutter和Android中使用if-else和公式计算来确定当前月份所属的季度,包括示例代码。适合开发者理解和应用到实际项目中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一年有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;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值