主要積累一些開發(fā)中比較 常用的工具類招刹,部分借鑒于網(wǎng)絡(luò)顷啼,主要來源于平時開發(fā)因需求而生的小工具類
13鱼炒、ArithUtil
/**
* Created by Administrator on 2016/10/31.
*/
import java.math.BigDecimal;
/**
* 由于Java的簡單類型不能夠精確的對浮點數(shù)進(jìn)行運算,這個工具類提供精
* 確的浮點數(shù)運算读虏,包括加減乘除和四舍五入娇未。
*/
public class ArithUtil {
? ?//默認(rèn)除法運算精度
? ?private static final int DEF_DIV_SCALE = 10;
? ?//這個類不能實例化
? ?private ArithUtil(){
? ?}
? ?/**
? ? * 提供精確的加法運算墨缘。
? ? * @param v1 被加數(shù)
? ? * @param v2 加數(shù)
? ? * @return 兩個參數(shù)的和
? ? */
? ?public static double add(double v1,double v2){
? ? ? ?BigDecimal b1 = new BigDecimal(Double.toString(v1));
? ? ? ?BigDecimal b2 = new BigDecimal(Double.toString(v2));
? ? ? ?return b1.add(b2).doubleValue();
? ?}
? ?/**
? ? * 提供精確的減法運算。
? ? * @param v1 被減數(shù)
? ? * @param v2 減數(shù)
? ? * @return 兩個參數(shù)的差
? ? */
? ?public static double sub(double v1,double v2){
? ? ? ?BigDecimal b1 = new BigDecimal(Double.toString(v1));
? ? ? ?BigDecimal b2 = new BigDecimal(Double.toString(v2));
? ? ? ?return b1.subtract(b2).doubleValue();
? ?}
? ?/**
? ? * 提供精確的乘法運算零抬。
? ? * @param v1 被乘數(shù)
? ? * @param v2 乘數(shù)
? ? * @return 兩個參數(shù)的積
? ? */
? ?public static double mul(double v1,double v2){
? ? ? ?BigDecimal b1 = new BigDecimal(Double.toString(v1));
? ? ? ?BigDecimal b2 = new BigDecimal(Double.toString(v2));
? ? ? ?return b1.multiply(b2).doubleValue();
? ?}
? ?/**
? ? * 提供(相對)精確的除法運算镊讼,當(dāng)發(fā)生除不盡的情況時,精確到
? ? * 小數(shù)點以后10位平夜,以后的數(shù)字四舍五入蝶棋。
? ? * @param v1 被除數(shù)
? ? * @param v2 除數(shù)
? ? * @return 兩個參數(shù)的商
? ? */
? ?public static double div(double v1,double v2){
? ? ? ?return div(v1,v2,DEF_DIV_SCALE);
? ?}
? ?/**
? ? * 提供(相對)精確的除法運算。當(dāng)發(fā)生除不盡的情況時忽妒,由scale參數(shù)指
? ? * 定精度玩裙,以后的數(shù)字四舍五入。
? ? * @param v1 被除數(shù)
? ? * @param v2 除數(shù)
? ? * @param scale 表示表示需要精確到小數(shù)點以后幾位段直。
? ? * @return 兩個參數(shù)的商
? ? */
? ?public static double div(double v1,double v2,int scale){
? ? ? ?if(scale<0){
? ? ? ? ? ?throw new IllegalArgumentException(
? ? ? ? ? ? ? ? ? ?"The scale must be a positive integer or zero");
? ? ? ?}
? ? ? ?BigDecimal b1 = new BigDecimal(Double.toString(v1));
? ? ? ?BigDecimal b2 = new BigDecimal(Double.toString(v2));
? ? ? ?return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
? ?}
? ?/**
? ? * 提供精確的小數(shù)位四舍五入處理吃溅。
? ? * @param v 需要四舍五入的數(shù)字
? ? * @param scale 小數(shù)點后保留幾位
? ? * @return 四舍五入后的結(jié)果
? ? */
? ?public static double round(double v,int scale){
? ? ? ?if(scale<0){
? ? ? ? ? ?throw new IllegalArgumentException(
? ? ? ? ? ? ? ? ? ?"The scale must be a positive integer or zero");
? ? ? ?}
? ? ? ?BigDecimal b = new BigDecimal(Double.toString(v));
? ? ? ?BigDecimal one = new BigDecimal("1");
? ? ? ?return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
? ?}
}
12、NetWorkUtil
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
/**
* Created by Administrator on 2016/11/3.
* 獲取手機(jī)網(wǎng)絡(luò)狀態(tài)坷牛,詳細(xì)到移動網(wǎng)絡(luò)類型
*/
public class NetWorkUtil {
? ?/** 沒有網(wǎng)絡(luò) */
? ?public static final int NETWORKTYPE_INVALID = 0;
? ?/** wap網(wǎng)絡(luò) */
? ?public static final int NETWORKTYPE_WAP = 1;
? ?/** 2G網(wǎng)絡(luò) */
? ?public static final int NETWORKTYPE_2G = 2;
? ?/** 3G和3G以上網(wǎng)絡(luò)罕偎,或統(tǒng)稱為快速網(wǎng)絡(luò) */
? ?public static final int NETWORKTYPE_3G = 3;
? ?/** wifi網(wǎng)絡(luò) */
? ?public static final int NETWORKTYPE_WIFI = 4;
? ?private static int mNetWorkType;
? ?/**
? ? * 獲取網(wǎng)絡(luò)狀態(tài)很澄,wifi,wap,2g,3g.
? ? *
? ? * @param context 上下文
? ? * @return int 網(wǎng)絡(luò)狀態(tài) {@link #NETWORKTYPE_2G},{@link #NETWORKTYPE_3G}, ? ? ? ? ?*{@link #NETWORKTYPE_INVALID},{@link #NETWORKTYPE_WAP}*
{@link #NETWORKTYPE_WIFI}
? ? */
? ?public static int getNetWorkType(Context context) {
? ? ? ?ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
? ? ? ?NetworkInfo networkInfo = manager.getActiveNetworkInfo();
? ? ? ?if (networkInfo != null && networkInfo.isConnected()) {
? ? ? ? ? ?String type = networkInfo.getTypeName();
? ? ? ? ? ?if (type.equalsIgnoreCase("WIFI")) {
? ? ? ? ? ? ? ?mNetWorkType = NETWORKTYPE_WIFI;
? ? ? ? ? ?} else if (type.equalsIgnoreCase("MOBILE")) {
? ? ? ? ? ? ? ?String proxyHost = android.net.Proxy.getDefaultHost();
? ? ? ? ? ? ? ?mNetWorkType = TextUtils.isEmpty(proxyHost)
? ? ? ? ? ? ? ? ? ? ? ?? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G)
? ? ? ? ? ? ? ? ? ? ? ?: NETWORKTYPE_WAP;
? ? ? ? ? ?}
? ? ? ?} else {
? ? ? ? ? ?mNetWorkType = NETWORKTYPE_INVALID;
? ? ? ?}
? ? ? ?LogUtil.e("info","mNetWorkType:" + mNetWorkType);
? ? ? ?return mNetWorkType;
? ?}
? ?/**
? ? * 通過TelephonyManager判斷移動網(wǎng)絡(luò)的類型
? ? * @param context
? ? * @return
? ? */
? ?private static boolean isFastMobileNetwork(Context context) {
? ? ? ?TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
? ? ? ?switch (telephonyManager.getNetworkType()) {
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_1xRTT:
? ? ? ? ? ? ? ?return false; // ~ 50-100 kbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_CDMA:
? ? ? ? ? ? ? ?return false; // ~ 14-64 kbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_EDGE:
? ? ? ? ? ? ? ?return false; // ~ 50-100 kbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_EVDO_0:
? ? ? ? ? ? ? ?return true; // ~ 400-1000 kbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_EVDO_A:
? ? ? ? ? ? ? ?return true; // ~ 600-1400 kbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_GPRS:
? ? ? ? ? ? ? ?return false; // ~ 100 kbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_HSDPA:
? ? ? ? ? ? ? ?return true; // ~ 2-14 Mbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_HSPA:
? ? ? ? ? ? ? ?return true; // ~ 700-1700 kbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_HSUPA:
? ? ? ? ? ? ? ?return true; // ~ 1-23 Mbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_UMTS:
? ? ? ? ? ? ? ?return true; // ~ 400-7000 kbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_EHRPD:
? ? ? ? ? ? ? ?return true; // ~ 1-2 Mbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_EVDO_B:
? ? ? ? ? ? ? ?return true; // ~ 5 Mbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_HSPAP:
? ? ? ? ? ? ? ?return true; // ~ 10-20 Mbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_IDEN:
? ? ? ? ? ? ? ?return false; // ~25 kbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_LTE:
? ? ? ? ? ? ? ?return true; // ~ 10+ Mbps
? ? ? ? ? ?case TelephonyManager.NETWORK_TYPE_UNKNOWN:
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?default:
? ? ? ? ? ? ? ?return false;
? ? ? ?}
? ?}
}
11京闰、AppVersionUtil
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
/**
* Created by david on 2016/10/20.
*/
public class AppVersionUtil {
? ?/**
? ? * 獲取版本號
? ? * String
? ? * @return 當(dāng)前應(yīng)用的版本名稱
? ? */
? ?public static String getVersionName(Context context) {
? ? ? ?try {
? ? ? ? ? ?PackageManager manager = context.getPackageManager();
? ? ? ? ? ?PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
? ? ? ? ? ?String versionName = info.versionName;
? ? ? ? ? ?LogUtil.e("info", "versionName" + versionName);
? ? ? ? ? ?return versionName;
? ? ? ?} catch (Exception e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ?return "";
? ? ? ?}
? ?}
? ?/**
? ? * 獲取版本號
? ? * int
? ? * @return 當(dāng)前應(yīng)用的版本號
? ? */
? ?public static int getVersionCode(Context context) {
? ? ? ?try {
? ? ? ? ? ?PackageManager manager = context.getPackageManager();
? ? ? ? ? ?PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
? ? ? ? ? ?int versionCode = info.versionCode;
? ? ? ? ? ?LogUtil.e("info", "versionCode" + versionCode);
? ? ? ? ? ?return versionCode;
? ? ? ?} catch (Exception e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ?return 1;
? ? ? ?}
? ?}
}
10、JsonAssetsReaderUtil
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Created by david on 2016/11/23.
* 從Assets文件夾中讀取.json文件 輸出String
*/
public class JsonAssetsReaderUtil {
? ?public static String getJsonStrFromAssets(Context context, String jsonFileName) {
? ? ? ?InputStreamReader inputStreamReader = null;
? ? ? ?StringBuilder stringBuilder = null;
? ? ? ?BufferedReader bufferedReader = null;
? ? ? ?try {
? ? ? ? ? ?inputStreamReader = new InputStreamReader(context.getAssets().open(jsonFileName), "UTF-8");
? ? ? ? ? ?bufferedReader = new BufferedReader(inputStreamReader);
? ? ? ? ? ?String jsonStr;
? ? ? ? ? ?stringBuilder = new StringBuilder();
? ? ? ? ? ?while ((jsonStr = bufferedReader.readLine()) != null) {
? ? ? ? ? ? ? ?stringBuilder.append(jsonStr);
? ? ? ? ? ?}
? ? ? ?} catch (IOException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?} finally {
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?inputStreamReader.close();
? ? ? ? ? ? ? ?bufferedReader.close();
? ? ? ? ? ?} catch (IOException e) {
? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?return stringBuilder.toString();
? ?}
}
9甩苛、LogUtil
import android.util.Log;
/**
* LEVEL = VERBOSE時打印所有調(diào)試信息
* 當(dāng)項目上線時蹂楣,改為 LEVEL = NOTHING
* 關(guān)閉所有打印信息
* @author 81091
*
*/
public class LogUtil {
? ?public static final int VERBOSE = 1;
? ?public static final int DEBUG = 2;
? ?public static final int INFO = 3;
? ?public static final int WARN = 4;
? ?public static final int ERROR = 5;
? ?public static final int NOTHING = 6;
? ?public static final int LEVEL = VERBOSE;//打印所有等級的信息
// ? ?public static final int LEVEL = NOTHING;//關(guān)閉所有等級的信息
? ?public static void v(String tag,String msg){
? ? ? ?if(LEVEL <= VERBOSE){
? ? ? ? ? ?Log.v(tag,msg);
? ? ? ?}
? ?}
public static void d(String tag,String msg){
if(LEVEL <= DEBUG){
Log.d(tag,msg);
}
}
public static void i(String tag,String msg){
if(LEVEL <= INFO){
Log.i(tag,msg);
}
}
public static void w(String tag,String msg){
if(LEVEL <= WARN){
Log.w(tag,msg);
}
}
public static void e(String tag,String msg){
if(LEVEL <= ERROR){
Log.e(tag,msg);
}
}
}
8、MD5Utils
/**
* 32位小寫
* Created by C5-0 on 2015/7/25.
*/
public class MD5Utils {
? ?public static String ecodeTwice(String str) {//MD5兩次
? ? ? ?return ecode(ecode(str));
? ?}
? ?public static String ecode(String passwd) {
? ? ? ?try {
? ? ? ? ? ?MessageDigest instance = MessageDigest.getInstance("MD5");
? ? ? ? ? ?byte[] digest = instance.digest(passwd.getBytes());
? ? ? ? ? ?StringBuilder sb = new StringBuilder();
? ? ? ? ? ?for (byte b : digest) {
? ? ? ? ? ? ? ?int i = b & 0xff;
? ? ? ? ? ? ? ?String hexString = Integer.toHexString(i);
? ? ? ? ? ? ? ?if (hexString.length() < 2) {
? ? ? ? ? ? ? ? ? ?hexString = "0" + hexString;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?sb.append(hexString);
? ? ? ? ? ?}
? ? ? ? ? ?return sb.toString();
? ? ? ?} catch (NoSuchAlgorithmException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ? ? ?return "";
? ?}
}
7讯蒲、比較常用的正則表達(dá)式驗證
/**
* Created by Administrator on 2016/8/10.
*/
public class RegularUtils {
? ?private RegularUtils() {
? ? ? ?throw new UnsupportedOperationException("u can't fuck me...");
? ?}
? ?/**
? ? * 驗證手機(jī)號(簡單)
? ? */
? ?private static final String REGEX_MOBILE_SIMPLE = "^[1]\\d{10}$";
? ?/**
? ? * 驗證手機(jī)號(精確)
? ? *
? ? *
移動:134(0-8)痊土、135、136墨林、137赁酝、138犯祠、139、147酌呆、150衡载、151、152隙袁、157痰娱、158、159菩收、178梨睁、182、183娜饵、184坡贺、187、188
? ? *
聯(lián)通:130箱舞、131拴念、132、145褐缠、155政鼠、156、175队魏、176公般、185、186
? ? *
電信:133胡桨、153官帘、173、177昧谊、180刽虹、181、189
? ? *
全球星:1349
? ? *
虛擬運營商:170
*/
private static final String REGEX_MOBILE_EXACT = "^((13[0-9])|(14[5,7])|(15[0-3,5-8])|(17[0,3,5-8])|(18[0-9])|(147))\\d{8}$";
/**
* 驗證座機(jī)號,正確格式:xxx/xxxx-xxxxxxx/xxxxxxxx/
*/
private static final String REGEX_TEL = "^0\\d{2,3}[- ]?\\d{7,8}";
/**
* 驗證郵箱
*/
private static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
/**
* 驗證url
*/
private static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?";
/**
* 驗證漢字
*/
private static final String REGEX_CHZ = "^[\\u4e00-\\u9fa5]+$";
/**
* 驗證用戶名,取值范圍為a-z,A-Z,0-9,"_",漢字呢诬,不能以"_"結(jié)尾,用戶名必須是6-20位
*/
private static final String REGEX_USERNAME = "^[\\w\\u4e00-\\u9fa5]{6,20}(?
/**
* 驗證IP地址
*/
private static final String REGEX_IP = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
? ?//If u want more please visit http://toutiao.com/i6231678548520731137/
? ?/**
? ? * @param string 待驗證文本
? ? * @return 是否符合手機(jī)號(簡單)格式
? ? */
? ?public static boolean isMobileSimple(String string) {
? ? ? ?return isMatch(REGEX_MOBILE_SIMPLE, string);
? ?}
? ?/**
? ? * @param string 待驗證文本
? ? * @return 是否符合手機(jī)號(精確)格式
? ? */
? ?public static boolean isMobileExact(String string) {
? ? ? ?return isMatch(REGEX_MOBILE_EXACT, string);
? ?}
? ?/**
? ? * @param string 待驗證文本
? ? * @return 是否符合座機(jī)號碼格式
? ? */
? ?public static boolean isTel(String string) {
? ? ? ?return isMatch(REGEX_TEL, string);
? ?}
? ?/**
? ? * @param string 待驗證文本
? ? * @return 是否符合郵箱格式
? ? */
? ?public static boolean isEmail(String string) {
? ? ? ?return isMatch(REGEX_EMAIL, string);
? ?}
? ?/**
? ? * @param string 待驗證文本
? ? * @return 是否符合網(wǎng)址格式
? ? */
? ?public static boolean isURL(String string) {
? ? ? ?return isMatch(REGEX_URL, string);
? ?}
? ?/**
? ? * @param string 待驗證文本
? ? * @return 是否符合漢字
? ? */
? ?public static boolean isChz(String string) {
? ? ? ?return isMatch(REGEX_CHZ, string);
? ?}
? ?/**
? ? * @param string 待驗證文本
? ? * @return 是否符合用戶名
? ? */
? ?public static boolean isUsername(String string) {
? ? ? ?return isMatch(REGEX_USERNAME, string);
? ?}
? ?/**
? ? * @param regex ?正則表達(dá)式字符串
? ? * @param string 要匹配的字符串
? ? * @return 如果str 符合 regex的正則表達(dá)式格式,返回true, 否則返回 false;
? ? */
? ?public static boolean isMatch(String regex, String string) {
? ? ? ?return !TextUtils.isEmpty(string) && Pattern.matches(regex, string);
? ?}
}
6涌哲、像素相關(guān),獲取Android屏幕寬度尚镰,控件寬度阀圾,dp跟px互轉(zhuǎn)
public class PixelUtil {
? ?//獲取運行屏幕寬度
? ?public static int getScreenWidth(Activity context) {
? ? ? ?DisplayMetrics dm = new DisplayMetrics();
? ? ? ?context.getWindowManager().getDefaultDisplay().getMetrics(dm);
? ? ? ?//寬度 dm.widthPixels
? ? ? ?//高度 dm.heightPixels
// ? ? ? ?LogUtil.i("info", "getScreenWidth" + dm.widthPixels);
? ? ? ?return dm.widthPixels;
? ?}
? ?/**
? ? * 獲取控件寬
? ? */
? ?public static int getWidth(View view) {
? ? ? ?int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
? ? ? ?int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
? ? ? ?view.measure(w, h);
? ? ? ?return (view.getMeasuredWidth());
? ?}
? ?//DP轉(zhuǎn)PX
? ?public static int dp2px(Activity context, float dpValue) {
? ? ? ?final float scale = context.getResources().getDisplayMetrics().density;
? ? ? ?return (int) (dpValue * scale + 0.5f);
? ?}
? ?//PX轉(zhuǎn)DP
? ?public static int px2dp(Activity context, float pxValue) {
? ? ? ?final float scale = context.getResources().getDisplayMetrics().density;
? ? ? ?return (int) (pxValue / scale + 0.5f);
? ?}
}
5、去除double類型數(shù)字尾巴的0
如:50.00 --> 50
public class MathDataUtil {
? ?public static BigDecimal stripTrailingZeros(double d) {//去除double尾巴的0
? ? ? ?return new BigDecimal(d).stripTrailingZeros();
? ?}
}
4狗唉、時間戳相關(guān)
public class DateUtils {
? ?private static SimpleDateFormat sf;
? ?private static SimpleDateFormat sdf;
? ?/**
? ? * 獲取系統(tǒng)時間 格式為:"yyyy/MM/dd "
? ? **/
? ?public static String getCurrentDate() {
? ? ? ?Date d = new Date();
? ? ? ?sf = new SimpleDateFormat("yyyy年MM月dd日");
? ? ? ?return sf.format(d);
? ?}
? ?/**
? ? * 獲取系統(tǒng)時間 格式為:"yyyy "
? ? **/
? ?public static String getCurrentYear() {
? ? ? ?Date d = new Date();
? ? ? ?sf = new SimpleDateFormat("yyyy");
? ? ? ?return sf.format(d);
? ?}
? ?/**
? ? * 獲取系統(tǒng)時間 格式為:"MM"
? ? **/
? ?public static String getCurrentMonth() {
? ? ? ?Date d = new Date();
? ? ? ?sf = new SimpleDateFormat("MM");
? ? ? ?return sf.format(d);
? ?}
? ?/**
? ? * 獲取系統(tǒng)時間 格式為:"dd"
? ? **/
? ?public static String getCurrentDay() {
? ? ? ?Date d = new Date();
? ? ? ?sf = new SimpleDateFormat("dd");
? ? ? ?return sf.format(d);
? ?}
? ?/**
? ? * 獲取當(dāng)前時間戳
? ? *
? ? * @return
? ? */
? ?public static long getCurrentTime() {
? ? ? ?long d = new Date().getTime() / 1000;
? ? ? ?return d;
? ?}
? ?/**
? ? * 時間戳轉(zhuǎn)換成字符竄
? ? */
? ?public static String getDateToString(long time) {
? ? ? ?Date d = new Date(time * 1000);
? ? ? ?sf = new SimpleDateFormat("yyyy年MM月dd日");
? ? ? ?return sf.format(d);
? ?}
? ?/**
? ? * 時間戳中獲取年
? ? */
? ?public static String getYearFromTime(long time) {
? ? ? ?Date d = new Date(time * 1000);
? ? ? ?sf = new SimpleDateFormat("yyyy");
? ? ? ?return sf.format(d);
? ?}
? ?/**
? ? * 時間戳中獲取月
? ? */
? ?public static String getMonthFromTime(long time) {
? ? ? ?Date d = new Date(time * 1000);
? ? ? ?sf = new SimpleDateFormat("MM");
? ? ? ?return sf.format(d);
? ?}
? ?/**
? ? * 時間戳中獲取日
? ? */
? ?public static String getDayFromTime(long time) {
? ? ? ?Date d = new Date(time * 1000);
? ? ? ?sf = new SimpleDateFormat("dd");
? ? ? ?return sf.format(d);
? ?}
? ?/**
? ? * 將字符串轉(zhuǎn)為時間戳
? ? */
? ?public static long getStringToDate(String time) {
? ? ? ?sdf = new SimpleDateFormat("yyyy年MM月dd日");
? ? ? ?Date date = new Date();
? ? ? ?try {
? ? ? ? ? ?date = sdf.parse(time);
? ? ? ?} catch (ParseException e) {
? ? ? ? ? ?// TODO Auto-generated catch block
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ? ? ?return date.getTime();
? ?}
}
3初烘、文件相關(guān)
public class FileUtils {
? ?public static String SDPATH = Environment.getExternalStorageDirectory() + "/formats/";// 獲取文件夾
? ?// 保存圖片
? ?public static boolean saveBitmap(Bitmap mBitmap, String path, String imgName) {
? ? ? ?String sdStatus = Environment.getExternalStorageState();
? ? ? ?if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測sd是否可用
? ? ? ? ? ?return false;
? ? ? ?}
? ? ? ?FileOutputStream b = null;
? ? ? ?File file = new File(path);
? ? ? ?file.mkdirs();// 創(chuàng)建文件夾
? ? ? ?String fileName = path + imgName;
// ? ? ? ?delFile(path, imgName);//刪除本地舊圖
? ? ? ?try {
? ? ? ? ? ?b = new FileOutputStream(fileName);
? ? ? ? ? ?mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數(shù)據(jù)寫入文件
? ? ? ?} catch (FileNotFoundException e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?} finally {
? ? ? ? ? ?try {
? ? ? ? ? ? ? ? ? ?b.flush();
? ? ? ? ? ? ? ? ? ?b.close();
? ? ? ? ? ?} catch (IOException e) {
? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ?}
? ? ? ?}
? ? ? ?return true;
? ?}
? ?public static File createSDDir(String dirName) throws IOException {
? ? ? ?File dir = new File(SDPATH + dirName);
? ? ? ?if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
? ? ? ? ? ?System.out.println("createSDDir:" + dir.getAbsolutePath());
? ? ? ? ? ?System.out.println("createSDDir:" + dir.mkdir());
? ? ? ?}
? ? ? ?return dir;
? ?}
? ?public static boolean isFileExist(String fileName) {
? ? ? ?File file = new File(SDPATH + fileName);
? ? ? ?file.isFile();
? ? ? ?return file.exists();
? ?}
? ?// 刪除文件
? ?public static void delFile(String path, String fileName) {
? ? ? ?File file = new File(path + fileName);
? ? ? ?if (file.isFile()) {
? ? ? ? ? ?file.delete();
? ? ? ?}
? ? ? ?file.exists();
? ?}
? ?// 刪除文件夾和文件夾里面的文件
? ?public static void deleteDir() {
? ? ? ?File dir = new File(SDPATH);
? ? ? ?if (dir == null || !dir.exists() || !dir.isDirectory())
? ? ? ? ? ?return;
? ? ? ?for (File file : dir.listFiles()) {
? ? ? ? ? ?if (file.isFile())
? ? ? ? ? ? ? ?file.delete(); // 刪除所有文件
? ? ? ? ? ?else if (file.isDirectory())
? ? ? ? ? ? ? ?deleteDir(); // 遞規(guī)的方式刪除文件夾
? ? ? ?}
? ? ? ?dir.delete();// 刪除目錄本身
? ?}
? ?public static boolean fileIsExists(String path) {
? ? ? ?try {
? ? ? ? ? ?File f = new File(path);
? ? ? ? ? ?if (!f.exists()) {
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}
? ? ? ?} catch (Exception e) {
? ? ? ? ? ?return false;
? ? ? ?}
? ? ? ?return true;
? ?}
}
2、AlertDialogUtil
/**
* Created by david on 2016/8/26.
*
* 使用觀察者模式來實現(xiàn)確定結(jié)果回調(diào)
*
* 調(diào)用方式:
* AlertDialogUtil dialogUtil = new AlertDialogUtil(context);
* dialogUtil.showDialog("確定刪除已上傳的圖片?");
* dialogUtil.setDialogPositiveButtonListener(new AlertDialogUtil.DialogPositiveButtonListener() {
*
* ? ? ?@Override
* ? ? public void setDialogPositiveButtonListener() {
*
* ? ?}
* });
*/
public class AlertDialogUtil {
? ?public Context context;
? ?private DialogPositiveButtonListener listener;
? ?public AlertDialogUtil(Context context) {
? ? ? ?this.context = context;
? ?}
? ?public void showDialog(String message) {
? ? ? ?AlertDialog.Builder dialog = new AlertDialog.Builder(context);
? ? ? ?dialog.setMessage(message);
? ? ? ?dialog.setCancelable(false);//點擊框外取消
? ? ? ?dialog.setPositiveButton("確定", new DialogInterface.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(DialogInterface dialogInterface, int i) {
? ? ? ? ? ? ? ?if (listener != null) {
? ? ? ? ? ? ? ? ? ?listener.setDialogPositiveButtonListener();
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
? ? ? ?});
? ? ? ?dialog.setNegativeButton("取消", null);
? ? ? ?dialog.show();
? ?}
? ?public void setDialogPositiveButtonListener(DialogPositiveButtonListener listener) {
? ? ? ?this.listener = listener;
? ?}
? ?public interface DialogPositiveButtonListener {
? ? ? ?void setDialogPositiveButtonListener();
? ?}
}
1肾筐、ToastUtil
public class ToastUtils {
? ?static Toast toast;
? ?private ToastUtils() {
? ? ? ?throw new AssertionError();
? ?}
? ?public static void show(Context paramContext, int paramInt) {
? ? ? ?show(paramContext, paramContext.getResources().getText(paramInt), 0);
? ?}
? ?public static void show(Context paramContext, int paramInt1, int paramInt2) {
? ? ? ?show(paramContext, paramContext.getResources().getText(paramInt1), paramInt2);
? ?}
? ?public static void show(Context paramContext, CharSequence paramCharSequence) {
? ? ? ?show(paramContext, paramCharSequence, 0);
? ?}
? ?public static void show(Context paramContext, CharSequence paramCharSequence, int paramInt) {
? ? ? ?if (toast != null) {
? ? ? ? ? ?toast.setText(paramCharSequence);
? ? ? ? ? ?toast.setDuration(paramInt);
? ? ? ? ? ?toast.show();//
? ? ? ? ? ?return;
? ? ? ?}
? ? ? ?toast = Toast.makeText(paramContext, paramCharSequence, paramInt);
? ? ? ?toast.show();
? ?}
? ?public static void toastCancle() {
? ? ? ?if (toast != null)
? ? ? ? ? ?toast.cancel();
? ?}
}