获取本地日期与时间
public String getCalendar() { @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Date dt = new Date(); return sdf.format(dt); } //SimpleDateFormat 时间格式 //y 年 MM 月 dd 日 //D 一年的第几天 W 一个月的第几星期 w 一年的第几星期 k时 z时区 //HH 小时(24小时制) hh小时(12小时制) mm 分 ss 秒 SS 毫秒 E 星期 //a 上午/下午
计算相隔天数
/** * 获得天数差 * @param begin * @param end * @return */ public long getDayDiff(Date begin, Date end){ long day = 1; if(end.getTime() < begin.getTime()){ day = -1; }else if(end.getTime() == begin.getTime()){ day = 1; }else { day += (end.getTime() - begin.getTime())/(24 * 60 * 60 * 1000) ; } return day-1; } Date date=new Date(2017-1900,11,1,0,30); Date date2=new Date(2018-1900,11,1,0,30); getDayDiff(date,date2)
获取网络时间与日期
private void getNetTime() { URL url = null; try { url = new URL("http://www.baidu.com"); URLConnection uc = url.openConnection(); uc.connect(); long ld = uc.getDate(); //取得网站日期时间 @SuppressLint("SimpleDateFormat") DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(ld); final String format = formatter.format(calendar.getTime()); boottom_tiem.setText(format); } catch (Exception e) { e.printStackTrace(); } }
判断网络是否可用
public class NetworkUtil { /** * 检查网络是否可用 * * @param context * @return */ public static boolean isNetworkAvailable(Context context) { ConnectivityManager manager = (ConnectivityManager) context .getApplicationContext().getSystemService( Context.CONNECTIVITY_SERVICE); if (manager == null) { return false; } NetworkInfo networkinfo = manager.getActiveNetworkInfo(); if (networkinfo == null || !networkinfo.isAvailable()) { return false; } return true; } }
内容来源于网络如有侵权请私信删除
- 还没有人评论,欢迎说说您的想法!