Linux编程
点击右侧关注,免费入门到精通!
作者丨LYing_
http://www.apkbus.com/blog-685845-76823.html
作为一个程序员界的新晋司机,也是时候整理一些东西了,两三年的路走来,代码也是边写边忘、边走边丢,很多问题忙着忙着就忘了,决定写点随笔供自己闲余时间回望,有需要的读者也可以随意瞄几眼,哪里有错有问题可以提出来,虽然我不见得会改,O(∩_∩)O哈哈~
日常开发中很多东西都是写过无数遍的,我本人没有每次都去重新写的习惯(不知道有没有小伙伴会如此耿直??)那么整理好自己的工具类还是有必要的。这里就记录几个目前为止我使用较多的。
常用工具类
这里还有一个根据经纬度计算两点间真实距离的,一般都直接使用所集成第三方地图SDK中包含的方法,这里还是给出代码
/** * 补充:计算两点之间真实距离 * * @return 米 */ public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
// 维度 double lat1 = (Math.PI / 180) * latitude1;
double lat2 = (Math.PI / 180) * latitude2;
// 经度 double lon1 = (Math.PI / 180) * longitude1;
double lon2 = (Math.PI / 180) * longitude2;
// 地球半径 double R = 6371;
// 两点间距离 km,如果想要米的话,结果*1000就可以了 double d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * R;
return d * 1000;
}
常用文件类
文件类的代码较多,这里就只给出读写文件的
/** * 判断SD卡是否可用 * @return SD卡可用返回true */ public static boolean hasSdcard() { String status = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(status); }
/** * 读取文件的内容 *
* 默认utf-8编码 * @param filePath 文件路径 * @return 字符串 * @throws IOException */ public static String readFile(String filePath) throws IOException {
return readFile(filePath, "utf-8"); }
/** * 读取文件的内容 * @param filePath 文件目录 * @param charsetName 字符编码 * @return String字符串 */ public static String readFile(String filePath, String charsetName) throws IOException {
if (TextUtils.isEmpty(filePath))
return null;
if (TextUtils.isEmpty(charsetName)) charsetName = "utf-8"; File file = new File(filePath); StringBuilder fileContent = new StringBuilder("");
if (file == null || !file.isFile())
return null; BufferedReader reader = null;
try { InputStreamReader is = new InputStreamReader(new FileInputStream( file), charsetName); reader = new BufferedReader(is); String line = null;
while ((line = reader.readLine()) != null) {
if (!fileContent.toString().equals("")) { fileContent.append("
"); } fileContent.append(line); }
return fileContent.toString(); } finally {
if (reader != null) {
try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
/** * 读取文本文件到List字符串集合中(默认utf-8编码) * @param filePath 文件目录 * @return 文件不存在返回null,否则返回字符串集合 * @throws IOException */ public static List readFileToList(String filePath) throws IOException { return readFileToList(filePath, "utf-8"); } /** * 读取文本文件到List字符串集合中 * @param filePath 文件目录 * @param charsetName 字符编码 * @return 文件不存在返回null,否则返回字符串集合 */ public static List readFileToList(String filePath, String charsetName) throws IOException { if (TextUtils.isEmpty(filePath)) return null; if (TextUtils.isEmpty(charsetName)) charsetName = "utf-8"; File file = new File(filePath); List fileContent = new ArrayList(); if (file == null || !file.isFile()) { return null; } BufferedReader reader = null; try { InputStreamReader is = new InputStreamReader(new FileInputStream( file), charsetName); reader = new BufferedReader(is); String line = null; while ((line = reader.readLine()) != null) { fileContent.add(line); } return fileContent; } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 向文件中写入数据 * @param filePath 文件目录 * @param content 要写入的内容 * @param append 如果为 true,则将数据写入文件末尾处,而不是写入文件开始处 * @return 写入成功返回true, 写入失败返回false * @throws IOException */ public static boolean writeFile(String filePath, String content,
boolean append) throws IOException { if (TextUtils.isEmpty(filePath)) return false; if (TextUtils.isEmpty(content)) return false; FileWriter fileWriter = null; try { createFile(filePath); fileWriter = new FileWriter(filePath, append); fileWriter.write(content); fileWriter.flush(); return true; } finally { if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 向文件中写入数据
* 默认在文件开始处重新写入数据 * @param filePath 文件目录 * @param stream 字节输入流 * @return 写入成功返回true,否则返回false * @throws IOException */ public static boolean writeFile(String filePath, InputStream stream) throws IOException { return writeFile(filePath, stream, false); } /** * 向文件中写入数据 * @param filePath 文件目录 * @param stream 字节输入流 * @param append 如果为 true,则将数据写入文件末尾处; * 为false时,清空原来的数据,从头开始写 * @return 写入成功返回true,否则返回false * @throws IOException */ public static boolean writeFile(String filePath, InputStream stream,
boolean append) throws IOException { if (TextUtils.isEmpty(filePath)) throw new NullPointerException("filePath is Empty"); if (stream == null) throw new NullPointerException("InputStream is null"); return writeFile(new File(filePath), stream, append); } /** * 向文件中写入数据 * 默认在文件开始处重新写入数据 * @param file 指定文件 * @param stream 字节输入流 * @return 写入成功返回true,否则返回false * @throws IOException */ public static boolean writeFile(File file, InputStream stream) throws IOException { return writeFile(file, stream, false); } /** * 向文件中写入数据 * @param file 指定文件 * @param stream 字节输入流 * @param append 为true时,在文件开始处重新写入数据; * 为false时,清空原来的数据,从头开始写 * @return 写入成功返回true,否则返回false * @throws IOException */ public static boolean writeFile(File file, InputStream stream,
boolean append) throws IOException { if (file == null) throw new NullPointerException("file = null"); OutputStream out = null; try { createFile(file.getAbsolutePath()); out = new FileOutputStream(file, append);
byte data[] = new byte[1024];
int length = -1; while ((length = stream.read(data)) != -1) { out.write(data, 0, length); } out.flush(); return true; } finally { if (out != null) { try { out.close(); stream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
日期工具类
这个可是相当的常用啊
/** * 将long时间转成yyyy-MM-dd HH:mm:ss字符串
* @param timeInMillis 时间long值 * @return yyyy-MM-dd HH:mm:ss */ public static String getDateTimeFromMillis(long timeInMillis) {
return getDateTimeFormat(new Date(timeInMillis)); }
/** * 将date转成yyyy-MM-dd HH:mm:ss字符串 *
* @param date Date对象 * @return yyyy-MM-dd HH:mm:ss */ public static String getDateTimeFormat(Date date) {
return dateSimpleFormat(date, defaultDateTimeFormat.get()); }
/** * 将年月日的int转成yyyy-MM-dd的字符串 * @param year 年 * @param month 月 1-12 * @param day 日 * 注:月表示Calendar的月,比实际小1 * 对输入项未做判断 */ public static String getDateFormat(int year, int month, int day) {
return getDateFormat(getDate(year, month, day)); }
/** * 获得HH:mm:ss的时间 * @param date * @return */ public static String getTimeFormat(Date date) {
return dateSimpleFormat(date, defaultTimeFormat.get()); }
/** * 格式化日期显示格式 * @param sdate 原始日期格式 "yyyy-MM-dd" * @param format 格式化后日期格式 * @return 格式化后的日期显示 */ public static String dateFormat(String sdate, String format) { SimpleDateFormat formatter = new SimpleDateFormat(format); java.sql.Date date = java.sql.Date.valueOf(sdate);
return dateSimpleFormat(date, formatter); }
/** * 格式化日期显示格式 * @param date Date对象 * @param format 格式化后日期格式 * @return 格式化后的日期显示 */ public static String dateFormat(Date date, String format) { SimpleDateFormat formatter = new SimpleDateFormat(format);
return dateSimpleFormat(date, formatter); }
/** * 将date转成字符串 * @param date Date * @param format SimpleDateFormat *
* 注: SimpleDateFormat为空时,采用默认的yyyy-MM-dd HH:mm:ss格式 * @return yyyy-MM-dd HH:mm:ss */ public static String dateSimpleFormat(Date date, SimpleDateFormat format) {
if (format == null) format = defaultDateTimeFormat.get();
return (date == null ? "" : format.format(date)); }
/** * 将"yyyy-MM-dd HH:mm:ss" 格式的字符串转成Date * @param strDate 时间字符串 * @return Date */ public static Date getDateByDateTimeFormat(String strDate) {
return getDateByFormat(strDate, defaultDateTimeFormat.get()); }
/** * 将"yyyy-MM-dd" 格式的字符串转成Date * @param strDate * @return Date */ public static Date getDateByDateFormat(String strDate) {
return getDateByFormat(strDate, defaultDateFormat.get()); }
/** * 将指定格式的时间字符串转成Date对象 * @param strDate 时间字符串 * @param format 格式化字符串 * @return Date */ public static Date getDateByFormat(String strDate, String format) {
return getDateByFormat(strDate, new SimpleDateFormat(format)); }
/** * 将String字符串按照一定格式转成Date
* 注: SimpleDateFormat为空时,采用默认的yyyy-MM-dd HH:mm:ss格式 * @param strDate 时间字符串 * @param format SimpleDateFormat对象 * @exception ParseException 日期格式转换出错 */ private static Date getDateByFormat(String strDate, SimpleDateFormat format) {
if (format == null) format = defaultDateTimeFormat.get();
try {
return format.parse(strDate); } catch (ParseException e) { e.printStackTrace(); } return null; }
/** * 将年月日的int转成date * @param year 年 * @param month 月 1-12 * @param day 日 * 注:月表示Calendar的月,比实际小1 */ public static Date getDate(int year, int month, int day) { Calendar mCalendar = Calendar.getInstance(); mCalendar.set(year, month - 1, day);
return mCalendar.getTime(); }
/** * 求两个日期相差天数 * * @param strat 起始日期,格式yyyy-MM-dd * @param end 终止日期,格式yyyy-MM-dd * @return 两个日期相差天数 */ public static long getIntervalDays(String strat, String end) {
return ((java.sql.Date.valueOf(end)).getTime() - (java.sql.Date .valueOf(strat)).getTime()) / (3600 * 24 * 1000); }
/** * 获得当前年份 * @return year(int) */ public static int getCurrentYear() { Calendar mCalendar = Calendar.getInstance();
return mCalendar.get(Calendar.YEAR); }
/** * 获得当前月份 * @return month(int) 1-12 */ public static int getCurrentMonth() { Calendar mCalendar = Calendar.getInstance();
return mCalendar.get(Calendar.MONTH) + 1; }
/** * 获得当月几号 * @return day(int) */ public static int getDayOfMonth() { Calendar mCalendar = Calendar.getInstance();
return mCalendar.get(Calendar.DAY_OF_MONTH); }
/** * 获得今天的日期(格式:yyyy-MM-dd) * @return yyyy-MM-dd */ public static String getToday() { Calendar mCalendar = Calendar.getInstance();
return getDateFormat(mCalendar.getTime()); }
/** * 获得昨天的日期(格式:yyyy-MM-dd) * @return yyyy-MM-dd */ public static String getYesterday() { Calendar mCalendar = Calendar.getInstance(); mCalendar.add(Calendar.DATE, -1);
return getDateFormat(mCalendar.getTime()); }
/** * 获得前天的日期(格式:yyyy-MM-dd) * @return yyyy-MM-dd */ public static String getBeforeYesterday() { Calendar mCalendar = Calendar.getInstance(); mCalendar.add(Calendar.DATE, -2);
return getDateFormat(mCalendar.getTime()); }
/** * 获得几天之前或者几天之后的日期 * @param diff 差值:正的往后推,负的往前推 * @return */ public static String getOtherDay(int diff) { Calendar mCalendar = Calendar.getInstance(); mCalendar.add(Calendar.DATE, diff);
return getDateFormat(mCalendar.getTime()); }
/** * 取得给定日期加上一定天数后的日期对象. * * @param //date 给定的日期对象 * @param amount 需要添加的天数,如果是向前的天数,使用负数就可以. * @return Date 加上一定天数以后的Date对象. */ public static String getCalcDateFormat(String sDate, int amount) { Date date = getCalcDate(getDateByDateFormat(sDate), amount);
return getDateFormat(date); }
/** * 取得给定日期加上一定天数后的日期对象. * * @param date 给定的日期对象 * @param amount 需要添加的天数,如果是向前的天数,使用负数就可以. * @return Date 加上一定天数以后的Date对象. */ public static Date getCalcDate(Date date, int amount) { Calendar cal = Calendar.getInstance(); cal.setTime(date); cal.add(Calendar.DATE, amount);
return cal.getTime(); }
基本都是代码,不是我懒(就是懒你咬我),主要是方便新手司机的copy好吧。好了,本篇随笔就到这里。
推荐↓↓↓
长
按
关
注
?【16个技术公众号】都在这里!
涵盖:程序员大咖、源码共读、程序员共读、数据结构与算法、黑客技术和网络安全、大数据科技、编程前端、Java、Python、Web编程开发、Android、iOS开发、Linux、数据库研发、幽默程序员等。
万水千山总是情,点个 “ 好看” 行不行