Tuesday, 1 May 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

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...