博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android多种方法显示当前日期和时间
阅读量:7081 次
发布时间:2019-06-28

本文共 4529 字,大约阅读时间需要 15 分钟。

文章选自系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术。本文探讨Android显示当前日期和时间的方法。

问题:

如何在Android应用中显示当前日期和时间?

答案:

这个有多种解决方法。我假设你想把当前日期和时间放在TextView上。

1
2
3
4
String currentDateTimeString = DateFormat.getDateTimeInstance().format(
new
Date());
 
// textView is the TextView view that should display it
textView.setText(currentDateTimeString);

文档里可以阅读更多,点击  。你可以在那里读到更多信息,来更改用于转换的格式。

user647826

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class XYZ extends Activity {
 
    
/** Called when the activity is first created. */
    
@Override
    
public void onCreate(Bundle savedInstanceState) {
        
super
.onCreate(savedInstanceState);
        
//setContentView(R.layout.main);
 
        
Calendar c = Calendar.getInstance();
        
System.out.println(
"Current time => "
+c.getTime());
 
        
SimpleDateFormat df =
new
SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss"
);
        
String formattedDate = df.format(c.getTime());
        
// formattedDate have current date/time
        
Toast.makeText(
this
, formattedDate, Toast.LENGTH_SHORT).show();
 
 
      
// Now we display formattedDate value in TextView
        
TextView txtView =
new
TextView(
this
);
        
txtView.setText(
"Current Date and Time : "
+formattedDate);
        
txtView.setGravity(Gravity.CENTER);
        
txtView.setTextSize(20);
        
setContentView(txtView);
    
}
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public void onCreate(Bundle savedInstanceState) {
    
super
.onCreate(savedInstanceState);
 
    
setContentView(R.layout.main);
    
Thread myThread =
null
;
 
    
Runnable runnable =
new
CountDownRunner();
    
myThread=
new
Thread(runnable);  
    
myThread.start();
 
}
 
public void doWork() {
    
runOnUiThread(
new
Runnable() {
        
public void run() {
            
try
{
                
TextView txtCurrentTime= (TextView)findViewById(R.id.lbltime);
                    
Date dt =
new
Date();
                    
int hours = dt.getHours();
                    
int minutes = dt.getMinutes();
                    
int seconds = dt.getSeconds();
                    
String curTime = hours +
":"
+ minutes +
":"
+ seconds;
                    
txtCurrentTime.setText(curTime);
            
}
catch
(Exception e) {}
        
}
    
});
}
 
 
class CountDownRunner implements Runnable{
    
// @Override
    
public void run() {
            
while
(!Thread.currentThread().isInterrupted()){
                
try
{
                
doWork();
                    
Thread.sleep(1000);
                
}
catch
(InterruptedException e) {
                        
Thread.currentThread().interrupt();
                
}
catch
(Exception e){
                
}
            
}
    
}
}

要展示当前时间, 和是明智的选择。

例如下面的布局:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout
    
xmlns:android=
""
    
android:layout_width=
"fill_parent"
    
android:layout_height=
"fill_parent"
    
android:orientation=
"vertical"
>
 
    
<AnalogClock
        
android:layout_width=
"fill_parent"
        
android:layout_height=
"wrap_content"
/>
 
    
<DigitalClock
        
android:layout_width=
"fill_parent"
        
android:layout_height=
"wrap_content"
        
android:gravity=
"center"
        
android:textSize=
"20sp"
/>
</LinearLayout>

看起来是这个样子:

我自己的展示方法:

1
2
3
4
5
6
7
Calendar c = Calendar.getInstance();
 
String sDate = c.get(Calendar.YEAR) +
"-"
+ c.get(Calendar.MONTH)
+
"-"
+ c.get(Calendar.DAY_OF_MONTH)
+
" at "
+ c.get(Calendar.HOUR_OF_DAY)
+
":"
+ c.get(Calendar.MINUTE);

如果你想用特定的模式展示日期和时间,可以用这个:

1
2
Date d =
new
Date();
CharSequence s = DateFormat.format(
"yyyy-MM-dd hh:mm:ss"
, d.getTime());

1
2
3
4
Calendar c = Calendar.getInstance();
int month=c.get(Calendar.MONTH)+1;
String sDate = c.get(Calendar.YEAR) +
"-"
+ month+
"-"
+ c.get(Calendar.DAY_OF_MONTH) +
"T"
+ c.get(Calendar.HOUR_OF_DAY)+
":"
+c.get(Calendar.MINUTE)+
":"
+c.get(Calendar.SECOND);

这个展示日期的格式是 2010-05-24T18:13:00

这能显示当前时间和日期:

1
2
3
4
5
6
7
public String getCurrDate()
{
    
String dt;
    
Date cal = Calendar.getInstance().getTime();
    
dt = cal.toLocaleString();
    
return
dt;
}

如下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
SimpleDateFormat dateFormat =
new
SimpleDateFormat(
                
"yyyy/MM/dd HH:mm:ss"
);
        
Calendar cal = Calendar.getInstance();
        
System.out.println(
"time => "
+ dateFormat.format(cal.getTime()));
 
        
String time_str = dateFormat.format(cal.getTime());
 
        
String[] s = time_str.split(
" "
);
 
        
for
(int i = 0; i < s.length; i++) {
             
System.out.println(
"date  => "
+ s[i]);
        
}
 
        
int year_sys = Integer.parseInt(s[0].split(
"/"
)[0]);
        
int month_sys = Integer.parseInt(s[0].split(
"/"
)[1]);
        
int day_sys = Integer.parseInt(s[0].split(
"/"
)[2]);
 
        
int hour_sys = Integer.parseInt(s[1].split(
":"
)[0]);
        
int min_sys = Integer.parseInt(s[1].split(
":"
)[1]);
 
        
System.out.println(
"year_sys  => "
+ year_sys);
        
System.out.println(
"month_sys  => "
+ month_sys);
        
System.out.println(
"day_sys  => "
+ day_sys);
 
        
System.out.println(
"hour_sys  => "
+ hour_sys);
        
System.out.println(
"min_sys  => "
+ min_sys);

原文链接:

转载于:https://www.cnblogs.com/aikongmeng/p/3697341.html

你可能感兴趣的文章
lucene 的评分机制
查看>>
Backup Volume 操作 - 每天5分钟玩转 OpenStack(59)
查看>>
JavaWeb之tomcat安装、配置与使用(一)
查看>>
SpringMVC Controller 返回值的可选类型
查看>>
kbmmw 5.03 发布
查看>>
iOS - App 与外设间的通信方式
查看>>
13.7. Device Management
查看>>
Hibernate详细教程
查看>>
144.2. tcpdump - A powerful tool for network monitoring and data acquisition
查看>>
查看ecshop广告位对应的广告详细信息
查看>>
Selenium2+python自动化51-unittest简介
查看>>
1.6. complete
查看>>
Solr5.3.1整合IKAnalyzer
查看>>
iOS - Socket 网络套接字
查看>>
Redis代码阅读1--Redis启动原理
查看>>
今天理了一个平头
查看>>
★路由递归查询方法及相关图示【转载】
查看>>
SAP 开源 SCA 工具,扫描软件包依赖漏洞
查看>>
Oracle 中 Object_iD 和 Data_Object_ID 的区别
查看>>
10年前的《武林外传》
查看>>