Android进阶之路 - 常用小工具

时间工具:

  • 获取当前日期
 /**
     * 获取当前日期--(格式2017-12-06)
     */
    public static String getCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(Calendar.getInstance().getTime());
    }
  • 输入时间加上1天再格式化
 /**
     * 输入时间加上1天再格式化
     * @param timeString 输入的时间(格式:2017-12-12)
     * @return 加过1天的时间
     */
    public static String getOneDayIncreaseTime(String timeString) {
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d;
        try {
            d = sdf.parse(timeString);
            //输入的时间加上1天的时间戳
            long l = d.getTime() + 86400000L;
            //单位秒
            timeStamp = sdf.format(new Date(l));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return timeStamp;
    }
  • 输入时间加上90天再格式化
/**
     * 输入时间加上90天再格式化
     * @param timeString 输入的时间(格式:2017-12-12)
     * @return 加过90天的时间
     */
    public static String getIncreaseTime(String timeString) {
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d;
        try {
            d = sdf.parse(timeString);
            //输入的时间加上90天的时间戳
            long l = d.getTime() + 7776000000L;
            //单位秒
            timeStamp = sdf.format(new Date(l));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return timeStamp;
    }
  • 时间戳转字符串
  public static String getStrTime(String timeStamp){
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
        long  l = Long.valueOf(timeStamp);
        timeString = sdf.format(new Date(l));//单位秒
        return timeString;
    }
  • 字符串转时间戳
  public static String getTime(String timeString){
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
        Date d;
        try{
            d = sdf.parse(timeString);
            long l = d.getTime();
            timeStamp = String.valueOf(l);
        } catch(ParseException e){
            e.printStackTrace();
        }
        return timeStamp;
    }

数据类型转换工具:

  • double→格式化的String
public static String formatMoney(double d) {
        DecimalFormat myformat = new DecimalFormat();
        myformat.applyPattern("0.00");
        //myformat.applyPattern("###,###.00");
        if (0 == d) {
            return "0.00";
        }
        return myformat.format(d);
    }
  • 把String转化为double
   public static double convertToDouble(String number, double defaultValue) {
        if (TextUtils.isEmpty(number)) {
            return defaultValue;
        }
        try {
            return Double.parseDouble(number);
        } catch (Exception e) {
            return defaultValue;
        }

    }
  • 把String转化为float
public static float convertToFloat(String number, float defaultValue) {
        if (TextUtils.isEmpty(number)) {
            return defaultValue;
        }
        try {
            return Float.parseFloat(number);
        } catch (Exception e) {
            return defaultValue;
        }

    }
  • 保留两位小数
/**
     * 保留两位小数
     * @param s
     * @param et
     */
    public static void keep2Decimal(TextWatcher textWatcher, CharSequence s, EditText et) {
        if (s.toString().contains(".")) {
            if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                s = s.toString().subSequence(0, s.toString().indexOf(".") + 3);
                et.removeTextChangedListener(textWatcher);
                et.setText(s);
                et.setSelection(s.length());
                et.addTextChangedListener(textWatcher);
            }
        }
        if (".".equals(s.toString())) {
            s = "0" + s;
            et.removeTextChangedListener(textWatcher);
            et.setText(s);
            et.setSelection(2);
            et.addTextChangedListener(textWatcher);
        }
        if (s.toString().startsWith("0") && s.toString().trim().length() > 1 && !".".equals(s.toString().substring(1, 2))) {
            et.removeTextChangedListener(textWatcher);
            et.setText(s.toString().substring(1, s.length()));
            et.setSelection(s.length() - 1);
            et.addTextChangedListener(textWatcher);
        }
    }

设备类型工具:

  • 获取手机Android 版本(4.4、5.0、5.1 …)
    /**
     * 获取手机Android 版本(4.4、5.0、5.1 ...)
     * @return String
     */
    public static String getBuildVersion() {
        return Build.VERSION.RELEASE;
    }
  • 获取手机Android API等级(22、23 …)
   /**
     * 获取手机Android API等级(22、23 ...)
     * @return int
     */
    public static int getBuildLevel() {
        return Build.VERSION.SDK_INT;
    }
  • 获取手机型号
    /**
     * 获取手机型号
     * @return String
     */
    public static String getPhoneModel() {
        return Build.MODEL;
    }
  • 获取手机品牌
 /**
     * 获取手机品牌
     * @return String
     */
    public static String getPhoneBrand() {
        return Build.BRAND;
    }
  • dp 转化为 px
  /**
     * dp 转化为 px
     * @param context context
     * @param dpValue dpValue
     * @return int
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
  • px 转化为 dp
/**
     * px 转化为 dp
     * @param context context
     * @param pxValue pxValue
     */
      public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
  • px 转化为 sp
  /**
     * px 转化为 sp
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }
  • sp 转化为 px
   public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }
  • 获取设备宽度(px)
    /**
     * 获取设备宽度(px)
     * @param context context
     * @return int
     */
    public static int deviceWidth(Context context) {
        return context.getResources().getDisplayMetrics().widthPixels;
    }
  • 获取设备高度(px)
  /**
     * 获取设备高度(px)
     */
  public static int deviceHeight(Context context) {
        return context.getResources().getDisplayMetrics().heightPixels;
    }

猜你喜欢

转载自blog.csdn.net/qq_20451879/article/details/80337334