-1

I need to find a balance months in my table. I did in numeric form, but I need in alphabetic form like "January, February". Please help

    int lastpaid = Integer.parseInt(s); **//this is i am getting from my database**
    int currentmonth = m; **// this is from georigian Calender**
    int[] arr = new int[12];
    int[] month = new int[12];
    int count = 0;

    for(int j=currentmonth;j>=lastpaid+1;j--)
    {
       arr[count] = j;
       count++;
    }


    for(int k : arr)
    {
        if(k == 0)
            {
                break;
            }

        month[count] = k;

    }
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • 1
    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. – Ousmane D. May 17 '17 at 17:42
  • So, you found it but need it in text? – litelite May 17 '17 at 17:42
  • 4
    Possible duplicate of [How can I convert an Integer to localized month name in Java?](http://stackoverflow.com/questions/1038570/how-can-i-convert-an-integer-to-localized-month-name-in-java) – litelite May 17 '17 at 17:43
  • bro i get output like (5,4,3,2) (4,3,2) etc i want to convert and show in alphabetic form – Muhammad Zohaib May 17 '17 at 17:51
  • What you are describing is just a simple mapping of integer to string. If 1 then January, etc... Not sure why you are thinking this is so much more complicated. – takendarkk May 17 '17 at 17:57

2 Answers2

5

tl;dr

Month.of( 5 ).getDisplayName( TextStyle.FULL , Locale.US )

May

java.time

The modern approach uses the java.time classes. The Month enum provides an object for each month of the year. You can retrieve each by a number. Then ask the Month name to generate a localized string for name of that month.

int[] monthNumbers = { 5,4,3,2} ;
for ( int monthNumber : monthNumbers ) {
    Month month = Month.of( monthNumber );  // Get a month for number, 1-12 for January-December.
    String monthName = month.getDisplayName( TextStyle.FULL , Locale.US ) ;  // Or Locale.CANADA_FRENCH etc.
    System.out.println(monthName);
}

When run.

May

April

March

February


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

you can get String description of a month from int using 1) array of month or 2) own method.

Case 1:

String[] monthString = new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

Later, in the code, you can call it like: EXAMPLE:

 for(int i=0; i<12; i++) {
       System.out.println("Month: "+monthString[i]);
    }

Assuming, we have case when we need to get month some times (5,4,3,2);

...(monthString[5], monthString[4], monthString[3], monthString[2]) ..

Case 2:

   public String getMonth(int month){
        String monthString;
        switch (month) {
            case 1:  monthString = "January";
                     break;
            case 2:  monthString = "February";
                     break;
            case 3:  monthString = "March";
                     break;
            case 4:  monthString = "April";
                     break;
            case 5:  monthString = "May";
                     break;
            case 6:  monthString = "June";
                     break;
            case 7:  monthString = "July";
                     break;
            case 8:  monthString = "August";
                     break;
            case 9:  monthString = "September";
                     break;
            case 10: monthString = "October";
                     break;
            case 11: monthString = "November";
                     break;
            case 12: monthString = "December";
                     break;
            default: monthString = "Invalid month";
                     break;
        }
        return monthString;
    }

EXAMPLE:

   for(int i=0; i<12; i++) {
       System.out.println("Month: "+getMonth(i));
    }

OUTPUT:

Month: January
Month: February
Month: March
Month: April
Month: May
Month: June
Month: July
Month: August
Month: September
Month: October
Month: November
Month: December
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38