0

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 ?

AndyN
  • 1,742
  • 1
  • 15
  • 30
Roshan
  • 497
  • 1
  • 6
  • 16

4 Answers4

2

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);
}
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

Try this..

String dd = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss").format(new Date())
Hariharan
  • 24,741
  • 6
  • 50
  • 54
1

You can get the date in just about any form you want by specifying how you want the output formatted when you create the SimpleDateFormat. You add the "T" by surrounding it with single quotes, as follows:

String formattedDate = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss").format(new Date());

For a handy simple date formatting reference, check out this page: http://developer.android.com/reference/java/text/SimpleDateFormat.html

VinceFior
  • 1,279
  • 13
  • 25
0
 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss"); 
    String finalDate=dateFormat.format(new Date());
Girish
  • 1,717
  • 1
  • 18
  • 30