Android Date Picker allows you to select the date consisting of day, month and year in your custom user interface. For this functionality android provides DatePicker and DatePickerDialog components
OUTPUT SCREEN :
Here is the way to implement DatePicker in application :
Here it the code declare variable and date format as well.
private static final int DIALOG_DATE = 1;
private static final int DIALOG_TIME = 2;
private Calendar dateTime = Calendar.getInstance();
private SimpleDateFormat
dateFormatter = new SimpleDateFormat(
"MMMM dd
yyyy");
private SimpleDateFormat
timeFormatter = new SimpleDateFormat(
"hh:mm a");
Set data to the date and time text view
txtstdate.setText(dateFormatter.format(dateTime.getTime()));
txtAddtime.setText(timeFormatter.format(dateTime.getTime()));
On calling this showDialog method, another method called onCreateDialog gets automatically called. So we have to override that method too. Its syntax is given below:
@Override
protected Dialog
onCreateDialog(int id)
{
switch (id)
{
case DIALOG_DATE:
return new DatePickerDialog(this, new OnDateSetListener()
{
@Override
public void onDateSet(DatePicker
view, int year,
int monthOfYear, int dayOfMonth)
{
dateTime.set(year,
monthOfYear, dayOfMonth); txtstdate.setText(dateFormatter.format(dateTime.getTime())); txtAdddate.setText(dateFormatter.format(dateTime.getTime()));
}
},
dateTime.get(Calendar.YEAR),
dateTime.get(Calendar.MONTH),
dateTime.get(Calendar.DAY_OF_MONTH));
case DIALOG_TIME:
return new TimePickerDialog(this, new OnTimeSetListener()
{
@Override
public void onTimeSet(TimePicker
view, int hourOfDay,
int minute)
{
dateTime.set(Calendar.HOUR_OF_DAY,
hourOfDay);
dateTime.set(Calendar.MINUTE,
minute);
txtsttime.setText(timeFormatter.format(dateTime.getTime()));
txtAddtime.setText(timeFormatter.format(dateTime.getTime()));
}
},
dateTime.get(Calendar.HOUR_OF_DAY),
dateTime.get(Calendar.MINUTE),
false);
}
return null;
}
}
Call dialog :
In order to show DatePickerDialog , you have to pass the DatePickerDialog id to showDialog(id) method.
showDialog(DIALOG_DATE);
showDialog(DIALOG_TIME);
Show Current Date and Time Format with Continuously update on text view:
private void Start_Timer()
{
timer1
= new Timer();
timer1.schedule(new UpdateTimeTask(),
1000,1000);
}
class UpdateTimeTask extends TimerTask
{
public void run()
{
Calendar
cal = Calendar.getInstance();
SimpleDateFormat
dateFormatter = new SimpleDateFormat(
"EEEE, MMMM d,
yyyy");
dt=dateFormatter.format(cal.getTime());
if(Chk_DateFormate.isChecked())
{
SimpleDateFormat
timeFormatter = new SimpleDateFormat("H:mm");
time=timeFormatter.format(cal.getTime());
}
else
{
SimpleDateFormat
timeFormatter = new SimpleDateFormat("hh:mm a");
time=timeFormatter.format(cal.getTime());
}
setTimertext(time);
}
}
public void setTimertext(String
strValue)
{
runOnUiThread(new Runnable()
{
public void run()
{
String[]time1=time.split(" ");
if(time.endsWith("M"))
{
txt_time.setText(""+time1[0]);
txt_AMPM.setText(""+time1[1]);
}
else
{
txt_time.setText(time);
txt_AMPM.setText("");
}
txt_date.setText(dt);
}
});
Date Format :
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate1 = df1.format(c.getTime());
SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate2 = df2.format(c.getTime());
SimpleDateFormat df3 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a");
String formattedDate3 = df3.format(c.getTime());
System.out.println("=========> Date 1 => "+formattedDate1);
System.out.println("=========> Date 2 => "+formattedDate2);
System.out.println("=========> Date 3 => "+formattedDate3);
I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)
Date Format :
Calendar c = Calendar.getInstance();
System.out.println("Current time => " + c.getTime());
SimpleDateFormat df1 = new SimpleDateFormat("dd-MMM-yyyy");
String formattedDate1 = df1.format(c.getTime());
SimpleDateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");
String formattedDate2 = df2.format(c.getTime());
SimpleDateFormat df3 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss a");
String formattedDate3 = df3.format(c.getTime());
System.out.println("=========> Date 1 => "+formattedDate1);
System.out.println("=========> Date 2 => "+formattedDate2);
System.out.println("=========> Date 3 => "+formattedDate3);
I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)
No comments:
Post a Comment