【问题标题】:How to use reusable function - Android, Java [duplicate]如何使用可重用功能 - Android,Java [重复]
【发布时间】:2021-12-29 05:23:27
【问题描述】:

当 Calendar instance = Calendar.getInstance(); 时,我实现了小的 if-else 部分来获取月份的前三个字母;检索月份的整数值。我想将它用作可重用的功能。我尝试过创建另一个 java 文件,但它想要工作。谁能告诉我如何使用它?

public class MstrMonth {

    public MstrMonth() {}

    public static String getCurrentMonth(String currentMonth) {

        Calendar instance = Calendar.getInstance();
        int mMonth = instance.get(Calendar.MONTH) + 1;

        if(mMonth == 1){
            currentMonth = "JAN";
        }else if (mMonth == 2){
            currentMonth = "FEB";
        }else if (mMonth == 3){
            currentMonth = "MAR";
        }else if (mMonth == 4){
            currentMonth = "APR";
        }else if (mMonth == 5){
            currentMonth = "MAY";
        }else if (mMonth == 6){
            currentMonth = "JUN";
        }else if (mMonth == 7){
            currentMonth = "JUL";
        }else if (mMonth == 8){
            currentMonth = "AUG";
        }else if (mMonth == 9){
            currentMonth = "SEP";
        }else if (mMonth == 10){
            currentMonth = "OCT";
        }else if (mMonth == 11){
            currentMonth = "NOV";
        }else if (mMonth == 12){
            currentMonth = "DEC";
        }
        return currentMonth;
    }
}

在主要活动中;

String currentMonth;
MstrMonth.getCurrentMonth(currentMonth);

我想在这里获取月份代码,但它给出了空值。

【问题讨论】:

  • 您需要将方法的返回值分配给某些东西...分配给参数不会影响调用代码(改变参数会,但这对于字符串来说是不可能的,因为它们是不可变)。

标签: java null coding-style reusability


【解决方案1】:

试试这个。

public static String getCurrentMonth() {
    Calendar instance = Calendar.getInstance();
    return instance.getDisplayName(
        Calendar.MONTH, Calendar.SHORT, Locale.ENGLISH).toUpperCase();
}

public static void main(String[] args) throws Exception {
    String currentMonth = getCurrentMonth();
    System.out.println(currentMonth);
}

输出:

NOV

【讨论】:

  • 嗨 @sadetaosa String currentMonth = new SimpleDateFormat("MMM", Locale.ENGLISH).format(instance.getTime()).toUpperCase(); String currentMonth2 = instance.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.ENGLISH).toUpperCase(); 两者都给出“MMM”代码。什么是最好的?
  • @TheArkD 随你喜欢。
【解决方案2】:

试试这个代码,在我的测试项目中工作


public class MstrMonth {

    public MstrMonth() {}

    public static String getCurrentMonth() {

        String currentMonth = null;
        Calendar instance = Calendar.getInstance();
        int mMonth = instance.get(Calendar.MONTH) + 1;

        if(mMonth == 1){
            currentMonth = "JAN";
        }else if (mMonth == 2){
            currentMonth = "FEB";
        }else if (mMonth == 3){
            currentMonth = "MAR";
        }else if (mMonth == 4){
            currentMonth = "APR";
        }else if (mMonth == 5){
            currentMonth = "MAY";
        }else if (mMonth == 6){
            currentMonth = "JUN";
        }else if (mMonth == 7){
            currentMonth = "JUL";
        }else if (mMonth == 8){
            currentMonth = "AUG";
        }else if (mMonth == 9){
            currentMonth = "SEP";
        }else if (mMonth == 10){
            currentMonth = "OCT";
        }else if (mMonth == 11){
            currentMonth = "NOV";
        }else if (mMonth == 12){
            currentMonth = "DEC";
        }
        return currentMonth;
    }
}

活动中


String currentMonth = MstrMonth.getCurrentMonth(currentMonth);

【讨论】:

  • 谢谢,但这也会产生错误。字符串 currentMonth = MstrMonth.getCurrentMonth();这解决了错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-22
  • 1970-01-01
  • 2016-03-03
  • 2019-09-27
相关资源
最近更新 更多