Format Timestamp for string without milliseconds?

0

I have the following variable public Timestamp CreatedAt; It is set to textview this way:

hora.setText(String.valueOf(notificacao.CreatedAt));

The output is:

  

2017-06-29 12: 31: 21,759

I would like it to be:

  

6/29/2017 12:31

How to do it?

    
asked by anonymous 29.06.2017 / 20:31

2 answers

2

Use SimpleDateFormat :

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
try {
    Date date = formatter.parse(notificacao.CreatedAt);
    String data = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(date);

} catch (ParseException e) {
    e.printStackTrace();
}
    
29.06.2017 / 20:37
1

You'll need to create a method that does the conversion, for example:

public static String convertDate(String mDate){

   SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
   try {
          Date newDate = inputFormat.parse(mDate);
          inputFormat = new SimpleDateFormat("dd/MM/yyyy");
          mDate = inputFormat.format(newDate);
    } catch (ParseException e) {
          e.printStackTrace();
    }

   return mDate;
}

That way, when setting the TextView, do the following:

hora.setText(convertDate(notificacao.CreatedAt));
    
29.06.2017 / 20:35