Tuesday, May 1, 2018

java - Parsing String to Date error in Android





I am facing a few date parsing errors in Android. I actually want the date in the format yyyy-MM-ddThh:mm:ss (2014-08-12T00:00:00).



I did the parsing in two ways, one using present date and using the date picker.



a) Using present date:




String d = DateFormat.getDateTimeInstance().format(new Date()).toString();


Here, value of "d" is Jun28, 2014 12:17:30AM
I tried parsing this using SimpleDateFormatter to get (yyyy-MM-dd),but it didnt work. The error was cannot parse String.



b) Using Date Picker, I was able to get the date by default as dd/MM/yyyy. I was able to parse that to yyyy-MM-dd. But, if the date or month is a single digit numbers, it takes as d/M/yyyy.



How is it possible to get date as yyyy-MM-ddThh:mm:ss ?



Answer



Here's some code I've used for inserting into databases



protected String formatDate(Date date){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(date);
}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...