获取当前系统时间

java代码:
import java.text.SimpleDateFormat;

SimpleDateFormat formatter = new SimpleDateFormat (“yyyy年MM月dd日 HH:mm:ss “);
Date curDate = new Date(System.currentTimeMillis());//获取当前时间
String str = formatter.format(curDate);

以上可以获取当前的年月时分,也可以分开写(如下):

java代码:
SimpleDateFormat sDateFormat = new SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”);
String date = sDateFormat.format(new java.util.Date());

如果想获取当前的年月,则可以这样写(只获取时间或秒种一样):

java代码:
SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM”);
String date=sdf.format(new java.util.Date());

当然还有就是可以指定时区的时间(待):
df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.CHINA);
System.out.println(df.format(new Date()));

如何获取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”);
  }

取得系统日期:

java代码:
Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)

取得系统时间:

java代码:
Calendar c = Calendar.getInstance();

hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)

利用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;

以下是我最近在项目中获取系统时间的方法:
Date now = new Date();
Calendar cal = Calendar.getInstance();
SimpleDateFormat d1 = new SimpleDateFormat(“yyyy-MM-dd HH:mm”); //默认语言(汉语)下的默认风格(MEDIUM风格,比如:2008-6-16 20:54:53)
String sysTimeStr = d1.format(now);
String[] c = sysTimeStr.split(” “);
String[] c1 = c[0].split(“-“);
String[] c2 = c[1].split(“:”);
int year = Integer.parseInt(c1[0]);
int month = Integer.parseInt(c1[1]);
int day = Integer.parseInt(c1[2]);

       int  hour = Integer.parseInt(c2[0]);
       int  minute = Integer.parseInt(c2[1]);


            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month - 1);
            calendar.set(Calendar.DAY_OF_MONTH, day);
            int weekIndex = calendar.get(Calendar.DAY_OF_WEEK);
            String mWay = String.valueOf(weekIndex);
            if ("1".equals(mWay)) {
                mWay = "天";
            } else if ("2".equals(mWay)) {
                mWay = "一";
            } else if ("3".equals(mWay)) {
                mWay = "二";
            } else if ("4".equals(mWay)) {
                mWay = "三";
            } else if ("5".equals(mWay)) {
                mWay = "四";
            } else if ("6".equals(mWay)) {
                mWay = "五";
            } else if ("7".equals(mWay)) {
                mWay = "六";
            }

    if (hour<10&&minute<10){
        time.setText("0"+hour+ " : "+"0" + minute);
    }else if (hour<10&&minute>10){
        time.setText("0"+hour+ " : "+ minute);
    }else if (hour>10&&minute<10){
        time.setText(hour+ " : "+"0"+ minute);
    }else {
        time.setText(hour + " : " + minute);
    }
    time.setTextSize(Integer.parseInt(value));

        date.setText("星期" + mWay +"\n"+year + "." + month + "." + day);

    date.setTextSize((float) ((Integer.parseInt(value))*0.4));

下面这种方法也很棒:
使用的是System.currentTimeMillis->Date+SimpleDateFormat,我觉得这种方式来展示时间非常的灵活。
执行结果:
获取当前系统时间_第1张图片

源代码:
long time=System.currentTimeMillis();
Date date=new Date(time);
SimpleDateFormat format=new SimpleDateFormat(“yyyy年MM月dd日 HH时mm分ss秒 EEEE”);
Log.e(“time”,”time1=”+format.format(date));
format=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);
Log.e(“time”,”time2=”+format.format(date));
format=new SimpleDateFormat(“yyyy/MM/dd”);
Log.e(“time”,”time3=”+format.format(date));
format=new SimpleDateFormat(“HH:mm:ss”);
Log.e(“time”,”time4=”+format.format(date));
format=new SimpleDateFormat(“EEEE”);
Log.e(“time”,”time5=”+format.format(date));
format=new SimpleDateFormat(“E”);
Log.e(“time”,”time6=”+format.format(date));

你可能感兴趣的:(Android基础)