I have a datepicker. My app is pushing a notification. I want to display the date in 01-07-2013 MM-dd-yyyy format. Please check my code below:
//---Button v
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainClass {
public static void main(String[] args) {
String pattern = "MM-dd-yyyy";
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
Date date = format.parse("01-07-2013");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println(format.format(new Date()));
}
}
Hope its helpful.
You can also refer to the documentation.
Try this:
CharSequence formatted = DateFormat.format("MM/dd/yyyy", mDueDate);
mDueDateView.setText(formatted);
try this
int year = mDatePicker.getYear();
int month = mDatePicker.getMonth();
int day = mDatePicker.getDayOfMonth();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
String strDate = format.format(calendar.getTime());
A simple way to do this might be to simply collect the data and then use String.format:
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH) + 1;
int day = calendar.get(Calendar.DAY_OF_MONTH);
String dateString = String.format("%02d-%02d-%d", month, day, year);
((TextView) m_view.findViewById(R.id.dob)).setText(dateString);