0

I implemented small if-else part to get First three letters of the month when Calendar instance = Calendar.getInstance(); retrieves integer value of the month. I wanna use it as a reusable function. I've tried by creating another java file but it want works. Can anyone tell me how can I use it?

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;
    }
}

In Main activity;

String currentMonth;
MstrMonth.getCurrentMonth(currentMonth);

I want get Month code in here but it gives null value.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
The Ark D
  • 7
  • 4
  • You need to assign the return value of your method to something... assignments to parameters don't affect the calling code (mutating the parameter does, but this is not possible for Strings, as they are immutable). – Hulk Nov 18 '21 at 10:11

2 Answers2

0

Try this code, worked in my test project


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;
    }
}

in activity


String currentMonth = MstrMonth.getCurrentMonth(currentMonth);
aref behboodi
  • 164
  • 2
  • 11
  • Thank you, but this gives also a error. String currentMonth = MstrMonth.getCurrentMonth(); this solves the error. – The Ark D Nov 18 '21 at 10:30
0

Try this.

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);
}

output:

NOV
  • Hi @sadetaosa ```String currentMonth = new SimpleDateFormat("MMM", Locale.ENGLISH).format(instance.getTime()).toUpperCase(); String currentMonth2 = instance.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.ENGLISH).toUpperCase(); ``` both two give "MMM" code. What is the best one? – The Ark D Nov 18 '21 at 10:42
  • @TheArkD As you like it. –  Nov 18 '21 at 10:48