温馨提示×

Android时间控件如何格式化显示

小樊
84
2024-07-20 06:52:41
栏目: 编程语言

Android中时间控件可以通过SimpleDateFormat类来格式化显示时间。以下是一个示例代码:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedTime = sdf.format(new Date());

// 将格式化后的时间显示在TextView控件上
textView.setText(formattedTime);

其中,"yyyy-MM-dd HH:mm:ss"是时间的格式化字符串,可以根据需要自定义格式。常见的时间格式化字符串包括:

  • “yyyy-MM-dd HH:mm:ss”:年-月-日 时:分:秒
  • “yyyy-MM-dd HH:mm”:年-月-日 时:分
  • “yyyy-MM-dd”:年-月-日
  • “HH:mm:ss”:时:分:秒
  • “HH:mm”:时:分
  • 等等

通过SimpleDateFormat类和自定义的格式化字符串,可以将时间按照指定格式显示在Android应用中的时间控件上。

0