For the current Time only
long currentDateTime = System.currentTimeMillis();
@SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");
String currentTime = simpleDateFormat.format(currentDateTime);
Toast.makeText(this, currentTime, Toast.LENGTH_SHORT).show();
and if you want the Date & Time both then the below code is for you
long currentDateTime = System.currentTimeMillis();
@SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy h:mm a");
String currentDateTime = simpleDateFormat.format(currentDateTime);
Toast.makeText(this, currentDateTime, Toast.LENGTH_SHORT).show();
EDIT 1
another method to do this
LocalDateTime myDt= LocalDateTime.now();
Toast.makeText(this, myDt.getHour()+":"+myDt.getMinute()+":"+myDt.getSecond(), Toast.LENGTH_SHORT).show();
EDIT 2
another method to get Date only
LocalDateTime myDt= LocalDateTime.now();
DateTimeFormatter newDt = DateTimeFormatter.ISO_DATE;
String dates = newDt.format(myDt);
Toast.makeText(this,dates, Toast.LENGTH_LONG).show();
You can get more options to get a date, time, GMT, or more by changing from ISO_DATE
to ISO_DATE_TIME
, ISO_LOCAL_DATE_TIME
etc.