Вот несколько способов получить время и дату
public static void getCurrentTimeUsingDate() {
Date date = new Date();
String strDateFormat = "hh:mm:ss a";
DateFormat dateFormat = new SimpleDateFormat(strDateFormat);
String formattedDate= dateFormat.format(date);
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
}
Время с использованием календаря
public static void getCurrentTimeUsingCalendar() {
Calendar cal = Calendar.getInstance();
Date date=cal.getTime();
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
String formattedDate=dateFormat.format(date);
Toast.makeText(this, formattedDate, Toast.LENGTH_SHORT).show();
}
Местноевремя и дата
public static void getCurrentTime(){
System.out.println("-----Current time of your time zone-----");
LocalTime time = LocalTime.now();
Toast.makeText(this, time, Toast.LENGTH_SHORT).show();
}
Зональное время
public static void getCurrentTimeWithTimeZone(){
Toast.makeText(this, "Current time of a different time zone using LocalTime", Toast.LENGTH_SHORT).show();
ZoneId zoneId = ZoneId.of("America/Los_Angeles");
LocalTime localTime=LocalTime.now(zoneId);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String formattedTime=localTime.format(formatter);
Toast.makeText (this, formattedTime, Toast.LENGTH_SHORT) .show ();
}
Простой способ узнать текущее время и дату
import java.util.Calendar
Date currentTime = Calendar.getInstance().getTime();