Android--获取系统时间的几种方式

Android–获取系统时间的几种方式

方式一:

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);

方式二:

取得系统时间
1。
long time=System.currentTimeMillis();

2。
final Calendar mCalendar=Calendar.getInstance();
mCalendar.setTimeInMillis(time);
取得小时:mHour=mCalendar.get(Calendar.HOUR);
取得分钟:mMinuts=mCalendar.get(Calendar.MINUTE);


3。
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

4。
DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());

TimeUtils
[java] view plain copy

import android.util.Log;      
import java.text.ParseException;      
import java.text.SimpleDateFormat;      
import java.util.Calendar;      
import java.util.Date;      


public class TimeUtils {      

    /**   
     * 获取当前时间   
     * @return   
     */      
    public static String getNowTime(){      
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");      
        Date date = new Date(System.currentTimeMillis());      
        return simpleDateFormat.format(date);      
    }      
    /**   
     * 获取时间戳   
     *   
     * @return 获取时间戳   
     */      
    public static String getTimeString() {      
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");      
        Calendar calendar = Calendar.getInstance();      
        return df.format(calendar.getTime());      
    }      
    /**   
     * 时间转换为时间戳   
     * @param time:需要转换的时间   
     * @return   
     */      
    public static String dateToStamp(String time)  {      
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");      
        Date date = null;      
        try {      
            date = simpleDateFormat.parse(time);      
        } catch (ParseException e) {      
            e.printStackTrace();      
        }      
        long ts = date.getTime();      
        return String.valueOf(ts);      
    }      

    /**   
     * 时间戳转换为字符串   
     * @param time:时间戳   
     * @return   
     */      
    public static String times(String time) {  
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");  
        @SuppressWarnings("unused")  
        long lcc = Long.valueOf(time);  
        int i = Integer.parseInt(time);  
        String times = sdr.format(new Date(i * 1000L));  
        return times;  

    }     
    /**   
     *获取距现在某一小时的时刻   
     * @param hour hour=-1为上一个小时,hour=1为下一个小时   
     * @return   
     */      
    public static String getLongTime(int hour){      
        Calendar c = Calendar.getInstance(); // 当时的日期和时间      
        int h; // 需要更改的小时      
        h = c.get(Calendar.HOUR_OF_DAY) - hour;      
        c.set(Calendar.HOUR_OF_DAY, h);      
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");      
        Log.v("time",df.format(c.getTime()));      
        return df.format(c.getTime());      
    }    
    /**  
     * 比较时间大小  
     * @param str1:要比较的时间  
     * @param str2:要比较的时间  
     * @return  
     */    
    public static boolean isDateOneBigger(String str1, String str2) {    
        boolean isBigger = false;    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");    
        Date dt1 = null;    
        Date dt2 = null;    
        try {    
            dt1 = sdf.parse(str1);    
            dt2 = sdf.parse(str2);    
        } catch (ParseException e) {    
            e.printStackTrace();    
        }    
        if (dt1.getTime() > dt2.getTime()) {    
            isBigger = true;    
        } else if (dt1.getTime() < dt2.getTime()) {    
            isBigger = false;    
        }    
        return isBigger;    
    }    
}      
  /** 
  * 当地时间 ---> UTC时间 
  * @return 
  */  
 public static String Local2UTC(){  
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
     sdf.setTimeZone(TimeZone.getTimeZone("gmt"));  
     String gmtTime = sdf.format(new Date());  
     return gmtTime;  
 }  

  /** 
  * UTC时间 ---> 当地时间 
  * @param utcTime   UTC时间 
  * @return 
  */  
 public static String utc2Local(String utcTime) {  
     SimpleDateFormat utcFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//UTC时间格式  
     utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));  
     Date gpsUTCDate = null;  
     try {  
         gpsUTCDate = utcFormater.parse(utcTime);  
     } catch (ParseException e) {  
         e.printStackTrace();  
     }  
     SimpleDateFormat localFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//当地时间格式  
     localFormater.setTimeZone(TimeZone.getDefault());  
     String localTime = localFormater.format(gpsUTCDate.getTime());  
     return localTime;  
 }  

package com.example.time;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Thread(){
            public void run() {
                String time = Local2UTC("2018-03-19 19:41:44");
                System.out.println("time"+time);
            };
        }.start();
    }
    /**
     * 将本地时间转为UTC时间
     * @param strDate:需要转化的date
     * @return
     */
    public String Local2UTC(String strDate){  
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
         String gmtTime = "";
         sdf.setTimeZone(TimeZone.getTimeZone("gmt"));  
         gmtTime = sdf.format(stringToDate(strDate, "yyyy-MM-dd HH:mm:ss"));
         return gmtTime;  
     }  
    /**
     * 将string类型转换为date类型
     * @param strTime:要转换的string类型的时间,时间格式必须要与formatType的时间格式相同
     * @param formatType:要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
     * @return
     */
    private Date stringToDate(String strTime, String formatType){
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        try {
            date = (Date) formatter.parse(strTime);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

}

转载自 https://blog.csdn.net/chaoyu168/article/details/50729689

谢谢楼主分享!使用时发现方式二的第二条有两个地方需要补充:1、mCalendar.setTimeInMillis(time); 这个使用时造成了小时和分钟都显示为0,查了一下这个方法是从给定的long值设置日历的当前时间,我直接将这行代码去掉后,就能正常显示了。2、mHour=mCalendar.get(Calendar.HOUR);显示的是12小时制,如果想显示24小时制用mHour=mCalendar.get(Calendar.HOUR_OF_DAY);

猜你喜欢

转载自blog.csdn.net/jscsd226/article/details/81176837