Android开发中常用到的工具类

移动开发 Android
日常开发中很多东西都是写过无数遍的,我本人没有每次都去重新写的习惯(不知道有没有小伙伴会如此耿直??)那么整理好自己的工具类还是有必要的。这里就记录几个目前为止我使用较多的。

作为一个程序员界的新晋司机,也是时候整理一些东西了,两三年的路走来,代码也是边写边忘、边走边丢,很多问题忙着忙着就忘了,决定写点随笔供自己闲余时间回望,有需要的读者也可以随意瞄几眼,哪里有错有问题可以提出来,虽然我不见得会改,O(∩_∩)O哈哈~

[[228901]]

日常开发中很多东西都是写过无数遍的,我本人没有每次都去重新写的习惯(不知道有没有小伙伴会如此耿直??)那么整理好自己的工具类还是有必要的。这里就记录几个目前为止我使用较多的。

常用工具类

 

  1.  /** 
  2.      * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
  3.      */ 
  4.     public static int dip2px(Context context, float dpValue) { 
  5.         final float scale = context.getResources().getDisplayMetrics().density; 
  6.         return (int) (dpValue * scale + 0.5f); 
  7.     } 
  8.  
  9.     /** 
  10.      * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
  11.      */ 
  12.     public static int px2dip(Context context, float pxValue) { 
  13.         final float scale = context.getResources().getDisplayMetrics().density; 
  14.         return (int) (pxValue / scale + 0.5f); 
  15.     } 
  16.  
  17.     /** 
  18.      * Md5 32位 or 16位 加密 
  19.      * 
  20.      * @param plainText 
  21.      * @return 32位加密 
  22.      */ 
  23.     public static String Md5(String plainText) { 
  24.         StringBuffer buf = null
  25.         try { 
  26.             MessageDigest md = MessageDigest.getInstance("MD5"); 
  27.             md.update(plainText.getBytes()); 
  28.             byte b[] = md.digest(); 
  29.             int i; 
  30.             buf = new StringBuffer(""); 
  31.             for (int offset = 0; offset < b.length; offset++) { 
  32.                 i = b[offset]; 
  33.                 if (i < 0) i += 256; 
  34.                 if (i < 16) 
  35.                     buf.append("0"); 
  36.                 buf.append(Integer.toHexString(i)); 
  37.             } 
  38.  
  39.         } catch (NoSuchAlgorithmException e) { 
  40.             e.printStackTrace(); 
  41.         } 
  42.         return buf.toString(); 
  43.     } 
  44.  
  45.  /** 
  46.      * 手机号正则判断 
  47.      * @param str 
  48.      * @return 
  49.      * @throws PatternSyntaxException 
  50.      */ 
  51.     public static boolean isPhoneNumber(String str) throws PatternSyntaxException { 
  52.         if (str != null) { 
  53.         String pattern = "(13\\d|14[579]|15[^4\\D]|17[^49\\D]|18\\d)\\d{8}"
  54.  
  55.         Pattern r = Pattern.compile(pattern); 
  56.         Matcher m = r.matcher(str); 
  57.             return m.matches(); 
  58.         } else { 
  59.             return false
  60.         } 
  61.     } 
  62.  
  63. /** 
  64.      * 检测当前网络的类型 是否是wifi 
  65.      * 
  66.      * @param context 
  67.      * @return 
  68.      */ 
  69.     public static int checkedNetWorkType(Context context) { 
  70.         if (!checkedNetWork(context)) { 
  71.             return 0;//无网络 
  72.         } 
  73.         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
  74.         if (cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()) { 
  75.             return 1;//wifi 
  76.         } else { 
  77.             return 2;//非wifi 
  78.         } 
  79.     } 
  80.  
  81.     /** 
  82.      * 检查是否连接网络 
  83.      * 
  84.      * @param context 
  85.      * @return 
  86.      */ 
  87.     public static boolean checkedNetWork(Context context) { 
  88.         // 获得连接设备管理器 
  89.         ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
  90.         if (cm == nullreturn false
  91.         /** 
  92.          * 获取网络连接对象 
  93.          */ 
  94.         NetworkInfo networkInfo = cm.getActiveNetworkInfo(); 
  95.  
  96.         if (networkInfo == null || !networkInfo.isAvailable()) { 
  97.             return false
  98.         } 
  99.         return true
  100.     } 
  101.  
  102.     /** 
  103.      * 检测GPS是否打开 
  104.      * 
  105.      * @return 
  106.      */ 
  107.     public static boolean checkGPSIsOpen(Context context) { 
  108.         boolean isOpen; 
  109.         LocationManager locationManager = (LocationManager) context 
  110.                 .getSystemService(Context.LOCATION_SERVICE); 
  111.         if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)||locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ 
  112.             isOpen=true
  113.         }else
  114.             isOpen = false
  115.         } 
  116.  
  117.         return isOpen; 
  118.     } 
  119.  
  120.     /** 
  121.      * 跳转GPS设置 
  122.      */ 
  123.     public static void openGPSSettings(final Context context) { 
  124.         if (checkGPSIsOpen(context)) { 
  125. //            initLocation(); //自己写的定位方法 
  126.         } else { 
  127. //            //没有打开则弹出对话框 
  128.             AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialogCustom); 
  129.  
  130.             builder.setTitle("温馨提示"); 
  131.             builder.setMessage("当前应用需要打开定位功能。请点击\"设置\"-\"定位服务\"打开定位功能。"); 
  132.             //设置对话框是可取消的 
  133.             builder.setCancelable(false); 
  134.  
  135.             builder.setPositiveButton("设置", new DialogInterface.OnClickListener() { 
  136.                 @Override 
  137.                 public void onClick(DialogInterface dialogInterface, int i) { 
  138.                     dialogInterface.dismiss(); 
  139.                     //跳转GPS设置界面 
  140.                     Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
  141.                     context.startActivity(intent); 
  142.                 } 
  143.             }); 
  144.             builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { 
  145.                 @Override 
  146.                 public void onClick(DialogInterface dialogInterface, int i) { 
  147.                     dialogInterface.dismiss(); 
  148.                     ActivityManager.getInstance().exit(); 
  149.                 } 
  150.             }); 
  151.             AlertDialog alertDialog = builder.create(); 
  152.             alertDialog.show(); 
  153.         } 
  154.     } 
  155.  
  156.     /** 
  157.      * 字符串进行Base64编码 
  158.      * @param str 
  159.      */ 
  160.     public static String StringToBase64(String str){ 
  161.         String encodedString = Base64.encodeToString(str.getBytes(), Base64.DEFAULT); 
  162.         return encodedString; 
  163.     } 
  164.  
  165.     /** 
  166.      * 字符串进行Base64解码 
  167.      * @param encodedString 
  168.      * @return 
  169.      */ 
  170.     public static String Base64ToString(String encodedString){ 
  171.         String decodedString =new String(Base64.decode(encodedString,Base64.DEFAULT)); 
  172.         return decodedString; 
  173.     } 

这里还有一个根据经纬度计算两点间真实距离的,一般都直接使用所集成第三方地图SDK中包含的方法,这里还是给出代码

 

  1. /** 
  2.      * 补充:计算两点之间真实距离 
  3.      * 
  4.      * @return 米 
  5.      */ 
  6.     public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) { 
  7.         // 维度 
  8.         double lat1 = (Math.PI / 180) * latitude1; 
  9.         double lat2 = (Math.PI / 180) * latitude2; 
  10.  
  11.         // 经度 
  12.         double lon1 = (Math.PI / 180) * longitude1; 
  13.         double lon2 = (Math.PI / 180) * longitude2; 
  14.  
  15.         // 地球半径 
  16.         double R = 6371; 
  17.  
  18.         // 两点间距离 km,如果想要米的话,结果*1000就可以了 
  19.         double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R; 
  20.  
  21.         return d * 1000; 
  22.     } 

常用文件类

文件类的代码较多,这里就只给出读写文件的

 

  1. /** 
  2.      * 判断SD卡是否可用 
  3.      * @return SD卡可用返回true 
  4.      */ 
  5.     public static boolean hasSdcard() { 
  6.         String status = Environment.getExternalStorageState(); 
  7.         return Environment.MEDIA_MOUNTED.equals(status); 
  8.     } 
  9.  
  10.     /** 
  11.      * 读取文件的内容 
  12.      * <br> 
  13.      * 默认utf-8编码 
  14.      * @param filePath 文件路径 
  15.      * @return 字符串 
  16.      * @throws IOException 
  17.      */ 
  18.     public static String readFile(String filePath) throws IOException { 
  19.         return readFile(filePath, "utf-8"); 
  20.     } 
  21.  
  22.     /** 
  23.      * 读取文件的内容 
  24.      * @param filePath 文件目录 
  25.      * @param charsetName 字符编码 
  26.      * @return String字符串 
  27.      */ 
  28.     public static String readFile(String filePath, String charsetName) 
  29.             throws IOException { 
  30.         if (TextUtils.isEmpty(filePath)) 
  31.             return null
  32.         if (TextUtils.isEmpty(charsetName)) 
  33.             charsetName = "utf-8"
  34.         File file = new File(filePath); 
  35.         StringBuilder fileContent = new StringBuilder(""); 
  36.         if (file == null || !file.isFile()) 
  37.             return null
  38.         BufferedReader reader = null
  39.         try { 
  40.             InputStreamReader is = new InputStreamReader(new FileInputStream( 
  41.                     file), charsetName); 
  42.             reader = new BufferedReader(is); 
  43.             String line = null
  44.             while ((line = reader.readLine()) != null) { 
  45.                 if (!fileContent.toString().equals("")) { 
  46.                     fileContent.append("\r\n"); 
  47.                 } 
  48.                 fileContent.append(line); 
  49.             } 
  50.             return fileContent.toString(); 
  51.         } finally { 
  52.             if (reader != null) { 
  53.                 try { 
  54.                     reader.close(); 
  55.                 } catch (IOException e) { 
  56.                     e.printStackTrace(); 
  57.                 } 
  58.             } 
  59.         } 
  60.     } 
  61.  
  62.     /** 
  63.      * 读取文本文件到List字符串集合中(默认utf-8编码) 
  64.      * @param filePath 文件目录 
  65.      * @return 文件不存在返回null,否则返回字符串集合 
  66.      * @throws IOException 
  67.      */ 
  68.     public static List<String> readFileToList(String filePath) 
  69.             throws IOException { 
  70.         return readFileToList(filePath, "utf-8"); 
  71.     } 
  72.  
  73.     /** 
  74.      * 读取文本文件到List字符串集合中 
  75.      * @param filePath 文件目录 
  76.      * @param charsetName 字符编码 
  77.      * @return 文件不存在返回null,否则返回字符串集合 
  78.      */ 
  79.     public static List<String> readFileToList(String filePath, 
  80.                                               String charsetName) throws IOException { 
  81.         if (TextUtils.isEmpty(filePath)) 
  82.             return null
  83.         if (TextUtils.isEmpty(charsetName)) 
  84.             charsetName = "utf-8"
  85.         File file = new File(filePath); 
  86.         List<String> fileContent = new ArrayList<String>(); 
  87.         if (file == null || !file.isFile()) { 
  88.             return null
  89.         } 
  90.         BufferedReader reader = null
  91.         try { 
  92.             InputStreamReader is = new InputStreamReader(new FileInputStream( 
  93.                     file), charsetName); 
  94.             reader = new BufferedReader(is); 
  95.             String line = null
  96.             while ((line = reader.readLine()) != null) { 
  97.                 fileContent.add(line); 
  98.             } 
  99.             return fileContent; 
  100.         } finally { 
  101.             if (reader != null) { 
  102.                 try { 
  103.                     reader.close(); 
  104.                 } catch (IOException e) { 
  105.                     e.printStackTrace(); 
  106.                 } 
  107.             } 
  108.         } 
  109.     } 
  110.  
  111.     /** 
  112.      * 向文件中写入数据 
  113.      * @param filePath 文件目录 
  114.      * @param content 要写入的内容 
  115.      * @param append 如果为 true,则将数据写入文件末尾处,而不是写入文件开始处 
  116.      * @return 写入成功返回true, 写入失败返回false 
  117.      * @throws IOException 
  118.      */ 
  119.     public static boolean writeFile(String filePath, String content, 
  120.                                     boolean append) throws IOException { 
  121.         if (TextUtils.isEmpty(filePath)) 
  122.             return false
  123.         if (TextUtils.isEmpty(content)) 
  124.             return false
  125.         FileWriter fileWriter = null
  126.         try { 
  127.             createFile(filePath); 
  128.             fileWriter = new FileWriter(filePath, append); 
  129.             fileWriter.write(content); 
  130.             fileWriter.flush(); 
  131.             return true
  132.         } finally { 
  133.             if (fileWriter != null) { 
  134.                 try { 
  135.                     fileWriter.close(); 
  136.                 } catch (IOException e) { 
  137.                     e.printStackTrace(); 
  138.                 } 
  139.             } 
  140.         } 
  141.     } 
  142.  
  143.  
  144.     /** 
  145.      * 向文件中写入数据<br> 
  146.      * 默认在文件开始处重新写入数据 
  147.      * @param filePath 文件目录 
  148.      * @param stream 字节输入流 
  149.      * @return 写入成功返回true,否则返回false 
  150.      * @throws IOException 
  151.      */ 
  152.     public static boolean writeFile(String filePath, InputStream stream) 
  153.             throws IOException { 
  154.         return writeFile(filePath, stream, false); 
  155.     } 
  156.  
  157.     /** 
  158.      * 向文件中写入数据 
  159.      * @param filePath 文件目录 
  160.      * @param stream 字节输入流 
  161.      * @param append 如果为 true,则将数据写入文件末尾处; 
  162.      *              为false时,清空原来的数据,从头开始写 
  163.      * @return 写入成功返回true,否则返回false 
  164.      * @throws IOException 
  165.      */ 
  166.     public static boolean writeFile(String filePath, InputStream stream, 
  167.                                     boolean append) throws IOException { 
  168.         if (TextUtils.isEmpty(filePath)) 
  169.             throw new NullPointerException("filePath is Empty"); 
  170.         if (stream == null
  171.             throw new NullPointerException("InputStream is null"); 
  172.         return writeFile(new File(filePath), stream, 
  173.                 append); 
  174.     } 
  175.  
  176.     /** 
  177.      * 向文件中写入数据 
  178.      * 默认在文件开始处重新写入数据 
  179.      * @param file 指定文件 
  180.      * @param stream 字节输入流 
  181.      * @return 写入成功返回true,否则返回false 
  182.      * @throws IOException 
  183.      */ 
  184.     public static boolean writeFile(File file, InputStream stream) 
  185.             throws IOException { 
  186.         return writeFile(file, stream, false); 
  187.     } 
  188.  
  189.     /** 
  190.      * 向文件中写入数据 
  191.      * @param file 指定文件 
  192.      * @param stream 字节输入流 
  193.      * @param append 为true时,在文件开始处重新写入数据; 
  194.      *              为false时,清空原来的数据,从头开始写 
  195.      * @return 写入成功返回true,否则返回false 
  196.      * @throws IOException 
  197.      */ 
  198.     public static boolean writeFile(File file, InputStream stream, 
  199.                                     boolean append) throws IOException { 
  200.         if (file == null
  201.             throw new NullPointerException("file = null"); 
  202.         OutputStream out = null
  203.         try { 
  204.             createFile(file.getAbsolutePath()); 
  205.             out = new FileOutputStream(file, append); 
  206.             byte data[] = new byte[1024]; 
  207.             int length = -1; 
  208.             while ((length = stream.read(data)) != -1) { 
  209.                 out.write(data, 0, length); 
  210.             } 
  211.             out.flush(); 
  212.             return true
  213.         } finally { 
  214.             if (out != null) { 
  215.                 try { 
  216.                     out.close(); 
  217.                     stream.close(); 
  218.                 } catch (IOException e) { 
  219.                     e.printStackTrace(); 
  220.                 } 
  221.             } 
  222.         } 
  223.     } 

日期工具类

 

  1. /** 
  2.      * 将long时间转成yyyy-MM-dd HH:mm:ss字符串<br> 
  3.      * @param timeInMillis 时间long值 
  4.      * @return yyyy-MM-dd HH:mm:ss 
  5.      */ 
  6.     public static String getDateTimeFromMillis(long timeInMillis) { 
  7.         return getDateTimeFormat(new Date(timeInMillis)); 
  8.     } 
  9.  
  10. /** 
  11.      * 将date转成yyyy-MM-dd HH:mm:ss字符串 
  12.      * <br> 
  13.      * @param date Date对象 
  14.      * @return  yyyy-MM-dd HH:mm:ss 
  15.      */ 
  16.     public static String getDateTimeFormat(Date date) { 
  17.         return dateSimpleFormat(date, defaultDateTimeFormat.get()); 
  18.     } 
  19.  
  20.     /** 
  21.      * 将年月日的int转成yyyy-MM-dd的字符串 
  22.      * @param year 年 
  23.      * @param month 月 1-12 
  24.      * @param day 日 
  25.      * 注:月表示Calendar的月,比实际小1 
  26.      * 对输入项未做判断 
  27.      */ 
  28.     public static String getDateFormat(int yearint monthint day) { 
  29.         return getDateFormat(getDate(yearmonthday)); 
  30.     } 
  31.  
  32. /** 
  33.      * 获得HH:mm:ss的时间 
  34.      * @param date 
  35.      * @return 
  36.      */ 
  37.     public static String getTimeFormat(Date date) { 
  38.         return dateSimpleFormat(date, defaultTimeFormat.get()); 
  39.     } 
  40.  
  41.     /** 
  42.      * 格式化日期显示格式 
  43.      * @param sdate 原始日期格式 "yyyy-MM-dd" 
  44.      * @param format 格式化后日期格式 
  45.      * @return 格式化后的日期显示 
  46.      */ 
  47.     public static String dateFormat(String sdate, String format) { 
  48.         SimpleDateFormat formatter = new SimpleDateFormat(format); 
  49.         java.sql.Date date = java.sql.Date.valueOf(sdate); 
  50.         return dateSimpleFormat(date, formatter); 
  51.     } 
  52.  
  53.     /** 
  54.      * 格式化日期显示格式 
  55.      * @param date Date对象 
  56.      * @param format 格式化后日期格式 
  57.      * @return 格式化后的日期显示 
  58.      */ 
  59.     public static String dateFormat(Date date, String format) { 
  60.         SimpleDateFormat formatter = new SimpleDateFormat(format); 
  61.         return dateSimpleFormat(date, formatter); 
  62.     } 
  63.  /** 
  64.      * 将date转成字符串 
  65.      * @param date Date 
  66.      * @param format SimpleDateFormat 
  67.      * <br> 
  68.      * 注: SimpleDateFormat为空时,采用默认的yyyy-MM-dd HH:mm:ss格式 
  69.      * @return yyyy-MM-dd HH:mm:ss 
  70.      */ 
  71.     public static String dateSimpleFormat(Date date, SimpleDateFormat format) { 
  72.         if (format == null
  73.             format = defaultDateTimeFormat.get(); 
  74.         return (date == null ? "" : format.format(date)); 
  75.     } 
  76.  
  77.     /** 
  78.      * 将"yyyy-MM-dd HH:mm:ss" 格式的字符串转成Date 
  79.      * @param strDate 时间字符串 
  80.      * @return Date 
  81.      */ 
  82.     public static Date getDateByDateTimeFormat(String strDate) { 
  83.         return getDateByFormat(strDate, defaultDateTimeFormat.get()); 
  84.     } 
  85.  
  86.     /** 
  87.      * 将"yyyy-MM-dd" 格式的字符串转成Date 
  88.      * @param strDate 
  89.      * @return Date 
  90.      */ 
  91.     public static Date getDateByDateFormat(String strDate) { 
  92.         return getDateByFormat(strDate, defaultDateFormat.get()); 
  93.     } 
  94.  
  95.     /** 
  96.      * 将指定格式的时间字符串转成Date对象 
  97.      * @param strDate 时间字符串 
  98.      * @param format 格式化字符串 
  99.      * @return Date 
  100.      */ 
  101.     public static Date getDateByFormat(String strDate, String format) { 
  102.         return getDateByFormat(strDate, new SimpleDateFormat(format)); 
  103.     } 
  104.  
  105.     /** 
  106.      * 将String字符串按照一定格式转成Date<br> 
  107.      * 注: SimpleDateFormat为空时,采用默认的yyyy-MM-dd HH:mm:ss格式 
  108.      * @param strDate 时间字符串 
  109.      * @param format SimpleDateFormat对象 
  110.      * @exception ParseException 日期格式转换出错 
  111.      */ 
  112.     private static Date getDateByFormat(String strDate, SimpleDateFormat format) { 
  113.         if (format == null
  114.             format = defaultDateTimeFormat.get(); 
  115.         try { 
  116.             return format.parse(strDate); 
  117.         } catch (ParseException e) { 
  118.             e.printStackTrace(); 
  119.         } 
  120.         return null
  121.     } 
  122.  
  123.     /** 
  124.      * 将年月日的int转成date 
  125.      * @param year 年 
  126.      * @param month 月 1-12 
  127.      * @param day 日 
  128.      * 注:月表示Calendar的月,比实际小1 
  129.      */ 
  130.     public static Date getDate(int yearint monthint day) { 
  131.         Calendar mCalendar = Calendar.getInstance(); 
  132.         mCalendar.set(yearmonth - 1, day); 
  133.         return mCalendar.getTime(); 
  134.     } 
  135.  
  136.     /** 
  137.      * 求两个日期相差天数 
  138.      * 
  139.      * @param strat 起始日期,格式yyyy-MM-dd 
  140.      * @param end 终止日期,格式yyyy-MM-dd 
  141.      * @return 两个日期相差天数 
  142.      */ 
  143.     public static long getIntervalDays(String strat, String end) { 
  144.         return ((java.sql.Date.valueOf(end)).getTime() - (java.sql.Date 
  145.                 .valueOf(strat)).getTime()) / (3600 * 24 * 1000); 
  146.     } 
  147.  
  148.     /** 
  149.      * 获得当前年份 
  150.      * @return year(int
  151.      */ 
  152.     public static int getCurrentYear() { 
  153.         Calendar mCalendar = Calendar.getInstance(); 
  154.         return mCalendar.get(Calendar.YEAR); 
  155.     } 
  156.  
  157.     /** 
  158.      * 获得当前月份 
  159.      * @return month(int) 1-12 
  160.      */ 
  161.     public static int getCurrentMonth() { 
  162.         Calendar mCalendar = Calendar.getInstance(); 
  163.         return mCalendar.get(Calendar.MONTH) + 1; 
  164.     } 
  165.  
  166.     /** 
  167.      * 获得当月几号 
  168.      * @return day(int
  169.      */ 
  170.     public static int getDayOfMonth() { 
  171.         Calendar mCalendar = Calendar.getInstance(); 
  172.         return mCalendar.get(Calendar.DAY_OF_MONTH); 
  173.     } 
  174.  
  175.     /** 
  176.      * 获得今天的日期(格式:yyyy-MM-dd) 
  177.      * @return yyyy-MM-dd 
  178.      */ 
  179.     public static String getToday() { 
  180.         Calendar mCalendar = Calendar.getInstance(); 
  181.         return getDateFormat(mCalendar.getTime()); 
  182.     } 
  183.  
  184.     /** 
  185.      * 获得昨天的日期(格式:yyyy-MM-dd) 
  186.      * @return yyyy-MM-dd 
  187.      */ 
  188.     public static String getYesterday() { 
  189.         Calendar mCalendar = Calendar.getInstance(); 
  190.         mCalendar.add(Calendar.DATE, -1); 
  191.         return getDateFormat(mCalendar.getTime()); 
  192.     } 
  193.  
  194.     /** 
  195.      * 获得前天的日期(格式:yyyy-MM-dd) 
  196.      * @return yyyy-MM-dd 
  197.      */ 
  198.     public static String getBeforeYesterday() { 
  199.         Calendar mCalendar = Calendar.getInstance(); 
  200.         mCalendar.add(Calendar.DATE, -2); 
  201.         return getDateFormat(mCalendar.getTime()); 
  202.     } 
  203.  
  204.     /** 
  205.      * 获得几天之前或者几天之后的日期 
  206.      * @param diff 差值:正的往后推,负的往前推 
  207.      * @return 
  208.      */ 
  209.     public static String getOtherDay(int diff) { 
  210.         Calendar mCalendar = Calendar.getInstance(); 
  211.         mCalendar.add(Calendar.DATE, diff); 
  212.         return getDateFormat(mCalendar.getTime()); 
  213.     } 
  214.  
  215.     /** 
  216.      * 取得给定日期加上一定天数后的日期对象. 
  217.      * 
  218.      * @param //date 给定的日期对象 
  219.      * @param amount 需要添加的天数,如果是向前的天数,使用负数就可以. 
  220.      * @return Date 加上一定天数以后的Date对象. 
  221.      */ 
  222.     public static String getCalcDateFormat(String sDate, int amount) { 
  223.         Date date = getCalcDate(getDateByDateFormat(sDate), amount); 
  224.         return getDateFormat(date); 
  225.     } 
  226.  
  227.     /** 
  228.      * 取得给定日期加上一定天数后的日期对象. 
  229.      * 
  230.      * @param date 给定的日期对象 
  231.      * @param amount 需要添加的天数,如果是向前的天数,使用负数就可以. 
  232.      * @return Date 加上一定天数以后的Date对象. 
  233.      */ 
  234.     public static Date getCalcDate(Date dateint amount) { 
  235.         Calendar cal = Calendar.getInstance(); 
  236.         cal.setTime(date); 
  237.         cal.add(Calendar.DATE, amount); 
  238.         return cal.getTime(); 
  239.     } 

基本都是代码,不是我懒(就是懒你咬我),主要是方便新手司机的copy好吧。好了,本篇随笔就到这里。

责任编辑:未丽燕 来源: 安卓巴士
相关推荐

2011-07-10 00:02:39

PHP

2014-05-13 09:55:13

iOS开发工具

2021-11-26 09:41:50

绘图工具软件工具开发

2010-06-04 14:00:32

Hadoop开发

2010-10-08 16:32:59

MySQL语句

2019-08-07 16:50:38

SQLjoingroup

2021-10-27 17:57:35

设计模式场景

2022-02-07 20:29:13

物联网数据传输单元DTU

2021-09-23 15:13:02

Spring依赖Java

2010-03-15 18:39:00

Python程序员

2023-03-17 16:49:42

开发Java框架

2022-08-30 21:01:17

开发Java框架

2014-10-21 15:11:29

Android工具类源码

2021-10-18 06:54:47

Go开源库业务

2011-08-22 10:40:07

SSH命令SSH命令

2015-01-09 11:29:53

Android开发工具类

2017-11-21 15:34:15

Linux 开发开源

2011-05-16 17:19:29

游戏开发iPhone

2010-03-24 19:09:43

Python语言

2019-11-20 08:56:51

Java工具类库IO
点赞
收藏

51CTO技术栈公众号