Reputation: 335
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
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
Reputation: 792
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