android 获取时间

首先,先说下java下可以正常使用的方法:

 1 import java.text.DateFormat;
 2 import java.text.SimpleDateFormat;
 3 import java.util.Calendar;
 4 import java.util.Date;
 5 import java.util.Locale;
 6 
 7 public class GetDate {
 8 
 9     /**
10      * @param args
11      */
12     public static void main(String[] args) {
13 
14         //获取 年月日 时分秒(12小时制)
15         SimpleDateFormat sDateFormat = new SimpleDateFormat(
16                 "yyyy-MM-dd    hh:mm:ss");//显示规则
17         String date = sDateFormat.format(new java.util.Date());
18         
19         System.out.println(date);
20         
21         //获取 年月日 时分秒(12小时制)
22         SimpleDateFormat sDateFormat1 = new SimpleDateFormat(
23                 "yyyy年MM月dd日    hh:mm:ss");//显示规则
24         String date1 = sDateFormat.format(new java.util.Date());
25                 
26         System.out.println(date1);
27         
28         //设置时区 使用 date()方法显示时间
29         DateFormat df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.CHINA); //中国 
30         System.out.println(df.format(new Date()));
31             
32         //24小時制
33         Calendar c = Calendar.getInstance();  
34         // 取得系统日期:
35         int year = c.get(Calendar.YEAR) ; 
36         int month = c.get(Calendar.MONTH)+1;  //获得的月份比现实月份小1
37         int day = c.get(Calendar.DAY_OF_MONTH)  ;
38         //取得系统时间:
39         int hour = c.get(Calendar.HOUR_OF_DAY);  
40         int minute = c.get(Calendar.MINUTE)  ;
41         
42         System.out.println(year+":"+month+":"+day+":"+hour+":"+minute+"");
43         
44         //Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
45     }
46 
47 }

可以直接考到电脑上跑跑看区别。

这里贴下结果:

2016-03-08    12:35:34
2016-03-08    12:35:34
2016年3月8日 星期二 上午12时35分34秒 CST
2016:3:8:0:35

下面放下android可用的方法:

如何获取Android系统时间是24小时制还是12小时制:

java代码:

    ContentResolver cv = this.getContentResolver();  
      String strTimeFormat = android.provider.Settings.System.getString(cv,
      android.provider.Settings.System.TIME_12_24);
      if(strTimeFormat.equals("24"))
      {
      Log.i("activity","24");
      }
利用Time获取:

java代码:

    Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
      t.setToNow(); // 取得系统时间。
      int year = t.year;
      int month = t.month;
      int date = t.monthDay;
      int hour = t.hour; // 0-23
      int minute = t.minute;
      int second = t.second;

上面的两个没测试过,不过time获取目前已经不推荐使用了

原文地址:https://www.cnblogs.com/wobeinianqing/p/5252544.html