获取系统时间

谁践踏了优雅 2022-07-15 12:04 313阅读 0赞

文章转载:http://blog.csdn.net/feifei454498130/article/details/6540133

  1. //获取当前的年月时分
  2. import java.text.SimpleDateFormat;
  3. SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");
  4. Date curDate = new Date(System.currentTimeMillis());//获取当前时间
  5. String str = formatter.format(curDate);
  6. //获取当前的年月时分分开
  7. SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  8. String date = sDateFormat.format(new java.util.Date());
  9. //如果想获取当前的年月,则可以这样写(只获取时间或秒种一样):
  10. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
  11. String date=sdf.format(new java.util.Date());
  12. //当然还有就是可以指定时区的时间(待):
  13. df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.CHINA);
  14. System.out.println(df.format(new Date()));
  15. //获取Android系统时间是24小时制还是12小时制
  16. ContentResolver cv = this.getContentResolver();
  17. String strTimeFormat = android.provider.Settings.System.getString(cv,android.provider.Settings.System.TIME_12_24);
  18. if(strTimeFormat.equals("24"))
  19. {
  20. Log.i("activity","24");
  21. }
  22. //利用Calendar获取
  23. Calendar c = Calendar.getInstance();
  24. 取得系统日期:year = c.get(Calendar.YEAR)
  25. month = c.grt(Calendar.MONTH)
  26. day = c.get(Calendar.DAY_OF_MONTH)
  27. 取得系统时间:hour = c.get(Calendar.HOUR_OF_DAY);
  28. minute = c.get(Calendar.MINUTE)
  29. Calendar c = Calendar.getInstance();
  30. 取得系统日期:year = c.get(Calendar.YEAR)
  31. month = c.grt(Calendar.MONTH)
  32. day = c.get(Calendar.DAY_OF_MONTH)
  33. 取得系统时间:hour = c.get(Calendar.HOUR_OF_DAY);
  34. minute = c.get(Calendar.MINUTE)
  35. //利用Time获取
  36. Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
  37. t.setToNow(); // 取得系统时间。
  38. int year = t.year;
  39. int month = t.month;
  40. int date = t.monthDay;
  41. int hour = t.hour; // 0-23
  42. int minute = t.minute;
  43. int second = t.second;

发表评论

表情:
评论列表 (有 0 条评论,313人围观)

还没有评论,来说两句吧...

相关阅读