1

With this query I select the month in the database. The cursor returns the exact month, but when I do the conversion to display the full name appears the following month. For example, the cursor returns 6, instead the method returns getDisplayName July.

SQLiteDatabase db = new DatabaseHelper(getActivity()).getReadableDatabase();

    String sql = "SELECT DISTINCT strftime('%m',"+Table.DATA+") FROM "+Table.TABLE_NAME;
    Cursor c = db.rawQuery(sql, null);

    while (c.moveToNext()){
        int monthNumero = (c.getInt(0));

        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.MONTH, monthNumero);
        String monthName = ca.getDisplayName
                (Calendar.MONTH, Calendar.LONG, Locale.getDefault()).toUpperCase();

        result1.add(monthName);

    }
    db.close();
user2847219
  • 555
  • 1
  • 16
  • 27

2 Answers2

3

For example, the cursor returns 6, instead the method returns getDisplayName July.

Yes, it would. Because the Calendar.MONTH "logical field" is 0-based. (Crazy decision, but...)

Basically you need to subtract 1 from the month number:

ca.set(Calendar.MONTH, monthNumero - 1);

For example Calendar.JANUARY is documented as having a value of 0.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • ok, But now I see that the month name in a spinner, and I have to convert to an integer to make another query. I do this: ` String mese = monthSelector.getSelectedItem().toString(); Calendar cal = Calendar.getInstance(); try { cal.setTime(new SimpleDateFormat("MMMM").parse(mese)); } catch (ParseException e) { e.printStackTrace(); } int monthInt = cal.get(Calendar.MONTH + 1); String monthConverted = ""+monthInt; if(monthInt<10){ monthConverted = "0"+monthConverted; } `   but I get the month as number 23! why? – user2847219 Jun 18 '14 at 20:28
  • 1
    @user2847219: Because you want `cal.get(Calendar.MONTH) + 1`, not `cal.get(Calendar.MONTH + 1)` - you're currently getting a different field entirely! – Jon Skeet Jun 18 '14 at 20:29
1

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to java.time, the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

import java.time.Month;

public class Main {
    public static void main(String[] args) {
        int monthNumero = 6;
        String monthName = Month.of(monthNumero).toString();
        System.out.println(monthName);
    }
}

Output:

JUNE

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Table.DATA is **not** of type DATE because there is no DATE data type in SQLite: https://www.sqlite.org/datatype3.html Also, this is a question about Android and SQLiteDatabase class which uses code agnostic of the database's location in the device and manages access to it. Your code uses Java to access the database without providing the means to connect to the database. What is conn? How do you establish connection to the database? – forpas Jul 05 '21 at 17:09
  • Thanks, @forpas for pointing it out. Much appreciated! I did not know about SQLiteDatabase. I have updated the answer now. – Arvind Kumar Avinash Jul 05 '21 at 18:03