What is wrong with my code i couldn't get the exact output from the date time that i want to be parsed?
String convertDate="03/19/2014 5:30:10 PM";
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss", Locale.CANADA);
Date myDate=new Date();
try {
myDate=df.parse(convertDate);
final Calendar c = Calendar.getInstance();
c.setTime(myDate);
System.out.println("Year = " + c.get(Calendar.YEAR));
System.out.println("Month = " + (c.get(Calendar.MONTH)));
System.out.println("Day = " + c.get(Calendar.DAY_OF_MONTH));
} catch (ParseException e1) {
e1.printStackTrace();
}
And my output is
- 04-28 03:07:15.322: I/System.out(25095): Year = 2015
- 04-28 03:07:15.322: I/System.out(25095): Month = 6
- 04-28 03:07:15.322: I/System.out(25095): Day = 3
it must be
- Year = 2014
- Month = 03
- Day = 19
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.CANADA);
Now try... It'll work.
With this you have to add 1 with your month. code is here:
String convertDate="03/19/2014 5:30:10 PM";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.CANADA);
Date myDate=new Date();
try {
myDate=df.parse(convertDate);
final Calendar c = Calendar.getInstance();
c.setTime(myDate);
int y,m,d;
y=Calendar.YEAR;
System.out.println("Year = " + c.get(Calendar.YEAR));
//Log.d("Year", y);
System.out.println("Month = " + (c.get(Calendar.MONTH)+1));
System.out.println("Day = " + c.get(Calendar.DATE));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}