Skip to Content

Hi Folks,

I was developing native android application with SMP SDK SP06 using SMP 3.0 SP05 with the backend MS SQL Server. While developing the application, i need to insert the DateTime using ODATA in non-SAP backend (SQL Server Database) in which i struggled but finally cracked. So, thought to share my experiences through this blog which may also give you some dramatic feeling 😀 !!!

I knew the format of inserting DateTime in backend using OData as i had already done this in my SAP Fiori/UI5 applications. So, i directly began with the known format of inserting DateTime in backend using OData services. And the known format was ‘yyyyMMdd’, so i converted the date in the same format and executed the query to insert date in my table. But no record was inserted and my Http conversation with the backEnd was null and response was 400 bad request.

After getting this error i changed the format to ‘yyyyMMddT:hh:mm:ss’ and ‘yyyyMMddT:hh:mm:ss.zzz’  which return me 500 internal server error. So, now i thought to test it with rest client first. I performed the PUT request with the rest client and tried to update the existing date in the table. And from the Rest Client, first i used the format ‘yyyyMMddT:hh:mm:ss’ to update table and it give my 400 Bad request error. After getting this error, i changed my format to ‘yyyyMMdd’ to update date and this time i got the status code of 204 i.e., of success. After getting the success response i checked into my table, and in table also it was successfully updated.

So, now again i tried to insert/update date in table with ‘yyyyMMdd’ format from my fronEnd but every time it was giving me the same null result and was not inserting/updating the date. Then i searched on google and by reading some of the links, i checked my data type of column which was correct i.e., DateTime.

I also tried to use Java util class and Java SQL date time methods to pass date into table but hard luck and could not succeed.

While searching on google, somewhere i read that OData v2.0 has some issues with the dateTime format which was resolved in OData v4.0, so i also updated the version of OData to 4.0 which also did not help me in inserting date into table. Along with this i also tried different different format like DataTime/DateTime Offset/ Calendar to insert/update date in my table but no luck.

Till now i have wasted my two days in searching and investigating that why the date is not getting insert/update? In the mean while i also posted a discussion on SCN How to insert datetime through ODATA in Native Android App using SMP 3.0 about my issue and i got some quick responses from JK but i could not get success. Now i started feeling pressure and getting tensed as it was very important for me to crack this issue as it was on client’s top priority. So, every single minute i was thinking about the issue that why the date is not inserting or getting updated?

Then, suddenly i got a click in my mind and realized that when i was reading the data from back-end/performing the read(GET) operation, the format in which i was getting Date was unique and that foramt was ‘Gregorian Calendar’.

So, now i converted my date into the Gregorian Calendar format and tried to insert and update the date from the front-end. And this time my Http Conversation was not null i.e., my front-End successfully communicated with my back-end. and return me the status code of ‘204’.  To cross check, i checked my table. Entry was successfully inserted with date time.

Hence, my 3 days struggle comes to an end. So thought to share my experience with you and writing this blog so that it may help the newbies like me or anyone performing the same action facing the same issue might save his precious time.

Below is the code which helped me.


// For inserting/Updating date from Front-end
DateFormat df = new SimpleDateFormat("yyyy/MM/dd"); // Defining the Format of date
Date dateFirst = null;
try
 {
dateFirst = df.parse("20140912"); // Parsing the date into above defined format
}
catch(Exception e) {
  e.printStackTrace();
}
Calendar curCal = new GregorianCalendar();
curCal.setTime(dateFirst);
createBean.setDate(curCal);    // Stting the date to the date property of Bean Class
//Then we can get the date the property from Bean Class at the time of ScheduleCreate by writing below line of code
ODataEntityName.getProperties().put("entityName", new ODataPropertyDefaultImpl("entityName", createBean.getDate());

For reading Date from Back-end.


property = properties.get(date);
try {
   if (property !=null) {
   Calendar temp = new GregorianCalendar();
  temp = (GregorianCalendar) property.getValue();
   int tempDate = temp.get(Calendar.DATE);
   int tempYear = temp.get(Calendar.YEAR);
    tempMonth = temp.get(Calendar.MONTH);
String date = tempDate+"/"+tempMonth+"/"+tempYear; // Setting the format in which we wants to display the date
}
catch(Exception e){
e.printStackTrace();
}

cheers!!! 🙂

Deepak Sharma

To report this post you need to login first.

2 Comments

You must be Logged on to comment or reply to a post.

  1. Hemendra Sabharwal

    Hi Deepak,

    Very useful information & shared it nicely. But to me this still reminds, the hard work  and struggle of whole night with you guys… 🙂

    Thanks,

    Warm Regards

    Hemendra

    (0) 

Leave a Reply