I'm receiving a db2 date as 1200703 to the mobile as the response. I need to convert that date to a readable format as YYYYMMDD. How can I do that from the android side?
- 
                    Have you seen these answers: https://stackoverflow.com/questions/10320918/db2-date-format – jmizv Aug 05 '20 at 05:27
- 
                    yes, but they are SQL queries. I need some java code to convert the db2 date from the mobile side – Udara Abeythilake Aug 05 '20 at 05:30
2 Answers
This is not db2 date format, but rather the way some systems store a date. It's so called CYYMMDD integer format.
YEAR = 1900 + 100*C + YY
MONTH = MM
DAY = DD
 
    
    - 11,456
- 2
- 8
- 16
The date string, 1200703 is in CYYMMDD format. This format was (I'm not sure if it is still in use as the last time when I used DB2 was in 2008) used by DB2.
In order to calculate the year, you need to use the following formula:
Year = 100 * C + 1900 + YY e.g. for CYY = 120, the value of year = 100 * 1 + 1900 + 20 = 2020.
Once you convert the CYY part into yyyy format, you can use date-time formatting API as shown below:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class Main {
    public static void main(String args[]) {
        // Given date string
        String dateStr = "1200703";
        // Convert the given date string into yyyyMMdd format
        int c = Integer.parseInt(dateStr.substring(0, 1));
        int yy = Integer.parseInt(dateStr.substring(1, 3));
        int year = 100 * c + 1900 + yy;
        String dateStrConverted = String.valueOf(year) + dateStr.substring(3);
        // ########## For Java 8 onwards ##############
        // Define a formatter
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate localDate = LocalDate.parse(dateStrConverted, dtf);
        System.out.println("Default format: " + localDate);
        // Printing the date in a sample custom format
        DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("EEE MMM dd yyyy");
        String strDate1 = dtf1.format(localDate);
        System.out.println(strDate1);
        // ############################################
        // ############## Before Java 8 ###############
        // Define a formatter
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date utilDate = null;
        try {
            utilDate = sdf.parse(dateStrConverted);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("Default format: " + utilDate);
        // Printing the date in a sample custom format
        SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd yyyy");
        String strDate2 = sdf1.format(utilDate);
        System.out.println(strDate2);
        // ############################################
    }
}
Output:
Default format: 2020-07-03
Fri Jul 03 2020
Default format: Fri Jul 03 00:00:00 BST 2020
Fri Jul 03 2020
Note: I recommend you use the modern date-time API. If the Android version which you are using is not compatible with Java-8, I suggest you backport using ThreeTen-Backport library. However, if you want to use the legacy API, you can use do so as shown in the answer.
 
    
    - 71,965
- 6
- 74
- 110
- 
                    Actually, the data type in Db2 is not a String. It's some numeric data type like INT or DECIMAL. So, if you convert it to String, you should pad it with zeroes at the left to get a String of 7 characters. You get wrong result on dates of 20-th century otherwise. I.e. `1900-01-01` comes as `101`, and not as `0000101`. – Mark Barinstein Aug 05 '20 at 11:14
