Last active
August 29, 2015 14:18
-
-
Save kurorido/8ed32b89b34258c4abf6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 日期操作 | |
| # 取得今天日期格式 | |
| public static String getDateString(int dateOffset) { | |
| Calendar cal = Calendar.getInstance(); | |
| cal.add(Calendar.DATE, dateOffset); | |
| SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); | |
| String s = format1.format(cal.getTime()); | |
| s += "(" + dayMapping[cal.get(Calendar.DAY_OF_WEEK) - 1] + ")"; | |
| return s; | |
| } | |
| # 現在的 timestamp | |
| public static Timestamp currentTimeStamp() { | |
| java.util.Date date= new java.util.Date(); | |
| return new Timestamp(date.getTime()); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Get URL Content to String | |
| public static String getText(String url) throws Exception { | |
| URL website = new URL(url); | |
| URLConnection connection = website.openConnection(); | |
| BufferedReader in = new BufferedReader( | |
| new InputStreamReader( | |
| connection.getInputStream())); | |
| StringBuilder response = new StringBuilder(); | |
| String inputLine; | |
| while ((inputLine = in.readLine()) != null) | |
| response.append(inputLine); | |
| in.close(); | |
| return response.toString(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Program Running Time | |
| # 計算執行時間 | |
| long startTime = System.currentTimeMillis(); | |
| .....your program.... | |
| long endTime = System.currentTimeMillis(); | |
| long totalTime = endTime - startTime; | |
| System.out.println(totalTime); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # MySQL 連線 | |
| String driver = "com.mysql.jdbc.Driver"; | |
| Class.forName(driver); | |
| String conUrl = "jdbc:mysql://localhost/smft?user=root&password=&charset=utf8&characterEncoding=UTF-8r"; | |
| Connection con = null; | |
| PreparedStatement ps = conn.prepareStatement(SQL); | |
| ResultSet rs = pstmt.executeQuery(); | |
| while (rs.next()) { | |
| rs.getString("UserName"); | |
| } | |
| # MS SQL 連線 | |
| String conUrl = "jdbc:sqlserver://10.57.35.33:1433;databaseName=SmftService;user=SmftDeveloper;password=SmftDeveloper"; | |
| Connection con = DriverManager.getConnection(conUrl); | |
| # URL Download 和 NIO 寫檔 | |
| String fileName = ParserUtils.parseFileName(logFilePath); | |
| System.out.println("Downloading " + fileName); | |
| URL website = new URL(Settings.FILE_WEBSITE_URL + logFilePath); | |
| ReadableByteChannel rbc = Channels.newChannel(website.openStream()); | |
| String filePath = logDirectory.getPath() + "/" + fileName; | |
| File file = new File(filePath); | |
| if(!file.exists()) { | |
| file.createNewFile(); | |
| } | |
| FileOutputStream fos = new FileOutputStream(file, false); | |
| fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); | |
| fos.close(); | |
| # Date 轉 Timestamp 字串 | |
| DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |
| Date date = new Date(); | |
| String timestamp = dateFormat.format(date); | |
| # 從整串檔案路徑中取得 Filename | |
| public static String parseFileName(String path) { | |
| int ind = path.lastIndexOf("/"); | |
| return path.substring(ind+1, path.length()); | |
| } | |
| # POI 基本操作 | |
| XSSFWorkbook workbook = new XSSFWorkbook(); | |
| XSSFSheet spreadsheet = workbook.createSheet(name); | |
| spreadsheet.createRow(0).createCell(0).setCellValue("PID"); | |
| String startColumn = CellReference.convertNumToColString(1); | |
| String endColumn = CellReference.convertNumToColString(2); | |
| CellRangeAddress region = CellRangeAddress.valueOf(startColumn + "1:" + endColumn + "1"); | |
| spreadsheet.addMergedRegion(region); | |
| spreadsheet.getRow(0).createCell(CellReference.convertColStringToIndex(startColumn)).setCellValue(pid); | |
| FileOutputStream out = new FileOutputStream(new File("Writesheet.xlsx | |
| # String Array Join | |
| public static String combine(String[] s, String glue) { | |
| int k = s.length; | |
| if (k == 0) { | |
| return null; | |
| } | |
| StringBuilder out = new StringBuilder(); | |
| out.append(s[0]); | |
| for (int x = 1; x < k; ++x) { | |
| out.append(glue).append(s[x]); | |
| } | |
| return out.toString(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| List<String> imei = new ArrayList<String>(); | |
| public static String SQL_LOG_URL_QUERY = "SELECT * FROM log WHERE imei IN (%s)"; | |
| String querySQL = String.format(Settings.SQL_LOG_URL_QUERY, SQLUtils.preparePlaceHolders(imei.size())); | |
| stmt = conn.prepareStatement(querySQL); | |
| SQLUtils.setPlaceHoldersValues(stmt, imei.toArray()); | |
| public class SQLUtils { | |
| public static String preparePlaceHolders(int length) { | |
| StringBuilder builder = new StringBuilder(); | |
| for (int i = 0; i < length;) { | |
| builder.append("?"); | |
| if (++i < length) { | |
| builder.append(","); | |
| } | |
| } | |
| return builder.toString(); | |
| } | |
| public static void setPlaceHoldersValues(PreparedStatement preparedStatement, Object... values) throws SQLException { | |
| for (int i = 0; i < values.length; i++) { | |
| preparedStatement.setObject(i + 1, values[i]); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Properties properties = System.getProperties(); | |
| properties.setProperty("mail.smtp.host", host); | |
| Session session = Session.getDefaultInstance(properties); | |
| // Create a default MimeMessage object. | |
| MimeMessage message = new MimeMessage(session | |
| message.addRecipient(Message.RecipientType.TO, new InternetAddress("Hello@example.com")); | |
| StringBuffer sb = new StringBuffer(); | |
| sb.append("<HTML>\n"); | |
| sb.append("<BODY>\n"); | |
| sb.append(summary); | |
| sb.append("</BODY>\n"); | |
| sb.append("</HTML>\n"); | |
| message.setDataHandler(new DataHandler(new ByteArrayDataSource(sb.toString(), "text/html"))); | |
| // Send message | |
| Transport.send(message); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| BufferedReader reader = null; | |
| OutputStreamWriter writer = null; | |
| String inFileName = "filename"; | |
| try { | |
| File inFile = new File(inFileName); | |
| InputStream is = new FileInputStream(inFile); | |
| reader = new BufferedReader(new InputStreamReader(is)); | |
| File outFile = new File(inFileName + ".out"); | |
| OutputStream os = new FileOutputStream(outFile); | |
| writer = new OutputStreamWriter(os); | |
| String str = ""; | |
| while((str = reader.readLine()) != null) { | |
| } | |
| reader.close(); | |
| writer.close(); | |
| } catch (FileNotFoundException e) { | |
| e.printStackTrace(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| if(reader != null) { | |
| try { | |
| reader.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| if(writer != null) { | |
| try { | |
| writer.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment