title: Save current date and time in Android
body: <p>First of all I read many posts about Date and found out how to get Date and customize it. But my problem is that I need current date for every time I press button. To be more clear:</p>
  
  <pre><code>DateFormat date = new SimpleDateFormat("MMM dd yyyy, h:mm");
  String dateFormat = date.format(Calendar.getInstance().getTime());
  </code></pre>
  
  <p>And after that I'm calling my Model class to setDate <code>gramHelp.setDate(dateFormat);</code> 
  That works fine but when I check list of dates in my SQL database I only get current date and time for this moment and not for moment when I saved the date. The same problem is in my another activity where I'm calling <code>getDate()</code> to list all dates in database. </p>
  
  <pre><code>DateFormat datum = new SimpleDateFormat("MMM dd yyyy, h:mm");
  String date = datum.format(Calendar.getInstance().getTime());
  holder.date.setText(date)
  </code></pre>
  
  <p>The code is basically the same, just this time I'm calling <code>getTime()</code> and not <code>setTime()</code>. </p>
  
  <p>For example, this is the list of dates:</p>
  
  <p><a href="https://i.stack.imgur.com/ur0MY.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ur0MY.jpg" alt="enter image description here"></a></p>
  
  <p>for the moment when I'm writing this post. You can see that I have 5 inputs, but I did that every few minutes. Nevertheless in SQL database and my list this is result.</p>
  
  <p>So how to get (save) date and time exactly when I pressed the button ? I guess the answer is really simple but still didn't manage to find it.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>In first activity I have FAB that saves date/time in SQL database after click.</p>
  
  <pre><code> public void onClick(View v) {        
              DateFormat date = new SimpleDateFormat("MMM dd yyyy, h:mm");
              String dateFormat = date.format(Calendar.getInstance().getTime());
              GramHelp gramHelp = new GramHelp();              
              gramHelp.setDate(dateFormat);
              mDatabase.addGramTotal(gramHelp);
          }
  </code></pre>
  
  <p><code>GramHelp</code>is my model class where I have all constructors and getter() and setter() methods. I am also calling <code>addGramTotal()</code> function from my database. This is simple function with <code>ContentValues</code> for inserting value in my SQL database.</p>
  
  <pre><code>public void addGramTotal(GramHelp gramHelp){
      ContentValues values = new ContentValues();      
      values.put(FoodEntry.COLUMN_DATE, String.valueOf(gramHelp.getDate()));       
      SQLiteDatabase db = this.getWritableDatabase();
      db.update(FoodEntry.TABLE_NAME_HELPER, values, null, null);
  }
  </code></pre>
  
  <p>To display all dates/times in SQL database I'm using RecyclerView. In activity with RecyclerView I am calling one more function called <code>listHistory()</code>. This function is simple <code>SELECT * FROM table ORDER BY COLUMN_DATE</code>. That part should be fine. </p>
  
  <p>And also in my Adapter class for RecyclerView inside <code>onBindViewHolder()</code> method I have: </p>
  
  <pre><code>public void onBindViewHolder(HistoryAdapter.HistoryViewHolder holder, final int position) {               
      DateFormat datum = new SimpleDateFormat("MMM dd yyyy, h:mm");
      String date = datum.format(Calendar.getInstance().getTime());
      holder.date.setText(date);     
  }
  </code></pre>
  
  <p><strong>SECOND UPDATE:</strong></p>
  
  <p>So this is of my table where date/time is saved. <a href="https://i.stack.imgur.com/BR0Vt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BR0Vt.png" alt="screenshoot"></a> 
  As you can see, time is showing only newest insert.
  This is my whole <code>SELECT</code> code from my database. You can see that I am saving more values in this table. Also I set date as <code>String</code> because I couldn't get <code>Date</code> for some reason (in RecyclerView Adapter method <code>onBindViewHolder</code>). It works with <code>String</code>. </p>
  
  <p>But problem shouldn't be with retrieving date/time. Because date/time is changed (updated) every time when I insert new value into table.</p>
  
  <p>Also this is <a href="https://github.com/infinum/android_dbinspector" rel="nofollow noreferrer">library</a> I use to inspect my database. </p>
  
  <pre><code>public List&lt;GramHelp&gt; listHistory(){
      String sql = "SELECT * FROM " +FoodEntry.TABLE_NAME_HELPER + " ORDER BY " +FoodEntry.COLUMN_DATE + " ASC ";
      SQLiteDatabase db = this.getReadableDatabase();
      List&lt;GramHelp&gt; listDate = new ArrayList&lt;&gt;();
      Cursor cursor = db.rawQuery(sql, null);
      if (cursor.moveToFirst()){
          do {
              int id = (cursor.getInt(0));
              int id_food = (cursor.getInt(1));
              int id_menu = (cursor.getInt(2));
              double gram = cursor.getDouble(3);
              double gram_total = cursor.getDouble(4);
              String date = new String(cursor.getString(5));
              listDate.add(new GramHelp(id, id_food, id_menu, gram, gram_total, date));
          } while (cursor.moveToNext());
      }
      cursor.close();
      return listDate;
  }
  </code></pre>