app第二阶段冲刺第八天
作者:@kuaiquxie
作者的github:https://github.com/bitebita
本文为作者原创,如需转载,请注明出处:https://www.cnblogs.com/dzwj/p/16254865.html
今天写的是关于数据处理,数据格式相关的内容
package com.linlang.grasharepro.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class DateUtil { private static String datePattern = "yyyyMMdd"; private static String timePattern = datePattern + "HHmmss"; public static String[] STR_YEAR = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; public static String[] WEEK_ZH = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" }; public static String[] MONTH_ZH = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" }; public static String[] MONTH_UP = { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" }; public static String[] MONTH_LO = { "Janury", "February", "March", "April", "May", "June", "July", "Auguest", "September", "October", "Novenber", "December" }; public static String[] MON_UP = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC" }; public static String[] MON_LO = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; // ~ Methods // ================================================================ /** * Return default datePattern (MMddyyyy) * * @return a string representing the date pattern on the UI */ public static String getDatePattern() { return datePattern; } /** * This method attempts to convert an Oracle-formatted date in the form * dd-MMM-yyyy to mm/dd/yyyy. * * @param aDate * date from database as a string * @return formatted string for the ui */ public static final String getDate(Date aDate) { SimpleDateFormat df = null; String returnValue = ""; if (aDate != null) { df = new SimpleDateFormat(datePattern); returnValue = df.format(aDate); } return (returnValue); } /** * This method generates a string representation of a date/time in the * format you specify on input * * @param aMask * the date pattern the string is in * @param strDate * a string representation of a date * @return a converted Date object * @see SimpleDateFormat * @throws ParseException */ public static final Date convertStringToDate(String aMask, String strDate) throws ParseException { SimpleDateFormat df = null; Date date = null; df = new SimpleDateFormat(aMask); try { date = df.parse(strDate); } catch (ParseException pe) { // log.error("ParseException: " + pe); throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return (date); } /** * This method returns the current date time in the format: MM/dd/yyyy HH:MM * a * * @param theTime * the current time * @return the current date/time */ public static String getTimeNow(Date theTime) { return getDateTime(timePattern, theTime); } /** * This method returns the current date in the format: MM/dd/yyyy * * @return the current date * @throws ParseException */ public static Calendar getToday() throws ParseException { Date today = new Date(); SimpleDateFormat df = new SimpleDateFormat(datePattern); // This seems like quite a hack (date -> string -> date), // but it works ;-) String todayAsString = df.format(today); Calendar cal = new GregorianCalendar(); cal.setTime(convertStringToDate(todayAsString)); return cal; } /** * This method generates a string representation of a date's date/time in * the format you specify on input * * @param aMask * the date pattern the string is in * @param aDate * a date object * @return a formatted string representation of the date * @see SimpleDateFormat */ public static final String getDateTime(String aMask, Date aDate) { SimpleDateFormat df = null; String returnValue = ""; if (aDate != null) { df = new SimpleDateFormat(aMask); returnValue = df.format(aDate); } return returnValue; } /** * This method generates a string representation of a date based on the * System Property 'dateFormat' in the format you specify on input * * @param aDate * A date to convert * @return a string representation of the date */ public static final String convertDateToString(Date aDate) { return getDateTime(datePattern, aDate); } /** * This method converts a String to a date using the datePattern * * @param strDate * the date to convert (in format MM/dd/yyyy) * @return a date object * @throws ParseException */ public static Date convertStringToDate(String strDate) throws ParseException { Date aDate = null; try { aDate = convertStringToDate(datePattern, strDate); } catch (ParseException pe) { pe.printStackTrace(); throw new ParseException(pe.getMessage(), pe.getErrorOffset()); } return aDate; } private static String divisionChars = "- / , . ; :"; private static String intToString(int val, double len) { Double dmask = new Double(Math.pow(10, len)); int mask = dmask.intValue(); return String.valueOf(val + mask).substring(1); } private static String intToString(int val, int len) { Integer ilen = new Integer(len); return intToString(val, ilen.doubleValue()); } private static StringTokenizer filterDivision(String str) { StringTokenizer divisionToken; StringTokenizer strToken; divisionToken = new StringTokenizer(divisionChars, " ", false); while (divisionToken.hasMoreTokens()) { strToken = new StringTokenizer(str, divisionToken.nextToken(), false); str = ""; while (strToken.hasMoreTokens()) { str += strToken.nextToken(); str += " "; } } // Debug.out ("The string is:" + str); strToken = new StringTokenizer(str, " ", false); String temp = ""; if (strToken.countTokens() == 1) { if (str.trim().length() == 8) { temp = str.substring(0, 4) + " " + str.substring(4, 6) + " " + str.substring(6, 8); str = temp; } else if (str.trim().length() == 7) { if ("0".equals(str.substring(4, 5))) { temp = str.substring(0, 4) + " " + str.substring(4, 6) + " " + str.substring(6, 7); } else { temp = str.substring(0, 4) + " " + str.substring(4, 5) + " " + str.substring(5, 7); } str = temp; } else if (str.trim().length() == 6) { temp = str.substring(0, 4) + " " + str.substring(4, 5) + " " + str.substring(5, 6); str = temp; } } strToken = new StringTokenizer(str, " ", false); return strToken; /* * str = ""; while (strToken.hasMoreTokens ()) { str += * strToken.nextToken(); str += " "; } return str; */ } private static HashMap parseInput(StringTokenizer strToken, StringTokenizer fmtToken) throws Exception { if (strToken.countTokens() != fmtToken.countTokens()){ throw new Exception("Not match value string and format string!"); } HashMap hm = new HashMap(); while (fmtToken.hasMoreTokens()) { hm.put(fmtToken.nextToken(), strToken.nextToken()); } return hm; } private static Calendar parseYear(Calendar cal, HashMap hm, Locale lo) throws Exception { int year; if (hm.get("YYYY") != null) { try { year = Integer.parseInt((String) hm.get("YYYY")); // Debug.out("Year :" + String.valueOf(year)); cal.set(Calendar.YEAR, year); } catch (NumberFormatException nfe) { throw new Exception("The year string is not valid!"); } return cal; } if (hm.get("YY") != null) { try { year = Integer.parseInt((String) hm.get("YY")); if (year < 50){ year += 2000; } else{ year += 1900; } // Debug.out("Year :" + String.valueOf(year)); cal.set(Calendar.YEAR, year); } catch (NumberFormatException nfe) { throw new Exception("The year string is not valid!"); } return cal; } return cal; } private static Calendar parseMonth(Calendar cal, HashMap hm, Locale lo) throws Exception { int month = 0; if (hm.get("MM") != null) { try { month = Integer.parseInt((String) hm.get("MM")) - 1; // Debug.out("Month :" + String.valueOf(month)); cal.set(Calendar.MONTH, month); } catch (NumberFormatException nfe) { throw new Exception("The month string is not valid!"); } return cal; } else if (hm.get("Mon") != null || hm.get("MON") != null) { try { String mon = ((String) hm.get("Mon")).toUpperCase(); for (int i = 0, l = MON_UP.length; i < l; i++) { if (mon.equals(MON_UP[i])) { month = i; } } // Debug.out("Month :" + String.valueOf(month)); cal.set(Calendar.MONTH, month); } catch (NumberFormatException nfe) { throw new Exception("The month string is not valid!"); } return cal; } return cal; } private static Calendar parseDate(Calendar cal, HashMap hm, Locale lo) throws Exception { int date; if (hm.get("DD") != null) { try { date = Integer.parseInt((String) hm.get("DD")); // Debug.out("Date :" + String.valueOf(date)); cal.set(Calendar.DATE, date); } catch (NumberFormatException nfe) { throw new Exception("The date string is not valid!"); } return cal; } return cal; } private static Calendar parseHour(Calendar cal, HashMap hm, Locale lo) throws Exception { int hour; if (hm.get("HH24") != null) { try { hour = Integer.parseInt((String) hm.get("HH24")); // Debug.out("Hour :" + String.valueOf(hour)); cal.set(Calendar.HOUR_OF_DAY, hour); } catch (NumberFormatException nfe) { throw new Exception("The hour string is not valid!"); } return cal; } if (hm.get("HH") != null) { try { hour = Integer.parseInt((String) hm.get("HH")); // Debug.out("Hour :" + String.valueOf(hour)); cal.set(Calendar.HOUR, hour); } catch (NumberFormatException nfe) { throw new Exception("The hour string is not valid!"); } return cal; } return cal; } private static Calendar parseMinute(Calendar cal, HashMap hm, Locale lo) throws Exception { int minute; if (hm.get("MI") != null) { try { minute = Integer.parseInt((String) hm.get("MI")); // Debug.out("Minute :" + String.valueOf(minute)); cal.set(Calendar.MINUTE, minute); } catch (NumberFormatException nfe) { throw new Exception("The minute string is not valid!"); } return cal; } return cal; } private static Calendar parseSecond(Calendar cal, HashMap hm, Locale lo) throws Exception { int second; if (hm.get("SS") != null) { try { second = Integer.parseInt((String) hm.get("SS")); // Debug.out("Second :" + String.valueOf(second)); cal.set(Calendar.SECOND, second); } catch (NumberFormatException nfe) { throw new Exception("The second string is not valid!"); } return cal; } return cal; } private static Calendar parseMeridian(Calendar cal, HashMap hm, Locale lo) throws Exception { int meridian = 0; String meridianStr; if (hm.get("AM") != null || hm.get("A.M.") != null || hm.get("PM") != null || hm.get("P.M.") != null) { meridianStr = (String) hm.get("AM"); if (meridianStr == null){ meridianStr = (String) hm.get("A.M."); } if (meridianStr == null){ meridianStr = (String) hm.get("PM"); } if (meridianStr == null){ meridianStr = (String) hm.get("P.M."); } switch (lo.hashCode()) { // US case 1591: if ("PM".equals(meridianStr) || "P.M.".equals(meridianStr)){ meridian = 1; } else if (!"AM".equals(meridianStr) && !"A.M.".equals(meridianStr)){ throw new Exception("The meridian string is wrong!"); } break; // SIMPLIFIED_CHINESE case 1861: if ("下午".equals(meridianStr)){ meridian = 1; } else if (!"上午".equals(meridianStr)){ throw new Exception("The meridian string is wrong!"); } break; default: if ("PM".equals(meridianStr) || "P.M.".equals(meridianStr)){ meridian = 1; } else if (!"AM".equals(meridianStr) && !"A.M.".equals(meridianStr)){ throw new Exception("The meridian string is wrong!"); } break; } // Debug.out("Meridian String :" + meridianStr); // Debug.out("Meridian :" + String.valueOf(meridian)); cal.set(Calendar.AM_PM, meridian); return cal; } return cal; } public static Date stringToDate(String str, String fmt) throws Exception { return stringToCalendar(str, fmt).getTime(); } public static Date stringToDate(String str, String fmt, Locale lo) throws Exception { return stringToCalendar(str, fmt, lo).getTime(); } public static Calendar stringToCalendar(String str, String fmt) throws Exception { return stringToCalendar(str, fmt, Locale.US); } public static Calendar stringToCalendar(String str, String fmt, Locale lo) throws Exception { // Debug.out ("Locale Page is set to :" + // String.valueOf(lo.hashCode())); Calendar cal = Calendar.getInstance(); HashMap hm = parseInput(filterDivision(str), filterDivision(fmt)); cal = parseYear(cal, hm, lo); cal = parseMonth(cal, hm, lo); cal = parseDate(cal, hm, lo); cal = parseHour(cal, hm, lo); cal = parseMinute(cal, hm, lo); cal = parseSecond(cal, hm, lo); cal = parseMeridian(cal, hm, lo); return cal; } private static String parseYear(Calendar cal, String dtStr, Locale lo) throws Exception { StringBuffer strBuf; try { while (dtStr.lastIndexOf("YYYY") > -1) { strBuf = new StringBuffer(dtStr); strBuf.replace(dtStr.lastIndexOf("YYYY"), dtStr .lastIndexOf("YYYY") + 4, String.valueOf(cal .get(Calendar.YEAR))); dtStr = strBuf.toString(); } while (dtStr.lastIndexOf("YY") > -1) { strBuf = new StringBuffer(dtStr); strBuf.replace(dtStr.lastIndexOf("YY"), dtStr.lastIndexOf("YY") + 2, String.valueOf( cal.get(Calendar.YEAR)).substring(2)); dtStr = strBuf.toString(); } return dtStr; } catch (StringIndexOutOfBoundsException ex) { throw new Exception("Parse Year Error!"); } } private static String parseMonth(Calendar cal, String dtStr, Locale lo) throws Exception { StringBuffer strBuf; try { while (dtStr.lastIndexOf("MM") > -1) { strBuf = new StringBuffer(dtStr); strBuf.replace(dtStr.lastIndexOf("MM"), dtStr.lastIndexOf("MM") + 2, intToString(cal .get(Calendar.MONTH) + 1, 2)); dtStr = strBuf.toString(); } while (dtStr.lastIndexOf("Mon") > -1) { strBuf = new StringBuffer(dtStr); switch (lo.hashCode()) { // US case 1591: strBuf.replace(dtStr.lastIndexOf("Mon"), dtStr .lastIndexOf("Mon") + 3, MON_LO[cal .get(Calendar.MONTH)]); break; // SIMPLIFIED_CHINESE case 1861: strBuf.replace(dtStr.lastIndexOf("Mon"), dtStr .lastIndexOf("Mon") + 3, MONTH_ZH[cal .get(Calendar.MONTH)]); break; default: strBuf.replace(dtStr.lastIndexOf("Mon"), dtStr .lastIndexOf("Mon") + 3, MON_LO[cal .get(Calendar.MONTH)]); } dtStr = strBuf.toString(); } return dtStr; } catch (StringIndexOutOfBoundsException ex) { throw new Exception("Parse Month Error!"); } } private static String parseDate(Calendar cal, String dtStr, Locale lo) throws Exception { StringBuffer strBuf; try { while (dtStr.lastIndexOf("DD") > -1) { strBuf = new StringBuffer(dtStr); strBuf.replace(dtStr.lastIndexOf("DD"), dtStr.lastIndexOf("DD") + 2, intToString(cal .get(Calendar.DATE), 2)); dtStr = strBuf.toString(); } return dtStr; } catch (StringIndexOutOfBoundsException ex) { throw new Exception("Parse Date Error!"); } } private static String parseHour(Calendar cal, String dtStr, Locale lo) throws Exception { StringBuffer strBuf; try { while (dtStr.lastIndexOf("HH24") > -1) { strBuf = new StringBuffer(dtStr); strBuf.replace(dtStr.lastIndexOf("HH24"), dtStr .lastIndexOf("HH24") + 4, intToString(cal .get(Calendar.HOUR_OF_DAY), 2)); dtStr = strBuf.toString(); } while (dtStr.lastIndexOf("HH") > -1) { strBuf = new StringBuffer(dtStr); strBuf.replace(dtStr.lastIndexOf("HH"), dtStr.lastIndexOf("HH") + 2, intToString(cal .get(Calendar.HOUR), 2)); dtStr = strBuf.toString(); } return dtStr; } catch (StringIndexOutOfBoundsException ex) { throw new Exception("Parse Hour Error!"); } } private static String parseMinute(Calendar cal, String dtStr, Locale lo) throws Exception { StringBuffer strBuf; try { while (dtStr.lastIndexOf("MI") > -1) { strBuf = new StringBuffer(dtStr); strBuf.replace(dtStr.lastIndexOf("MI"), dtStr.lastIndexOf("MI") + 2, intToString(cal .get(Calendar.MINUTE), 2)); dtStr = strBuf.toString(); } return dtStr; } catch (StringIndexOutOfBoundsException ex) { throw new Exception("Parse Minute Error!"); } } private static String parseSecond(Calendar cal, String dtStr, Locale lo) throws Exception { StringBuffer strBuf; try { while (dtStr.lastIndexOf("SS") > -1) { strBuf = new StringBuffer(dtStr); strBuf.replace(dtStr.lastIndexOf("SS"), dtStr.lastIndexOf("SS") + 2, intToString(cal .get(Calendar.SECOND), 2)); dtStr = strBuf.toString(); } return dtStr; } catch (StringIndexOutOfBoundsException ex) { throw new Exception("Parse Second Error!"); } } private static String parseMeridian(Calendar cal, String dtStr, Locale lo) throws Exception { StringBuffer strBuf; int beginIndex; int endIndex; try { while (dtStr.lastIndexOf("AM") > -1 || dtStr.lastIndexOf("PM") > -1) { if (dtStr.lastIndexOf("AM") > -1) { beginIndex = dtStr.lastIndexOf("AM"); endIndex = beginIndex + 2; } else { beginIndex = dtStr.lastIndexOf("PM"); endIndex = beginIndex + 2; } strBuf = new StringBuffer(dtStr); // Debug.out("Parse AM begin:"+ dtStr); switch (lo.hashCode()) { // US case 1591: strBuf.replace(beginIndex, endIndex, cal .get(Calendar.AM_PM) == 0 ? "=%A=%M" : "=%P=%M"); break; // SIMPLIFIED_CHINESE case 1861: strBuf.replace(beginIndex, endIndex, cal .get(Calendar.AM_PM) == 0 ? "=%上=%午" : "=%下=%午"); break; default: strBuf.replace(beginIndex, endIndex, cal .get(Calendar.AM_PM) == 0 ? "=%A=%M" : "=%P=%M"); } dtStr = strBuf.toString(); // Debug.out("Parse AM end:"+ dtStr); } while (dtStr.lastIndexOf("A.M.") > -1 || dtStr.lastIndexOf("P.M.") > -1) { strBuf = new StringBuffer(dtStr); if (dtStr.lastIndexOf("A.M.") > -1) { beginIndex = dtStr.lastIndexOf("A.M."); endIndex = beginIndex + 4; } else { beginIndex = dtStr.lastIndexOf("P.M."); endIndex = beginIndex + 4; } switch (lo.hashCode()) { // US case 1591: strBuf.replace(beginIndex, endIndex, cal .get(Calendar.AM_PM) == 0 ? "=%A=%.M=%." : "=%P=%.=%M=%."); break; // SIMPLIFIED_CHINESE case 1861: strBuf.replace(beginIndex, endIndex, cal .get(Calendar.AM_PM) == 0 ? "=%上=%午" : "=%下=%午"); break; default: strBuf.replace(beginIndex, endIndex, cal .get(Calendar.AM_PM) == 0 ? "=%A=%.M=%." : "=%P=%.=%M=%."); } dtStr = strBuf.toString(); } while (dtStr.lastIndexOf("=%") > -1) { strBuf = new StringBuffer(dtStr); strBuf.delete(dtStr.lastIndexOf("=%"), dtStr.lastIndexOf("=%") + 2); dtStr = strBuf.toString(); } return dtStr; } catch (StringIndexOutOfBoundsException ex) { throw new Exception("Parse Date Error!"); } } public static String dateToString(String fmt) throws Exception { return dateToString(fmt, Locale.US); } public static String dateToString(String fmt, Locale lo) throws Exception { Calendar cal = Calendar.getInstance(); return calendarToString(cal, fmt, lo); } public static String dateToString(Date dt, String fmt) throws Exception { return dateToString(dt, fmt, Locale.US); } public static String dateToString(Date dt, String fmt, Locale lo) throws Exception { Calendar cal = Calendar.getInstance(); cal.setTime(dt); return calendarToString(cal, fmt, lo); } public static String calendarToString(Calendar cal, String fmt) throws Exception { return calendarToString(cal, fmt, Locale.US); } public static String calendarToString(Calendar cal, String fmt, Locale lo) throws Exception { String dtStr = fmt; // // Debug.out ("Locale Page is set to :" + // String.valueOf(lo.hashCode())); dtStr = parseYear(cal, dtStr, lo); dtStr = parseMonth(cal, dtStr, lo); dtStr = parseDate(cal, dtStr, lo); dtStr = parseHour(cal, dtStr, lo); dtStr = parseMinute(cal, dtStr, lo); dtStr = parseSecond(cal, dtStr, lo); dtStr = parseMeridian(cal, dtStr, lo); return dtStr; } public static Date getDate(int year, int month, int date) { return getDate(year, month, date, 0, 0, 0); } public static Date getDate(int year, int month, int date, int hour, int minute) { return getDate(year, month, date, hour, minute, 0); } public static Date getDate(int year, int month, int date, int hour, int minute, int second) { Calendar cal = Calendar.getInstance(); cal.set(year, month, date, hour, minute, second); return cal.getTime(); } public static Date getDate(String year, String month, String date) throws Exception { return getDate(year, month, date, "0", "0", "0"); } public static Date getDate(String year, String month, String date, String hour, String minute) throws Exception { return getDate(year, month, date, hour, minute, "0"); } public static Date getDate(String year, String month, String date, String hour, String minute, String second) throws Exception { Calendar cal = Calendar.getInstance(); try { cal.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(date), Integer.parseInt(hour), Integer .parseInt(minute), Integer.parseInt(second)); return cal.getTime(); } catch (NumberFormatException ne) { throw new Exception("Can not convert string to valid int!"); } } public static Date beginOfDate(Date dt) { int hour = 0; int minute = 0; int second = 0; Calendar cal = Calendar.getInstance(); cal.setTime(dt); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, second); return cal.getTime(); } public static Date endOfDate(Date dt) { int hour = 23; int minute = 59; int second = 59; Calendar cal = Calendar.getInstance(); cal.setTime(dt); cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, second); return cal.getTime(); } // Add millisecond public static Date addMillisecond(Date dt, int millisecond) { return addSecond(dt, (long) millisecond); } public static Date addMillisecond(Date dt, long millisecond) { dt.setTime(dt.getTime() + millisecond); return dt; } // add second public static Date addSecond(Date dt, int second) { return addSecond(dt, (long) second); } public static Date addSecond(Date dt, float second) { return addSecond(dt, (double) second); } public static Date addSecond(Date dt, long second) { return addMillisecond(dt, 1000L * second); } public static Date addSecond(Date dt, double second) { Double millisecond = new Double(1000.0 * second); return addMillisecond(dt, millisecond.longValue()); } // add minute public static Date addMinute(Date dt, int minute) { return addMinute(dt, (long) minute); } public static Date addMinute(Date dt, float minute) { return addMinute(dt, (double) minute); } public static Date addMinute(Date dt, long minute) { return addMillisecond(dt, 1000L * 60L * minute); } public static Date addMinute(Date dt, double minute) { Double millisecond = new Double(1000.0 * 60.0 * minute); return addMillisecond(dt, millisecond.longValue()); } // add hour public static Date addHour(Date dt, int hour) { return addHour(dt, (long) hour); } public static Date addHour(Date dt, float hour) { return addHour(dt, (double) hour); } public static Date addHour(Date dt, long hour) { return addMillisecond(dt, 1000L * 60L * 60L * hour); } public static Date addHour(Date dt, double hour) { Double millisecond = new Double(1000.0 * 60.0 * 60.0 * hour); return addMillisecond(dt, millisecond.longValue()); } // add day public static Date addDay(Date dt, int day) { return addDay(dt, (long) day); } public static Date addDay(Date dt, float day) { return addDay(dt, (double) day); } public static Date addDay(Date dt, long day) { return addMillisecond(dt, 1000L * 60L * 60L * 24L * day); } public static Date addDay(Date dt, double day) { Double millisecond = new Double(1000.0 * 60.0 * 60.0 * 24.0 * day); return addMillisecond(dt, millisecond.longValue()); } // add month public static Date addMonth(Date dt, int month) { Calendar cal = Calendar.getInstance(); cal.setTime(dt); cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + month); return cal.getTime(); } public static Date addMonth(Date dt, float month) { return addMonth(dt, (double) month); } public static Date addMonth(Date dt, long month) { return addMonth(dt, (new Long(month)).intValue()); } public static Date addMonth(Date dt, double month) { double floorMonth = Math.floor(month); double decimalMonth = month - floorMonth; dt = addMonth(dt, (new Double(floorMonth)).intValue()); Calendar cal = Calendar.getInstance(); cal.setTime(dt); cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); Date nextdt = cal.getTime(); long monthMillisecond = nextdt.getTime() - dt.getTime(); double millisecond = (double) monthMillisecond * decimalMonth; return addMillisecond(dt, (long) millisecond); } // add year public static Date addYear(Date dt, int year) { Calendar cal = Calendar.getInstance(); cal.setTime(dt); cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + year); return cal.getTime(); } public static Date addYear(Date dt, float year) { return addYear(dt, (double) year); } public static Date addYear(Date dt, long year) { return addYear(dt, (new Long(year)).intValue()); } public static Date addYear(Date dt, double year) { double floorYear = Math.floor(year); double decimalYear = year - floorYear; dt = addYear(dt, (new Double(floorYear)).intValue()); Calendar cal = Calendar.getInstance(); cal.setTime(dt); cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1); Date nextdt = cal.getTime(); long yearMillisecond = nextdt.getTime() - dt.getTime(); double millisecond = (double) yearMillisecond * decimalYear; return addSecond(dt, (long) millisecond); } public static String getStrYear(int curYear) { if (String.valueOf(curYear) == null){ return null; } if (curYear - 2000 < 0 || curYear - 2000 > 25){ return null; } return STR_YEAR[curYear - 2000]; } /** * 按格式返回当前日期字符串 * * @param str * @return */ public static String getCurDate(String str) { try { return dateToString(new Date(), str); } catch (Exception e) { return null; } } /** * 按格式返回当前日期字符串 * * @param str * @return */ public static String getCurDate(Calendar calendar, String str) { try { return calendarToString(calendar, str); } catch (Exception e) { return null; } } /** * 判断是否为闰年 * * @param str * @param datePattern * @return * @throws Exception */ public static boolean isLeapYear(String str, String datePattern) throws Exception { Calendar cal = stringToCalendar(str, datePattern); GregorianCalendar gc = new GregorianCalendar(); return gc.isLeapYear(cal.get(Calendar.YEAR)); } /** * 判断是否为闰年 * * @param str * @return */ public static boolean isLeapYear(String str) throws Exception { return isLeapYear(str, datePattern); } /** * 取得一个月的最后一天 * * @param * @return */ public static int getLastDayOfMonth(int year, int month) { int day = 30; switch (month) { case 1: day = 31; break; case 2: // 判断是否是闰年 GregorianCalendar gc = new GregorianCalendar(); if (gc.isLeapYear(year)) { day = 29; } else { day = 28; } break; case 3: day = 31; break; case 4: day = 30; break; case 5: day = 31; break; case 6: day = 30; break; case 7: day = 31; break; case 8: day = 31; break; case 9: day = 30; break; case 10: day = 31; break; case 11: day = 30; break; case 12: day = 31; break; default: day = 30; break; } return day; } /** * 取得一个月的最后一天 * * @param * @return */ public static int getLastDayOfMonth(String str, String datePattern) throws Exception { Calendar cal = stringToCalendar(str, datePattern); return getLastDayOfMonth(cal.get(Calendar.YEAR), cal .get(Calendar.MONTH)); } /** * 取得一个月的最后一天 * * @param * @return */ public static int getLastDayOfMonth(String str) throws Exception { Calendar cal = stringToCalendar(str, datePattern); return getLastDayOfMonth(cal.get(Calendar.YEAR), cal .get(Calendar.MONTH) + 1); } /** * 取得上年末的会计期 * * @param * @return */ public static String getLastYearAcctPeriodNo(String acctPeriodNo) { String ret = ""; if (acctPeriodNo.equals("") || acctPeriodNo.length() != 6) { return ""; } String strYear = acctPeriodNo.substring(0, 4); String strMonth = acctPeriodNo.substring(4, 6); if (strMonth.equals("00")) { ret = String.valueOf(Integer.parseInt(strYear) - 2) + "13"; } else { ret = String.valueOf(Integer.parseInt(strYear) - 1) + "13"; } return ret; } /** * 取得当前会计期的上一会计期 * * @param acctPeriodNo * @return */ public static String getLastMonth(String acctPeriodNo) { String ret = ""; if (acctPeriodNo.equals("") || acctPeriodNo.length() != 6) { return ""; } String strYear = acctPeriodNo.substring(0, 4); String strMonth = acctPeriodNo.substring(4, 6); if (strMonth.equals("01")) { ret = String.valueOf(Integer.parseInt(strYear) - 1) + "12"; } else { int month = Integer.parseInt(strMonth) - 1; strMonth = month > 9 ? String.valueOf(month) : "0" + month; ret = strYear + strMonth; } return ret; } /** * 根据传入会计期取得传入会计期所在季度的最后一个会计期 * * @param acctPeriodNo * @return */ public static String getLastPeriodOfQuarter(String acctPeriodNo) { String ret = ""; if (acctPeriodNo.equals("") || acctPeriodNo.length() != 6) { return ""; } String strYear = acctPeriodNo.substring(0, 4); // 取得当前会计期的季度 int quarter = DateUtil.getQuarterOfMonth(acctPeriodNo); // 取得当前季度的最后一月 String month = DateUtil.getLastMonthOfQuarter(quarter); ret = strYear + month; return ret; } /** * 根据传入会计期取得传入会计期所在季度的上年最后一个会计期 * * @param acctPeriodNo * @return */ public static String getLastYearPeriodOfQuarter(String acctPeriodNo) { String ret = ""; if (acctPeriodNo.equals("") || acctPeriodNo.length() != 6) { return ""; } int year = Integer.parseInt(acctPeriodNo.substring(0, 4)) - 1; // 取得当前会计期的季度 int quarter = DateUtil.getQuarterOfMonth(acctPeriodNo); // 取得当前季度的最后一月 String month = DateUtil.getLastMonthOfQuarter(quarter); ret = String.valueOf(year) + month; return ret; } /** * 根据传入会计期取得传入会计期上季度的最后一个会计期 * * @param acctPeriodNo * @return */ public static String getLastPeriodOfLastQuarter(String acctPeriodNo) { String ret = ""; if (acctPeriodNo.equals("") || acctPeriodNo.length() != 6) { return ""; } int year = Integer.parseInt(acctPeriodNo.substring(0, 4)); // 取得当前会计期的季度 int quarter = DateUtil.getQuarterOfMonth(acctPeriodNo); if(quarter == 1){ year = year - 1; quarter = 4; }else{ quarter = quarter - 1; } // 取得当前季度的最后一月 String month = DateUtil.getLastMonthOfQuarter(quarter); ret = String.valueOf(year) + month; return ret; } /** * 取得当前会计期所在的季度 * * @param acctPeriodNo * @return */ public static int getQuarterOfMonth(String acctPeriodNo) { int ret = 1; if (acctPeriodNo.equals("") || acctPeriodNo.length() != 6) { return 1; } int month = Integer.parseInt(acctPeriodNo.substring(4, 6)); switch (month) { case 1: case 2: case 3: ret = 1; break; case 4: case 5: case 6: ret = 2; break; case 7: case 8: case 9: ret = 3; break; case 10: case 11: case 12: ret = 4; break; default: ret = 1; break; } return ret; } /** * 取得当前季度所在的最后的月份 * * @param * @return */ public static String getLastMonthOfQuarter(int quarter) { String ret = "03"; switch (quarter) { case 1: ret = "03"; break; case 2: ret = "06"; break; case 3: ret = "09"; break; case 4: ret = "12"; break; default: ret = "01"; break; } return ret; } public static int diff(Date bDate, Date eDate, boolean include) { Calendar c1 = new GregorianCalendar(); Calendar c2 = new GregorianCalendar(); c1.setTime(bDate); c2.setTime(eDate); long time = c2.getTimeInMillis() - c1.getTimeInMillis(); int diff = (int) (time / (24 * 60 * 60 * 1000)); if (include){ diff += 1; } return diff; } public static void main(String args[]) { try { /*System.out.println(DateUtil.convertStringToDate("ddMMyyyy", "30072004").toString()); Date d = DateUtil.stringToDate("20070401", "YYYYMMDD"); System.out.println(d.toString()); System.out.println("---" + DateUtil.getTimeNow(new Date())); System.out.println(DateUtil.getLastYearAcctPeriodNo("200801")); System.out.println(DateUtil.getLastMonth("200812")); System.out.println(DateUtil.getLastMonth("200801")); System.out.println(DateUtil.getLastPeriodOfLastQuarter("200801")); System.out.println(DateUtil.getLastPeriodOfLastQuarter("200810")); System.out.println(DateUtil.getLastPeriodOfQuarter("200801")); System.out.println(DateUtil.getLastYearPeriodOfQuarter("200801")); */ // Date d = DateUtil.stringToDate("20070401", "YYYYMMDD"); // // System.out.println(d.toString()); // System.out.println(""); // System.out.println(""); // Date a = DateUtil.stringToDate("20091201", "YYYYMMDD"); // Date b = DateUtil.stringToDate("20091202", "YYYYMMDD"); // int x = DateUtil.diff(a, b, true); // System.out.println(x); String time = DateUtil.dateToString("YYYY-MM-DD HH:MM:SS"); System.out.println(time); } catch (Exception e) { e.printStackTrace(); } } }
package com.linlang.grasharepro.utils; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import com.linlang.grasharepro.R; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /** * 把网络图片保存到本地 * 1.强引用,正常实例化一个对象。 * jvm无论内存是否够用系统都不会释放这块内存 * 2.软引用(softReference):当我们系统内存不够时,会释放掉 * 3.弱引用:当我们系统清理内存时发现是一个弱引用对象,直接清理掉 * 4.虚引用:当我们清理内存时会 * 把虚引用对象放入一个清理队列当中, * 让我们程序员保存当前对象的状态 * */ public class FileUtiles { private Context ctx; public FileUtiles(Context ctx){ this.ctx = ctx; } //获取手机在sdcard保存图片的地址 public String getAbsolutePath(){ File root = ctx.getExternalFilesDir(null); //返回手机端的绝对路径,当我们软件卸载,以及清理缓存时会被清理掉 if(root != null){ return root.getAbsolutePath(); } return null; } //判断图片在本地缓存当中是否存在,如果存在返回一个true public boolean isBitmap(String name){ File root = ctx.getExternalFilesDir(null); //file地址拼接 File file = new File(root,name); return file.exists(); } //添加到本地缓存当中 public void saveBitmap(String name,Bitmap bitmap){ if(bitmap == null) { return; } //如果sdcard不能使用 if(!Environment.getExternalStorageState(). equals(Environment.MEDIA_UNMOUNTED)){ return; } //拼接图片要保存到sd卡的地址 String BitPath = getAbsolutePath()+"/"+name; //mtn/sdcard/android/com.anjoyo.zhangxinyi/files/ try { FileOutputStream fos = new FileOutputStream(BitPath); /** * bitmap.compress把图片通过输出流保存到本地 * Bitmap.CompressFormat.JPEG 保存图片的格式 * 100 保存到本地的图片质量,需要压缩时适当调整大小 * * */ bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); BitmapFactory.decodeResource(Resources.getSystem(), R.drawable.account_tab_bg,options); options.inSampleSize = calculateInSampleSize(options, 100, 100);//自定义一个宽和高 options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } public static Bitmap getSmallBitmap(Resources res,int id) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, id,options); options.inSampleSize = calculateInSampleSize(options, 100, 100);//自定义一个宽和高 options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, id); } //计算图片的缩放值 public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) { final int height = options.outHeight;//获取图片的高 final int width = options.outWidth;//获取图片的框 int inSampleSize = 4; if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height/ (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize;//求出缩放值 } }
明天弄弄爬虫,之前听老师说,需要从网上爬取数据,觉得挺有意思的,明天查查资料。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」