Search code examples
javaandroiddatabasedatesimpledateformat

Android convert numbers into date format


databaseI am storing month and year in my android database. I want to fetch it and convert it to date format so that I can match it with current month and year present in array. Im fetching it through ArrayList but how to convert in date format and match?

private ArrayList<String> getGspApprovedMonthData() {
    List<Date> dates = new ArrayList<>(gspApprovedMonth.size());
    gspApprovedMonth.clear();
    SqlDataStore sd = new SqlDataStore(this);
    sd.open();
    String gspQuery = " SELECT * FROM "+ TABLE_GSP_APPROVED_DATA;
    Cursor gspCu = sd.getData(gspQuery);
    if(gspCu.moveToFirst()){
        do {
            String gspMonth = gspCu.getString(gspCu.getColumnIndex(Queryclass.GSP_APPROVED_MONTH));
            String gspYr = gspCu.getString(gspCu.getColumnIndex(Queryclass.GSP_APPROVED_YEAR));

            gspApprovedMonth.add(gspMonth+gspYr);
        } while (gspCu.moveToNext());
    }
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    for (String dateString : gspApprovedMonth) {
        try {
            dates.add(sdf.parse(dateString));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    gspCu.close();
    sd.close();
    return gspApprovedMonth;
}

Solution

  • java.time

    The java.util date-time API and their formatting API, SimpleDateFormat were supplanted by the modern date-time API in Mar 2014. Since then, it is highly recommended to stop using the legacy date-time API.

    Solution using java.time, the modern date-time API:

    The java.time API provides you with Year that you can combine with a month using Year#atMonth to get a YearMonth. A YearMonth can be compared with another using its methods like YearMonth#isAfter, YearMonth#isBefore etc.

    Demo:

    import java.time.Year;
    import java.time.YearMonth;
    
    class Main {
        public static void main(String[] args) {
            // Sample year and month strings
            String strYear = "2022";
            String strMonth = "9";
            YearMonth ym = Year.of(Integer.parseInt(strYear))
                               .atMonth(Integer.parseInt(strMonth));
            System.out.println(ym);
    
            // Comparing two instances of YearMonth
            YearMonth currentYm = YearMonth.now();
            if (ym.isAfter(currentYm))
                System.out.println(ym + " is after " + currentYm);
            else
                System.out.println(ym + " is before or equal to " + currentYm);
        }
    }
    

    Output:

    2022-09
    2022-09 is before or equal to 2023-01
    

    ONLINE DEMO

    How to implement it in your code?

    List<YearMonth> yearMonths = new ArrayList<>(gspApprovedMonth.size());
    
    // ...
    
    if(gspCu.moveToFirst()){
        do {
            String gspMonth = gspCu.getString(gspCu.getColumnIndex(Queryclass.GSP_APPROVED_MONTH));
            String gspYr = gspCu.getString(gspCu.getColumnIndex(Queryclass.GSP_APPROVED_YEAR));
    
            yearMonths.add(Year.of(Integer.parseInt(gspYr))
                               .atMonth(Integer.parseInt(gspMonth)));
            // ...
        } while (gspCu.moveToNext());
    }
    // ...
    

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