如何在Android应用程序中获取当前时间和日期?


当前回答

对于后缀为“AM”或“PM”的12小时时钟:

DateFormat df = new SimpleDateFormat("KK:mm:ss a, dd/MM/yyyy", Locale.getDefault());
String currentDateAndTime = df.format(new Date());

对于带有“AM”或“PM”后缀的24小时时钟:

 DateFormat df = new SimpleDateFormat("HH:mm:ss a, dd/MM/yyyy", Locale.getDefault());
 String currentDateAndTime = df.format(new Date());

要删除后缀,只需删除以时间格式书写的“a”。

其他回答

如果你想以特定的模式获取日期和时间,你可以使用以下命令:

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
String currentDateandTime = sdf.format(new Date());

Or,

日期:

String currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());

时间:

String currentTime = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(new Date());

Java

Long date=System.currentTimeMillis();
SimpleDateFormat dateFormat =new SimpleDateFormat("dd / MMMM / yyyy - HH:mm", Locale.getDefault());
String dateStr = dateFormat.format(date);

科特林

日期(毫秒)和13位数字(十六进制到日期)

val date=System.currentTimeMillis() //here the date comes in 13 digits
val dtlong = Date(date)
val sdfdate = SimpleDateFormat(pattern, Locale.getDefault()).format(dtlong)

日期格式化程序

"dd / MMMM / yyyy - HH:mm" -> 29 / April / 2022 - 12:03 
"dd / MM / yyyy" -> 29 / 03 / 2022
"dd / MMM / yyyy" -> 29 / Mar / 2022 (shortens the month) 
"EEE, d MMM yyyy HH:mm:ss" -> Wed, 4 Jul 2022 12:08:56
Date todayDate = new Date();
todayDate.getDay();
todayDate.getHours();
todayDate.getMinutes();
todayDate.getMonth();
todayDate.getTime();

下面是一些获取时间和日期的方法:

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();

如果需要当前日期:

Calendar cc = Calendar.getInstance();
int year = cc.get(Calendar.YEAR);
int month = cc.get(Calendar.MONTH);
int mDay = cc.get(Calendar.DAY_OF_MONTH);
System.out.println("Date", year + ":" + month + ":" + mDay);

如果需要当前时间:

 int mHour = cc.get(Calendar.HOUR_OF_DAY);
 int mMinute = cc.get(Calendar.MINUTE);
 System.out.println("time_format" + String.format("%02d:%02d", mHour , mMinute));