如何在Android应用程序中获取当前时间和日期?
当前回答
//currentTimeMillis is System.currentTimeMillis()
long totalSeconds = currentTimeMillis / 1000;
int currentSecond = (int)totalSeconds % 60;
long totalMinutes = totalSeconds / 60;
int currentMinute = (int)totalMinutes % 60;
long totalHours = totalMinutes / 60;
int currentHour = (int)totalHours % 12;
TextView tvTime = findViewById(R.id.tvTime);
tvTime.setText((currentHour + OR - TIME YOU ARE FROM GMT) + ":" + currentMinute + ":" + currentSecond);
其他回答
实际上,使用time . getcurrenttimezone()在设备上设置当前时区更安全,否则您将获得UTC的当前时间。
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
然后,你可以得到你想要的所有日期字段,例如:
textViewDay.setText(today.monthDay + ""); // Day of the month (1-31)
textViewMonth.setText(today.month + ""); // Month (0-11)
textViewYear.setText(today.year + ""); // Year
textViewTime.setText(today.format("%k:%M:%S")); // Current time
详情请参阅android.text.format.Time类。
更新
正如许多人指出的那样,谷歌说这个类有一些问题,不应该再使用了:
This class has a number of issues and it is recommended that GregorianCalendar is used instead. Known issues: For historical reasons when performing time calculations all arithmetic currently takes place using 32-bit integers. This limits the reliable time range representable from 1902 until 2037.See the wikipedia article on the Year 2038 problem for details. Do not rely on this behavior; it may change in the future. Calling switchTimezone(String) on a date that cannot exist, such as a wall time that was skipped due to a DST transition, will result in a date in 1969 (i.e. -1, or 1 second before 1st Jan 1970 UTC). Much of the formatting / parsing assumes ASCII text and is therefore not suitable for use with non-ASCII scripts.
下面是一些获取时间和日期的方法:
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();
要获取当前时间,您可以使用System.currentTimeMillis(),这是Java中的标准。然后您可以使用它来创建日期
Date currentDate = new Date(System.currentTimeMillis());
正如其他人提到的创造一个时间
Time currentTime = new Time();
currentTime.setToNow();
对于后缀为“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”。
试着使用下面的代码:
Date date = new Date();
SimpleDateFormat dateFormatWithZone = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.getDefault());
String currentDate = dateFormatWithZone.format(date);
推荐文章
- Keytool错误:java.io。IoException:错误的AVA格式
- GCM与PHP(谷歌云消息传递)
- 自定义圆圈按钮
- 我如何使用一个复合绘图而不是一个线性布局,包含一个ImageView和一个TextView
- 如何绘制一个线性布局的边界
- 如何动画添加或删除Android列表视图行
- JavaScript中两个日期之间的月份差异
- 如何解决“丢失的PendingIntent可变性标志”在android api 30+ lint警告?
- 我怎么能修复“意外元素<查询>发现<manifest>”错误?
- SQL:从时间戳日期减去1天
- Android房间-自动生成新插入行的id
- Android Studio在哪里保存ProGuard映射文件?
- 我如何从资产文件夹解析一个本地JSON文件到一个ListView?
- Android 4.4 (KitKat)上的Android Gallery为Intent返回不同的URI。ACTION_GET_CONTENT
- 移除动作栏下面的阴影