一些常用函数
来源:互联网 发布:零度网络 编辑:程序博客网 时间:2024/05/29 19:02
public static void makeDir(String path) { File file = new File(path); if (!file.exists() && !file.mkdirs()) { log.e("mkdirs failed"); } } public static boolean isPathExsit(String path) { File file = new File(path); return file.exists(); } /** * 删除文件 * * @param filePath * 文件目录 */ public static boolean deleteFile(String filePath) { if (isStringEmpty(filePath)) { log.d("deleteFile | filePath is empty"); return false; } File file = new File(filePath); return file.delete(); } /** * 获得时间格式化字符串 * * @param date * 要格式化的时间 * @return 格式化后时间字符串(YYYY-MM-DD),异常则返回空字符串 */ public static String getFormatTime(Date date, String formatter) { if (null == formatter || null == date) { log.e("param is null"); return ""; } SimpleDateFormat sdf = new SimpleDateFormat(formatter, Locale.CHINA); sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); return sdf.format(date); } /** * 取得两个时间的跨度 * * @param beginTime * @param endTime * @return */ public static long timeLong(String beginTime, String endTime) { long recordTime = 0; try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); Date beginDate = sdf.parse(beginTime); Date endDate = sdf.parse(endTime); long recordSeconds = Math.abs(endDate.getTime() - beginDate.getTime()); recordTime = recordSeconds / 1000; } catch (ParseException e) { log.e("timeLong | " + e.getMessage()); } return recordTime; } /** * 取得UTC时间 * * @param beginTime * @param duration * @return */ /* * public static String getUTCTime(String beginTime, int duration) { long * utcMillis = transTimeToMills(beginTime) + duration * 1000; * * SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmmss", * Locale.CHINA); * * String utcTime = sdf.format(new Date(utcMillis)).concat("Z"); utcTime = * utcTime.replace(" ", "T"); * * return utcTime; } */ /** * 将字符串数据转化为毫秒数 * * @param beginTime * @param duration * @return */ public static long transTimeToMills(String beginTime) { long millis = 0; Calendar c = Calendar.getInstance(); try { c.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) .parse(beginTime)); millis = c.getTimeInMillis(); } catch (ParseException e) { millis = 0; log.e("transTimeToMills | Parse error"); } return millis; } /** * 获取文件大小 * * @param url * @return */ public static long getFileSize(String url) { if (isStringEmpty(url)) { return 0; } File file = new File(url); if (file.isDirectory()) { return 0; } return file.length(); } /** * * 获得count月前的第一天 * * @param endtime * @param count * @return */ public static String getPreMonFirstDay(String endtime, int count) { String beginTime = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); try { Date date = sdf.parse(endtime); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH);// int day = calendar.get(Calendar.DAY_OF_MONTH);// int hour = calendar.get(Calendar.HOUR_OF_DAY);// int minute = calendar.get(Calendar.MINUTE);// int second = calendar.get(Calendar.SECOND); int beginYear = 0; int beginMonth = 0; int beginDay = 1; int beginHour = 0; int beginMinute = 0; int beginSecond = 0; if (month > count) { beginYear = year; beginMonth = month - count; } else { beginYear = year - 1; beginMonth = month + 12 - count; } calendar.set(beginYear, beginMonth, beginDay, beginHour, beginMinute, beginSecond); beginTime = sdf.format(calendar.getTime()); } catch (ParseException e) { e.printStackTrace(); } return beginTime; } /** * * 获得本月的最后一天 * * @param endtime * @param count * @return */ public static String getCurMonLastDay(String time) { String beginTime = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA); try { Date date = sdf.parse(time); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH);// int day = calendar.get(Calendar.DAY_OF_MONTH);// int hour = calendar.get(Calendar.HOUR_OF_DAY);// int minute = calendar.get(Calendar.MINUTE);// int second = calendar.get(Calendar.SECOND); int lastDay = getMaxDay(year - 1900, month + 1); int endYear = year; int endMonth = month; int endDay = lastDay; int endHour = 23; int endMinute = 59; int beginSecond = 59; calendar.set(endYear, endMonth, endDay, endHour, endMinute, beginSecond); beginTime = sdf.format(calendar.getTime()); } catch (ParseException e) { e.printStackTrace(); } return beginTime; } /** * * */ /** * * 判断闰年还是平年 * * @param year * @return */ public static boolean isLeapYear(int year) { return (year % 400 == 0 || (year % 4 == 0) && (year % 100 != 0)); } /** * * 获得某年某月的最大天数 * * @param year * @param month * @return */ public static int getMaxDay(int year, int month) { int day = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = 31; break; case 4: case 6: case 9: case 11: day = 30; break; case 2: if (isLeapYear(year)) { day = 29; } else { day = 28; } break; default: break; } return day; } /** * * 按照flag参数来截取字符串得到时间 * * @param time yyyy-MM-dd HH:mm:ss格式时间 * @param flag day,month,year * @return */ public static String interceptionString(String time,String flag) { if(flag.equals("day")) { time = time.substring(0,10); } else if(flag.equals("month")) { time = time.substring(0,7); } else if(flag.equals("year")) { time = time.substring(0,4); } return time; }