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
just use this code..
date.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
DatePickerDialog dateDlg = new DatePickerDialog(New_Quote_Activity.this, new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth)
{
CharSequence strDate = null;
Time chosenDate = new Time();
chosenDate.set(dayOfMonth, monthOfYear, year);
long dtDob = chosenDate.toMillis(true);
strDate = DateFormat.format("MM/dd/yyyy", dtDob);
txtDate.setText(strDate);
}}, year,month,day);
dateDlg.show();
}
});
Check this:
date format change in datepicker and calendar
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
if(month < 10){
month = "0" + month;
}
if(day < 10){
day = "0" + day ;
}
searchText.setText(day + "-" + month + "-" + year);
Try out this way:
private int m_month, m_year, m_day; SimpleDateFormat dateParser = new SimpleDateFormat("MM-dd-yyyy", Locale.ENGLISH); m_month = m_calendar.get(Calendar.MONTH) + 1; m_year = m_calendar.get(Calendar.YEAR); m_day = m_calendar.get(Calendar.DAY_OF_MONTH); //for the datepicker listener. m_calendar.set(Calendar.YEAR, p_year); m_calendar.set(Calendar.MONTH, p_monthOfYear); m_calendar.set(Calendar.DAY_OF_MONTH, p_dayOfMonth); String m_sDate = dateParser.format(m_calendar.getTime()).toString();
I tried following in my project. It might help you. Ask if you have any query.
try{
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy");
Date originalDate = new Date(); //it will give today's date.
String dateString = sdf.format(originalDate);
Date formattedDate = sdf.parse(dateString);
}catch(Exception e){
e.printStackTrace();
}
I want to display the date in 01-07-2013 MM-dd-yyyy format.
yes you can Do this by Below Steps.
Step 1: you First have to Convert your String to Particular Date Format
Step 2: Using SimpleDateFormat Convert your Date to MM-dd-yyyy format
Step 3: Convert to String and Use it.
you can User Below Code for this.
public class DateFormat {
public static void main(String[] args) {
// Put here what your are getting as String
String mytime="Jan 7, 2013";
SimpleDateFormat dateFormat = new SimpleDateFormat(
"MMM dd, yyyy"); //Step1: Convert yout String to perticular Date Formate according to your Date String your are getting
Date myDate = null;
try {
myDate = dateFormat.parse(mytime);
} catch (ParseException e) {
e.printStackTrace();
}
//Step2
SimpleDateFormat timeFormat = new SimpleDateFormat("MM-dd-yyyy");
String finalDate = timeFormat.format(myDate);
//Step3: Here you will get your Final Date. Set your String now to textview.
}
}
Hope it will help you.
You can add this code:
Date d = new Date(year, month, day);
SimpleDateFormat dateFormatter = new SimpleDateFormat(
"MM-dd-yyyy hh:mm");
strDate = dateFormatter.format(d);
after
year = calendar.get( Calendar.YEAR );
month = calendar.get( Calendar.MONTH );
day = calendar.get( Calendar.DAY_OF_MONTH );
hour = calendar.get( Calendar.HOUR );
minute = calendar.get( Calendar.MINUTE );
So your strDate will be in format you want (MM-dd-yyyy)