How To Get Datepicker Value In Date Format?
I have a problem with my Datapicker i use the code for getting date,month & year shown below DatePicker datePicker; datePicker = (DatePicker) findViewByI
Solution 1:
You can format the date like this :
intday= datePicker.getDayOfMonth();
intmonth= datePicker.getMonth();
intyear= datePicker.getYear();
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
SimpleDateFormat sdf =new SimpleDateFormat("dd-MM-yyyy");
String formatedDate = sdf.format(calendar.getTime());
You can parse the String back to Date object by calling
Datedate= sdf.parse(formatedDate);
Solution 2:
publicStringcheckDigit(int number)
{
returnnumber<=9?"0"+number:String.valueOf(number);
}
basic use:
month=checkDigit(month);
input 7
output 07
String date=checkDigit(monthOfYear+1)+"/"+checkDigit(dayOfMonth)+"/"+year;
Solution 3:
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="@+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="@+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
package com.mkyong.android;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
impot android.widget.TextView;
publicclassMyAndroidAppActivityextendsActivity {
private TextView tvDisplayDate;
private DatePicker dpResult;
private Button btnChangeDate;
privateint year;
privateint month;
privateint day;
staticfinalintDATE_DIALOG_ID=999;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCurrentDateOnView();
addListenerOnButton();
}
// display current datepublicvoidsetCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
dpResult = (DatePicker) findViewById(R.id.dpResult);
finalCalendarc= Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(newStringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// set current date into datepicker
dpResult.init(year, month, day, null);
}
publicvoidaddListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
@Overrideprotected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current datereturnnewDatePickerDialog(this, datePickerListener,
year, month,day);
}
returnnull;
}
private DatePickerDialog.OnDateSetListenerdatePickerListener=newDatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.publicvoidonDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
tvDisplayDate.setText(newStringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
}
Solution 4:
Starting from API level 12, DatePicker
has public CalendarView getCalendarView ()
, using this view you can get the date:
long dateTime = datePicker.getCalendarView().getDate();
Datedate = newDate(dateTime);
Then format if needed:
DateFormatdateFormat=newSimpleDateFormat("dd-MM-yyyy");
StringformattedDate= dateFormat.format(date);
Solution 5:
You can use a custom method for setting date and month in the desired format
privatestaticStringpad(int c)
{
return c>=10 ? ""+c : "0"+c;
}
Instead of using the values for date and month use pad(date)
and pad(month)
Hope this helps.
Post a Comment for "How To Get Datepicker Value In Date Format?"