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

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

}



  • 24
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值