android 中 系統日期時間的獲取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);
能夠獲取當前的年月時分,也能夠分開寫:android
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String date = sDateFormat.format(new java.util.Date());
若是想獲取當前的年月,則能夠這樣寫(只獲取時間或秒種同樣):ide
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM"); String date=sdf.format(new java.util.Date());
固然還有就是能夠指定時區的時間(待):this
df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.CHINA); System.out.println(df.format(new Date()));
如何獲取Android系統時間是24小時制仍是12小時制code
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"); } Calendar c = Calendar.getInstance(); //取得系統日期: year = c.get(Calendar.YEAR) month = c.grt(Calendar.MONTH) day = c.get(Calendar.DAY_OF_MONTH) //取得系統時間: hour = c.get(Calendar.HOUR_OF_DAY); minute = c.get(Calendar.MINUTE)
利用Calendar獲取orm
Calendar c = Calendar.getInstance(); 取得系統日期:year = c.get(Calendar.YEAR) month = c.grt(Calendar.MONTH) day = c.get(Calendar.DAY_OF_MONTH) 取得系統時間:hour = c.get(Calendar.HOUR_OF_DAY); minute = c.get(Calendar.MINUTE) Calendar c = Calendar.getInstance(); 取得系統日期:year = c.get(Calendar.YEAR) month = c.grt(Calendar.MONTH) day = c.get(Calendar.DAY_OF_MONTH) 取得系統時間:hour = c.get(Calendar.HOUR_OF_DAY); minute = c.get(Calendar.MINUTE)
利用Time獲取字符串
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;
計算時間差:get
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try{ Date d1 = df.parse("2004-03-26 13:31:40"); Date d2 = df.parse("2004-01-02 11:30:24"); long diff = d1.getTime() - d2.getTime(); long days = diff / (1000 * 60 * 60 * 24); }catch (Exception e){ } 方法二: SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date now = df.parse("2004-03-26 13:31:40"); java.util.Date date=df.parse("2004-01-02 11:30:24"); long l=now.getTime()-date.getTime(); long day=l/(24*60*60*1000); long hour=(l/(60*60*1000)-day*24); long min=((l/(60*1000))-day*24*60-hour*60); long s=(l/1000-day*24*60*60-hour*60*60-min*60); System.out.println(""+day+"天"+hour+"小時"+min+"分"+s+"秒"); 方法三: SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date begin=dfs.parse("2004-01-02 11:30:24"); java.util.Date end = dfs.parse("2004-03-26 13:31:40"); long between=(end.getTime()-begin.getTime())/1000;//除以1000是爲了轉換成秒 long day1=between/(24*3600); long hour1=between%(24*3600)/3600; long minute1=between%3600/60; long second1=between%60/60; System.out.println(""+day1+"天"+hour1+"小時"+minute1+"分"+second1+"秒"); ==================================================== java 比較時間大小 String s1="2008-01-25 09:12:09"; String s2="2008-01-29 09:12:11"; java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Calendar c1=java.util.Calendar.getInstance(); java.util.Calendar c2=java.util.Calendar.getInstance(); try{ c1.setTime(df.parse(s1)); c2.setTime(df.parse(s2)); }catch(java.text.ParseException e){ System.err.println("格式不正確"); } int result=c1.compareTo(c2); if(result==0) System.out.println("c1相等c2"); else if(result<0) System.out.println("c1小於c2"); else System.out.println("c1大於c2"); //時間格式轉換 FastDateFormat df = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss"); String date = df.format(java.util.Date()
時間的各類轉換:it
/** * 把日期轉爲字符串 * / public static String ConverToString(Date date) { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); return df.format(date); } /** * 把字符串轉爲日期 */ public static Date ConverToDate(String strDate) throws Exception{ DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); return df.parse(strDate); } /** * 獲取當前時間戳 */ public long getSysMillis(){ return System.currentTimeMillis(); } /** * 獲取明天的這個時間 */ public Date ConverToDate(){ return new Date(System.currentTimeMillis() + 86400000); } /** * 時間戳轉時間格式字符串 */ public String formatString(long time){ Date d = new Date(time * 1000L); SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); return sf.format(d); }
int轉時間格式io
public static String changeToTimeFormat(int length) { String time = ""; SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault()); sdf.setTimeZone(TimeZone.getTimeZone("GMT+0")); Date date = new Date(length * 1000); time = sdf.format(date); return time; }
Date d = new Date(); SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//12小時制 System.out.println(ss.format(d)); Date date = new Date(); SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小時制 String LgTime = sdformat.format(date); System.out.println(LgTime); 結果爲 2008-05-28 01:32:54 2008-05-28 13:32:54