Get the current android time and date

In the application, you need to display the date and time in two separate textviews. I have already tried everything I could find on the Internet, for example:

SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
textview.setText(format.format(new Date()));

DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm");
String date = df.format(Calendar.getInstance().getTime());

And many more different examples, and as a result-I either do not output anything in textview or the application crashes, as an error outputs for example problems with the Gregorian calendar or something else like that, well, that is, it shows that the problem is in some deep class that is responsible for the function that I call, and I do not I want to get into those classes because I didn't create them and I can disrupt their work by interfering. If someone has encountered similar problems or knows how to solve mine, I will be grateful for your help.

Author: Andrew, 2018-07-09

1 answers

Both of your options should be working. Go through the code with the debugger, see what the methods return, SimpleDateFormat.format(...) should return a formatted representation of the date as a string.

Similar option, separate date and time:

// Текущее время
Date currentDate = new Date();
// Форматирование времени как "день.месяц.год"
DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());
String dateText = dateFormat.format(currentDate);
// Форматирование времени как "часы:минуты:секунды"
DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
String timeText = timeFormat.format(currentDate);

textViewDate.setText(dateText);
textViewTime.setText(timeText);

Used imports:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
 3
Author: AleksanderSh, 2018-07-09 19:46:43