Search code examples
androidbroadcastreceiverdate-format

working of DateFormat.getDateTimeInstance().format(date)


I am confused about the working of DateFormat.getDateTimeInstance().format(date) In my code I am doing this:

BroadcastReceiver myReciever = new BroadcastReceiver(){
 String currentDateTimeString;
 @Override
 public void onReceive(Context context, Intent intent){
    currentDateTimeString = DateFormat.getDateTimeInstance().format(date);
    switch(prevRAT)       //somevariable
    {
        case 0:
          det1.setText(currentDateTimeString);    //det is an EditText variable
          break;
        case 1:
          det2.setText(currentDateTimeString;
          break;
        default:
          break;
    }
  }
};

The next time the control enters into the onReceive function the currentDateTimeString is not updated and it shows the same value of date and time. I cant understand this behavior. How to get the current time.


Solution

  • BroadcastReceiver myReciever = new BroadcastReceiver(){
     String currentDateTimeString;
     @Override
     public void onReceive(Context context, Intent intent){
        currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
        switch(prevRAT)       //somevariable
        {
            case 0:
              det1.setText(currentDateTimeString);    //det is an EditText variable
              break;
            case 1:
              det2.setText(currentDateTimeString;
              break;
            default:
              break;
        }
      }
    };