Thursday 2 May 2013

Date and time Dialog in Android


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");


       txtstdate.setText(dateFormatter.format(dateTime.getTime()));  
       txtAddtime.setText(timeFormatter.format(dateTime.getTime()));


       @Override
       protected Dialog onCreateDialog(int id)
       {
              switch (id)
              {
              case DIALOG_DATE:
                     return new DatePickerDialog(thisnew 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(thisnew 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:

       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);

              }
       });

No comments:

Post a Comment