Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 197|回复: 0

[默认分类] Android--获取系统时间的几种方式

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-5-24 13:23:02 | 显示全部楼层 |阅读模式


    方式一:

    1. import   java.text.SimpleDateFormat;   
    2.   SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy年MM月dd日   HH:mm:ss");   
    3.   Date curDate =  new Date(System.currentTimeMillis());
    4. //获取当前时间  
    复制代码
    1. String   str   =   formatter.format(curDate);
    复制代码

    方式二:


    1. 取得系统时间
    2. 1。
    3. long time=System.currentTimeMillis();
    4. 2。
    5. final Calendar mCalendar=Calendar.getInstance();
    6. mCalendar.setTimeInMillis(time);
    7. 取得小时:mHour=mCalendar.get(Calendar.HOUR);
    8. 取得分钟:mMinuts=mCalendar.get(Calendar.MINUTE);
    9. 3。
    10. Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
    11. t.setToNow(); // 取得系统时间。
    12. int year = t.year;
    13. int month = t.month;
    14. int date = t.monthDay;
    15. int hour = t.hour;    // 0-23
    16. 4。
    17. DateFormat df = new SimpleDateFormat("HH:mm:ss");
    18. df.format(new Date());
    复制代码
    1. [/code]
    2. TimeUtils
    3. [code]
    复制代码
    1. import android.util.Log;      
    2. import java.text.ParseException;      
    3. import java.text.SimpleDateFormat;      
    4. import java.util.Calendar;      
    5. import java.util.Date;      
    6.       
    7.       
    8. public class TimeUtils {      
    9.       
    10.     /**   
    11.      * 获取当前时间   
    12.      * @return   
    13.      */      
    14.     public static String getNowTime(){      
    15.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");      
    16.         Date date = new Date(System.currentTimeMillis());      
    17.         return simpleDateFormat.format(date);      
    18.     }      
    19.     /**   
    20.      * 获取时间戳   
    21.      *   
    22.      * @return 获取时间戳   
    23.      */      
    24.     public static String getTimeString() {      
    25.         SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");      
    26.         Calendar calendar = Calendar.getInstance();      
    27.         return df.format(calendar.getTime());      
    28.     }      
    29.     /**   
    30.      * 时间转换为时间戳   
    31.      * @param time:需要转换的时间   
    32.      * @return   
    33.      */      
    34.     public static String dateToStamp(String time)  {      
    35.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");      
    36.         Date date = null;      
    37.         try {      
    38.             date = simpleDateFormat.parse(time);      
    39.         } catch (ParseException e) {      
    40.             e.printStackTrace();      
    41.         }      
    42.         long ts = date.getTime();      
    43.         return String.valueOf(ts);      
    44.     }      
    45.       
    46.     /**   
    47.      * 时间戳转换为字符串   
    48.      * @param time:时间戳   
    49.      * @return   
    50.      */      
    51.     public static String times(String time) {  
    52.         SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");  
    53.         @SuppressWarnings("unused")  
    54.         long lcc = Long.valueOf(time);  
    55.         int i = Integer.parseInt(time);  
    56.         String times = sdr.format(new Date(i * 1000L));  
    57.         return times;  
    58.   
    59.     }     
    60.     /**   
    61.      *获取距现在某一小时的时刻   
    62.      * @param hour hour=-1为上一个小时,hour=1为下一个小时   
    63.      * @return   
    64.      */      
    65.     public static String getLongTime(int hour){      
    66.         Calendar c = Calendar.getInstance(); // 当时的日期和时间      
    67.         int h; // 需要更改的小时      
    68.         h = c.get(Calendar.HOUR_OF_DAY) - hour;      
    69.         c.set(Calendar.HOUR_OF_DAY, h);      
    70.         SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");      
    71.         Log.v("time",df.format(c.getTime()));      
    72.         return df.format(c.getTime());      
    73.     }   
    74.     /**  
    75.      * 比较时间大小  
    76.      * @param str1:要比较的时间  
    77.      * @param str2:要比较的时间  
    78.      * @return  
    79.      */   
    80.     public static boolean isDateOneBigger(String str1, String str2) {   
    81.         boolean isBigger = false;   
    82.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");   
    83.         Date dt1 = null;   
    84.         Date dt2 = null;   
    85.         try {   
    86.             dt1 = sdf.parse(str1);   
    87.             dt2 = sdf.parse(str2);   
    88.         } catch (ParseException e) {   
    89.             e.printStackTrace();   
    90.         }   
    91.         if (dt1.getTime() > dt2.getTime()) {   
    92.             isBigger = true;   
    93.         } else if (dt1.getTime() < dt2.getTime()) {   
    94.             isBigger = false;   
    95.         }   
    96.         return isBigger;   
    97.     }   
    98. }      
    99.   /**
    100.   * 当地时间 ---> UTC时间
    101.   * @return
    102.   */  
    103. public static String Local2UTC(){  
    104.      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    105.      sdf.setTimeZone(TimeZone.getTimeZone("gmt"));  
    106.      String gmtTime = sdf.format(new Date());  
    107.      return gmtTime;  
    108. }  
    109.   
    110.   /**
    111.   * UTC时间 ---> 当地时间
    112.   * @param utcTime   UTC时间
    113.   * @return
    114.   */  
    115. public static String utc2Local(String utcTime) {  
    116.      SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//UTC时间格式  
    117.      utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));  
    118.      Date gpsUTCDate = null;  
    119.      try {  
    120.          gpsUTCDate = utcFormater.parse(utcTime);  
    121.      } catch (ParseException e) {  
    122.          e.printStackTrace();  
    123.      }  
    124.      SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//当地时间格式  
    125.      localFormater.setTimeZone(TimeZone.getDefault());  
    126.      String localTime = localFormater.format(gpsUTCDate.getTime());  
    127.      return localTime;  
    128. }  
    复制代码
    1. package com.example.time;
    2. import java.text.ParseException;
    3. import java.text.SimpleDateFormat;
    4. import java.util.Date;
    5. import java.util.TimeZone;
    6. import android.app.Activity;
    7. import android.os.Bundle;
    8. public class MainActivity extends Activity {
    9.         @Override
    10.         protected void onCreate(Bundle savedInstanceState) {
    11.                 super.onCreate(savedInstanceState);
    12.                 setContentView(R.layout.activity_main);
    13.                
    14.                 new Thread(){
    15.                         public void run() {
    16.                                 String time = Local2UTC("2018-03-19 19:41:44");
    17.                                 System.out.println("time"+time);
    18.                         };
    19.                 }.start();
    20.         }
    21.         /**
    22.          * 将本地时间转为UTC时间
    23.          * @param strDate:需要转化的date
    24.          * @return
    25.          */
    26.         public String Local2UTC(String strDate){  
    27.              SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    28.              String gmtTime = "";
    29.              sdf.setTimeZone(TimeZone.getTimeZone("gmt"));  
    30.              gmtTime = sdf.format(stringToDate(strDate, "yyyy-MM-dd HH:mm:ss"));
    31.              return gmtTime;  
    32.          }  
    33.         /**
    34.          * 将string类型转换为date类型
    35.          * @param strTime:要转换的string类型的时间,时间格式必须要与formatType的时间格式相同
    36.          * @param formatType:要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    37.          * @return
    38.          */
    39.     private Date stringToDate(String strTime, String formatType){
    40.         SimpleDateFormat formatter = new SimpleDateFormat(formatType);
    41.         Date date = null;
    42.         try {
    43.                         date = (Date) formatter.parse(strTime);
    44.                 } catch (ParseException e) {
    45.                         // TODO Auto-generated catch block
    46.                         e.printStackTrace();
    47.                 }
    48.         return date;
    49.     }
    50. }
    复制代码






    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-17 08:10 , Processed in 0.391669 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表