pavan kumar
pavan kumar

Reputation: 335

Convert string to date using SimpleDateFormat

I am doing application in android platform. My application will retrieve date from MySQL as a string format, again i am trying to convert to date format.

Upvotes: 2

Views: 4821

Answers (2)

Tarsem Singh
Tarsem Singh

Reputation: 14199

try

String DateFromDb = "your Date from DB";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//set format of date you receiving from db
Date date = (Date) sdf.parse(DateFromDb);
SimpleDateFormat newDate = new SimpleDateFormat("yyyy-MM-dd");//set format of new date
System.out.println(newDate.format(date));
String ActDate = newDate.format(date);// here is your new date !

Upvotes: 1

To convert String to date you can use SimpleDateFormat's parse function.

String myFormat = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
Date dateObj = sdf.parse(dateStr);
Calendar myCal = Calendar.getInstance();
myCal.setTime(dateObj);

Upvotes: 4

Related Questions