Popular Posts
javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i...
Stats
Form tool
  1. var FormTool = {
  2.     submit: function(obj, theForm){
  3.         var f = theForm && theForm.tagName == "FORM" ? theForm : document.createElement("form");
  4.         if(obj){
  5.             var e = false;
  6.             for(var i in obj){
  7.                 if(i.toString().charAt(0) == "$"){  // form attribute
  8.                     f.setAttribute(i.toString().substring(1),obj[i]);
  9.                 }else{
  10.                     e = document.createElement("input");
  11.                     e.type = "hidden";
  12.                     e.name = i;
  13.                     e.value = obj[i];
  14.                     f.appendChild(e);
  15.                 }
  16.             }
  17.         }
  18.         if(!f.parentElement) document.body.appendChild(f);
  19.         f.submit();        
  20.     }
  21. };
  22. // ex:
  23. // FormTool.submit({$action:'/myPage.php',pageIndex:2,pageSize:10});
FileFactory
  1. package bruce.lib.io;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.FileWriter;
  10. import java.io.IOException;
  11. import java.io.OutputStreamWriter;
  12.  
  13. /**
  14.  * @author Bruce Tsai
  15.  * 
  16.  */
  17. public final class FileFactory {
  18.  
  19.     /**
  20.      * 建立目錄, 若父層目錄不存在一併建立
  21.      * 
  22.      * @param file
  23.      *            目錄路徑
  24.      */
  25.     public static void createDirectory(String file) {
  26.         createDirectory(new File(file));
  27.     }
  28.  
  29.     /**
  30.      * 建立目錄, 若父層目錄不存在一併建立
  31.      * 
  32.      * @param file
  33.      *            目錄
  34.      */
  35.     public static void createDirectory(File file) {
  36.         if (file.getParentFile() != null && !file.getParentFile().exists()) {
  37.             createDirectory(file.getParentFile());
  38.         }
  39.         if (!file.exists())
  40.             file.mkdir();
  41.     }
  42.  
  43.     /**
  44.      * 刪除檔案或目錄, 若刪除的為目錄, 一併刪除目錄下的所有檔案
  45.      * 
  46.      * @param file
  47.      *            檔案或目錄
  48.      */
  49.     public static void delete(String file) {
  50.         delete(new File(file));
  51.     }
  52.  
  53.     /**
  54.      * 刪除檔案或目錄, 若刪除的為目錄, 一併刪除目錄下的所有檔案
  55.      * 
  56.      * @param file
  57.      *            檔案或目錄
  58.      */
  59.     public static void delete(File file) {
  60.         if (file.exists()) {
  61.             if (file.isDirectory()) {
  62.                 File[] files = file.listFiles();
  63.                 for (File f : files) {
  64.                     FileFactory.delete(f);
  65.                 }
  66.             }
  67.             int limit = 10 * 1000; // 10秒
  68.             long tick = System.currentTimeMillis();
  69.             while (file.exists()) {
  70.                 // 指定時間內無法刪除時擲出例外
  71.                 if (file.delete() && System.currentTimeMillis() - tick > limit)
  72.                     throw new RuntimeException();
  73.  
  74.             }
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * 複製檔案或目錄
  80.      * 
  81.      * @param sourceFile
  82.      *            來源檔案或目錄
  83.      * @param destFile
  84.      *            目的目錄
  85.      * @throws IOException
  86.      */
  87.     public static void copy(String sourceFile, String destFile) throws IOException {
  88.         copy(new File(sourceFile), new File(destFile));
  89.     }
  90.  
  91.     /**
  92.      * 複製檔案或目錄
  93.      * 
  94.      * @param sourceFile
  95.      *            來源檔案或目錄
  96.      * @param destFile
  97.      *            目的目錄
  98.      * @throws IOException
  99.      */
  100.     public static void copy(File sourceFile, File destFile) throws IOException {
  101.         if (sourceFile.isFile()) {
  102.             sourceFile.setReadable(true);
  103.             FileInputStream fis = new FileInputStream(sourceFile);
  104.             FileOutputStream fos = new FileOutputStream(destFile);
  105.             BufferedInputStream bis = new BufferedInputStream(fis);
  106.             BufferedOutputStream bos = new BufferedOutputStream(fos);
  107.  
  108.             byte[] buffer = new byte[128];
  109.             int readed = -1;
  110.             while ((readed = bis.read(buffer)) > -1) {
  111.                 bos.write(buffer, 0, readed);
  112.             }
  113.  
  114.             bos.close();
  115.             fos.close();
  116.             fis.close();
  117.             fos.close();
  118.         } else {
  119.             destFile.mkdir();
  120.             File[] files = sourceFile.listFiles();
  121.             for (File f : files) {
  122.                 copy(f, new File(String.format("%s\\%s", destFile.getAbsolutePath(), f.getName())));
  123.             }
  124.         }
  125.     }
  126.  
  127.     /**
  128.      * 讀取檔案內的全部文字資料
  129.      * 
  130.      * @param file
  131.      *            要讀取的檔案
  132.      * @return
  133.      * @throws IOException
  134.      */
  135.     public static String readAllText(File file) throws IOException {
  136.         return new String(readAllBytes(file));
  137.     }
  138.  
  139.     /**
  140.      * 讀取檔案內的全部文字資料
  141.      * 
  142.      * @param file
  143.      *            要讀取的檔案
  144.      * @param encoding
  145.      *            文字編碼
  146.      * @return
  147.      * @throws IOException
  148.      */
  149.     public static String readAllText(File file, String encoding) throws IOException {
  150.         return new String(readAllBytes(file), encoding);
  151.     }
  152.  
  153.     /**
  154.      * 讀取檔案內的全部位元資料
  155.      * 
  156.      * @param file
  157.      *            要讀取的檔案
  158.      * @return
  159.      * @throws IOException
  160.      */
  161.     public static byte[] readAllBytes(File file) throws IOException {
  162.         FileInputStream fis = new FileInputStream(file);
  163.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  164.         int b = -1;
  165.         while ((= fis.read()) > -1) {
  166.             baos.write(b);
  167.         }
  168.         baos.close();
  169.         fis.close();
  170.         return baos.toByteArray();
  171.     }
  172.  
  173.     /**
  174.      * 將內容寫入檔案. 寫入時呼叫 toString() 並將回傳值寫入檔案
  175.      * 
  176.      * @param file
  177.      *            要寫入的檔案
  178.      * @param content
  179.      *            要寫入的內容
  180.      * @throws IOException
  181.      */
  182.     public static void writeAllText(File file, Object content) throws IOException {
  183.         FileWriter writer = new FileWriter(file);
  184.         writer.write(content.toString());
  185.         writer.close();
  186.     }
  187.  
  188.     /**
  189.      * 將內容寫入檔案. 寫入時呼叫 toString() 並將回傳值寫入檔案
  190.      * 
  191.      * @param file
  192.      *            要寫入的檔案
  193.      * @param content
  194.      *            要寫入的內容
  195.      * @param encoding
  196.      *            文字編碼
  197.      * @throws IOException
  198.      */
  199.     public static void writeAllText(File file, Object content, String encoding) throws IOException {
  200.         FileOutputStream fos = new FileOutputStream(file);
  201.         OutputStreamWriter writer = new OutputStreamWriter(fos, encoding);
  202.         writer.write(content.toString());
  203.         writer.close();
  204.         fos.close();
  205.     }
  206.  
  207.     /**
  208.      * 將內容寫入檔案. 寫入時呼叫 toString() 並將回傳值寫入檔案, 若檔案存在, 將內容加入至檔案結尾, 檔案不存在時, 直接寫入
  209.      * 
  210.      * @param file
  211.      *            要寫入的檔案
  212.      * @param content
  213.      *            要寫入的內容
  214.      * @throws IOException
  215.      */
  216.     public static void appendAllText(File file, Object content) throws IOException {
  217.         FileWriter writer = new FileWriter(file, true);
  218.         writer.write(content.toString());
  219.         writer.close();
  220.     }
  221.  
  222.     /**
  223.      * 將內容寫入檔案. 寫入時呼叫 toString() 並將回傳值寫入檔案, 若檔案存在, 將內容加入至檔案結尾, 檔案不存在時, 直接寫入
  224.      * 
  225.      * @param file
  226.      *            要寫入的檔案
  227.      * @param content
  228.      *            要寫入的內容
  229.      * @param encoding
  230.      *            文字編碼
  231.      * @throws IOException
  232.      */
  233.     public static void appendAllText(File file, Object content, String encoding) throws IOException {
  234.         if (file.exists()) {
  235.             appendAllText(file, content);
  236.         } else {
  237.             writeAllText(file, content, encoding);
  238.         }
  239.     }
  240.  
  241.     /**
  242.      * 將內容寫入檔案
  243.      * 
  244.      * @param file
  245.      *            要寫入的檔案
  246.      * @param bytes
  247.      *            要寫入的內容
  248.      * @throws IOException
  249.      */
  250.     public static void writeAllBytes(File file, byte[] bytes) throws IOException {
  251.         FileOutputStream fos = new FileOutputStream(file);
  252.         fos.write(bytes);
  253.         fos.close();
  254.     }
  255.  
  256.     /**
  257.      * 將內容寫入檔案. 若檔案存在, 將內容加入至檔案結尾, 檔案不存在時, 直接寫入
  258.      * 
  259.      * @param f
  260.      *            要寫入的檔案
  261.      * @param bytes
  262.      *            要寫入的內容
  263.      * @throws IOException
  264.      */
  265.     public static void appendAllBytes(File f, byte[] bytes) throws IOException {
  266.         FileOutputStream fos = new FileOutputStream(f, true);
  267.         fos.write(bytes);
  268.         fos.close();
  269.     }
  270. }
DateHelper
  1. package bruce.lib;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.Locale;
  6.  
  7. /**
  8.  * @author NaNashi
  9.  * 
  10.  */
  11. public class DateHelper {
  12.     private final static SimpleDateFormat LOG_DATE = new SimpleDateFormat("yyyyMMdd", Locale.US);
  13.     private final static SimpleDateFormat DATE_ONLY = new SimpleDateFormat("yyyy/MM/dd", Locale.US);
  14.     private final static SimpleDateFormat FULL_DATE = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US);
  15.  
  16.     public final static String logDate() {
  17.         return LOG_DATE.format(new Date());
  18.     }
  19.  
  20.     public final static String formatDate(Date date) {
  21.         return date == null ? null : DATE_ONLY.format(date);
  22.     }
  23.  
  24.     public final static String formatFullDate(Date date) {
  25.         return date == null ? null : FULL_DATE.format(date);
  26.     }
  27.  
  28.     public final static Date parseDate(String source) {
  29.         return parseDate(source, false);
  30.     }
  31.  
  32.     public final static Date parseDate(String source, boolean returnNowOnfailed) {
  33.         try {
  34.             return DATE_ONLY.parse(source);
  35.         } catch (Exception e) {
  36.             return returnNowOnfailed ? new Date() : null;
  37.         }
  38.     }
  39.  
  40.     public final static Date parseFullDate(String source) {
  41.         return parseDate(source, false);
  42.     }
  43.  
  44.     public final static Date parseFullDate(String source, boolean returnNowOnfailed) {
  45.         try {
  46.             return FULL_DATE.parse(source);
  47.         } catch (Exception e) {
  48.             return returnNowOnfailed ? new Date() : null;
  49.         }
  50.     }
  51.  
  52.     public final static String diffTime(Date before, Date after) {
  53.         long sticks = Math.abs(before.getTime() - after.getTime());
  54.         int seconds = (int) (sticks / 1000);
  55.         int minutes = seconds / 60;
  56.         int hours = minutes / 60;
  57.         int days = hours / 24;
  58.  
  59.         StringBuilder sb = new StringBuilder();
  60.         if (days > 0) {
  61.             sb.append(days).append("天");
  62.             hours -= days * 24;
  63.             minutes -= days * 24 * 60;
  64.             seconds -= days * 24 * 60 * 60;
  65.         }
  66.         if (hours > 0) {
  67.             sb.append(hours).append("小時");
  68.             minutes -= hours * 60;
  69.             seconds -= hours * 60 * 60;
  70.         }
  71.         if (hours > 0 || minutes > 0) {
  72.             sb.append(minutes).append("分");
  73.             seconds -= minutes * 60;
  74.         }
  75.  
  76.         sb.append(seconds).append("秒").append(before.after(after) ? "後" : "前");
  77.         return sb.toString();
  78.     }
  79.  
  80.     public static String replaceMonthWord(String s) {
  81.         s = s.toLowerCase();
  82.         String[] month = new String[] { "January", "Februaary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  83.         for (int i = 0; i < month.length; i++) {
  84.             String m = month[i].toLowerCase();
  85.             if (s.contains(m.toLowerCase()))
  86.                 return s.replace(m, String.valueOf(+ 1));
  87.         }
  88.         return s;
  89.     }
  90. }
DateTime
  1. package bruce.lib;
  2.  
  3. import java.io.Serializable;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Arrays;
  7. import java.util.Calendar;
  8. import java.util.Date;
  9. import java.util.Locale;
  10. import java.util.TimeZone;
  11. import java.util.TreeSet;
  12.  
  13. public class DateTime implements Serializable, Cloneable, Comparable<DateTime> {
  14.  
  15.     public static DateTime now() {
  16.         return new DateTime();
  17.     }
  18.  
  19.     public static DateTime fromDate(Date date) {
  20.         DateTime d = new DateTime();
  21.         d.c.setTime(date);
  22.         return d;
  23.     }
  24.  
  25.     public static DateTime fromTimestamp(long stamp) {
  26.         DateTime d = new DateTime();
  27.         d.c.setTimeInMillis(stamp);
  28.         return d;
  29.     }
  30.  
  31.     public static DateTime fromCalendar(Calendar c) {
  32.         DateTime d = new DateTime();
  33.         d.= c;
  34.         return d;
  35.     }
  36.  
  37.     public static DateTime fromString(String date, String format) throws ParseException {
  38.         DateTime d = new DateTime();
  39.         d.c.setTime(new SimpleDateFormat(format).parse(date));
  40.         return d;
  41.     }
  42.  
  43.     public static DateTime fromString(String date, String format, Locale locale) throws ParseException {
  44.         DateTime d = new DateTime();
  45.         d.c.setTime(new SimpleDateFormat(format, locale).parse(date));
  46.         return d;
  47.     }
  48.  
  49.     public static DateTime latest(DateTime... dates) {
  50.         TreeSet<DateTime> set = new TreeSet<DateTime>();
  51.         set.addAll(Arrays.asList(dates));
  52.         return set.last();
  53.     }
  54.  
  55.     public static DateTime oldest(DateTime... dates) {
  56.         TreeSet<DateTime> set = new TreeSet<DateTime>();
  57.         set.addAll(Arrays.asList(dates));
  58.         return set.first();
  59.     }
  60.  
  61.     private Calendar c;
  62.  
  63.     public enum Field {
  64.         ERA, // AD/BC
  65.         YEAR, //
  66.         MONTH, //
  67.         WEEK_OF_YEAR, //
  68.         WEEK_OF_MONTH, //
  69.         AM_PM_MARKER, // AM/PM
  70.         DATE, //
  71.         DAY_OF_MONTH, //
  72.         DAY_OF_WEEK, //
  73.         DAY_OF_WEEK_IN_MONTH, //
  74.         DAY_OF_YEAR, //
  75.         HOUR, //
  76.         MINUTE, //
  77.         SECOND, //
  78.         MILLISECOND, //
  79.         TIMEZONE
  80.     }
  81.  
  82.     public class Month {
  83.         public static final int JANUARY = 0;
  84.         public static final int FEBRUARY = 1;
  85.         public static final int MARCH = 2;
  86.         public static final int APRIL = 3;
  87.         public static final int MAY = 4;
  88.         public static final int JUNE = 5;
  89.         public static final int JULY = 6;
  90.         public static final int AUGUST = 7;
  91.         public static final int SEPTEMBER = 8;
  92.         public static final int OCTOBER = 9;
  93.         public static final int NOVEMBER = 10;
  94.         public static final int DECEMBER = 11;
  95.         public static final int UNDECIMBER = 12;
  96.     }
  97.  
  98.     public class Day {
  99.         public static final int SUNDAY = 1;
  100.         public static final int MONDAY = 2;
  101.         public static final int TUESDAY = 3;
  102.         public static final int WEDNESDAY = 4;
  103.         public static final int THURSDAY = 5;
  104.         public static final int FRIDAY = 6;
  105.         public static final int SATURDAY = 7;
  106.     }
  107.  
  108.     private DateTime() {
  109.         this.= Calendar.getInstance();
  110.     }
  111.  
  112.     public boolean after(DateTime datetime) {
  113.         return this.toTimeInMillis() > datetime.toTimeInMillis();
  114.     }
  115.  
  116.     public boolean before(DateTime datetime) {
  117.         return this.toTimeInMillis() < datetime.toTimeInMillis();
  118.     }
  119.  
  120.     public DateTime add(Field field, int amount) {
  121.         switch (field) {
  122.         case YEAR:
  123.             this.c.add(Calendar.YEAR, amount);
  124.             return this;
  125.         case MONTH:
  126.             this.c.add(Calendar.MONTH, amount);
  127.             return this;
  128.         case WEEK_OF_YEAR:
  129.         case WEEK_OF_MONTH:
  130.             this.c.add(Calendar.DATE, amount * 7);
  131.             return this;
  132.         case DATE:
  133.         case DAY_OF_MONTH:
  134.         case DAY_OF_WEEK:
  135.         case DAY_OF_WEEK_IN_MONTH:
  136.         case DAY_OF_YEAR:
  137.             this.c.add(Calendar.DATE, amount);
  138.             return this;
  139.         case MINUTE:
  140.             this.c.add(Calendar.MINUTE, amount);
  141.             return this;
  142.         case SECOND:
  143.             this.c.add(Calendar.SECOND, amount);
  144.             return this;
  145.         case HOUR:
  146.             this.c.add(Calendar.HOUR, amount);
  147.             return this;
  148.         case MILLISECOND:
  149.             this.c.add(Calendar.MILLISECOND, amount);
  150.             return this;
  151.         default:
  152.             throw new IllegalArgumentException("Unaccepted field.");
  153.         }
  154.     }
  155.  
  156.     public DateTime addYear(int amount) {
  157.         return this.add(Field.YEAR, amount);
  158.     }
  159.  
  160.     public DateTime addMonth(int amount) {
  161.         return this.add(Field.MONTH, amount);
  162.     }
  163.  
  164.     public DateTime addWeek(int amount) {
  165.         return this.add(Field.WEEK_OF_YEAR, amount);
  166.     }
  167.  
  168.     public DateTime addDay(int amount) {
  169.         return this.add(Field.DAY_OF_YEAR, amount);
  170.     }
  171.  
  172.     public DateTime addMinute(int amount) {
  173.         return this.add(Field.MINUTE, amount);
  174.     }
  175.  
  176.     public DateTime addSecond(int amount) {
  177.         return this.add(Field.SECOND, amount);
  178.     }
  179.  
  180.     public DateTime addHour(int amount) {
  181.         return this.add(Field.HOUR, amount);
  182.     }
  183.  
  184.     public DateTime addMilliSecond(int amount) {
  185.         return this.add(Field.MILLISECOND, amount);
  186.     }
  187.  
  188.     public DateTime set(Field field, int value) {
  189.         switch (field) {
  190.         // case ERA:
  191.         case YEAR:
  192.             this.c.set(Calendar.YEAR, value);
  193.             return this;
  194.         case MONTH:
  195.             this.c.set(Calendar.MONTH, value);
  196.             return this;
  197.             // case WEEK_OF_YEAR:
  198.             // case WEEK_OF_MONTH:
  199.             // case AM_PM_MARKER:
  200.         case DATE:
  201.             this.c.set(Calendar.DATE, value);
  202.             return this;
  203.             // case DAY_OF_MONTH:
  204.             // case DAY_OF_WEEK:
  205.             // case DAY_OF_WEEK_IN_MONTH:
  206.             // case DAY_OF_YEAR:
  207.         case HOUR:
  208.             this.c.set(Calendar.HOUR, value);
  209.             return this;
  210.         case MINUTE:
  211.             this.c.set(Calendar.MINUTE, value);
  212.             return this;
  213.         case SECOND:
  214.             this.c.set(Calendar.SECOND, value);
  215.             return this;
  216.         case MILLISECOND:
  217.             this.c.set(Calendar.MILLISECOND, value);
  218.             return this;
  219.             // case TIMEZONE:
  220.         default:
  221.             throw new IllegalArgumentException("Unaccepted field.");
  222.         }
  223.     }
  224.  
  225.     public DateTime setYear(int value) {
  226.         return this.set(Field.YEAR, value);
  227.     }
  228.  
  229.     public DateTime setMonth(int value) {
  230.         return this.set(Field.MONTH, value);
  231.     }
  232.  
  233.     public DateTime setDate(int value) {
  234.         return this.set(Field.DATE, value);
  235.     }
  236.  
  237.     public DateTime setHour(int value) {
  238.         return this.set(Field.HOUR, value);
  239.     }
  240.  
  241.     public DateTime setMinute(int value) {
  242.         return this.set(Field.MINUTE, value);
  243.     }
  244.  
  245.     public DateTime setSecond(int value) {
  246.         return this.set(Field.SECOND, value);
  247.     }
  248.  
  249.     public DateTime setMilliSecond(int value) {
  250.         return this.set(Field.MILLISECOND, value);
  251.     }
  252.  
  253.     public int get(Field field) {
  254.         switch (field) {
  255.         case YEAR:
  256.             return this.c.get(Calendar.YEAR);
  257.         case MONTH:
  258.             return this.c.get(Calendar.MONTH);
  259.         case WEEK_OF_YEAR:
  260.             return this.c.get(Calendar.WEEK_OF_YEAR);
  261.         case WEEK_OF_MONTH:
  262.             return this.c.get(Calendar.WEEK_OF_MONTH);
  263.         case DATE:
  264.             return this.c.get(Calendar.DATE);
  265.         case DAY_OF_MONTH:
  266.             return this.c.get(Calendar.DAY_OF_MONTH);
  267.         case DAY_OF_WEEK:
  268.             return this.c.get(Calendar.DAY_OF_WEEK);
  269.         case DAY_OF_WEEK_IN_MONTH:
  270.             return this.c.get(Calendar.DAY_OF_WEEK_IN_MONTH);
  271.         case DAY_OF_YEAR:
  272.             return this.c.get(Calendar.DAY_OF_YEAR);
  273.         case MINUTE:
  274.             return this.c.get(Calendar.MINUTE);
  275.         case SECOND:
  276.             return this.c.get(Calendar.SECOND);
  277.         case HOUR:
  278.             return this.c.get(Calendar.HOUR);
  279.         case MILLISECOND:
  280.             return this.c.get(Calendar.MILLISECOND);
  281.         default:
  282.             throw new IllegalArgumentException("Unaccepted field.");
  283.         }
  284.     }
  285.  
  286.     public int getYear() {
  287.         return this.get(Field.YEAR);
  288.     }
  289.  
  290.     public int getMonth() {
  291.         return this.get(Field.MONTH);
  292.     }
  293.  
  294.     public int getWeekOfYear() {
  295.         return this.get(Field.WEEK_OF_YEAR);
  296.     }
  297.  
  298.     public int getWeekOfMonth() {
  299.         return this.get(Field.WEEK_OF_MONTH);
  300.     }
  301.  
  302.     public int getDate() {
  303.         return this.get(Field.DATE);
  304.     }
  305.  
  306.     public int getDayOfMonth() {
  307.         return this.get(Field.DAY_OF_MONTH);
  308.     }
  309.  
  310.     public int getDayOfWeek() {
  311.         return this.get(Field.DAY_OF_WEEK);
  312.     }
  313.  
  314.     public int getDayOfWeekInMonth() {
  315.         return this.get(Field.DAY_OF_WEEK_IN_MONTH);
  316.     }
  317.  
  318.     public int getDayOfYear() {
  319.         return this.get(Field.DAY_OF_YEAR);
  320.     }
  321.  
  322.     public int getHour() {
  323.         return this.get(Field.HOUR);
  324.     }
  325.  
  326.     public int getMinute() {
  327.         return this.get(Field.MINUTE);
  328.     }
  329.  
  330.     public int getSecond() {
  331.         return this.get(Field.SECOND);
  332.     }
  333.  
  334.     public int getMilliSecond() {
  335.         return this.get(Field.MILLISECOND);
  336.     }
  337.  
  338.     public TimeZone getTimeZone() {
  339.         return this.c.getTimeZone();
  340.     }
  341.  
  342.     public DateTime setTimeZone(TimeZone zone) {
  343.         this.c.setTimeZone(zone);
  344.         return this;
  345.     }
  346.  
  347.     public long toTimeInMillis() {
  348.         return this.c.getTimeInMillis();
  349.     }
  350.  
  351.     public Date toDate() {
  352.         return this.c.getTime();
  353.     }
  354.  
  355.     public Calendar toCalendar() {
  356.         return this.c;
  357.     }
  358.  
  359.     @Override
  360.     public String toString() {
  361.         return this.c.getTime().toString();
  362.     }
  363.  
  364.     public String toString(String format) {
  365.         SimpleDateFormat f = new SimpleDateFormat(format);
  366.         return f.format(this.c.getTime());
  367.     }
  368.  
  369.     public String toString(String format, Locale locale) {
  370.         SimpleDateFormat f = new SimpleDateFormat(format, locale);
  371.         return f.format(this.c.getTime());
  372.     }
  373.  
  374.     @Override
  375.     public int hashCode() {
  376.         return c.getTime().hashCode();
  377.     }
  378.  
  379.     @Override
  380.     public boolean equals(Object obj) {
  381.         if (this == obj)
  382.             return true;
  383.         if (obj == null)
  384.             return false;
  385.         if (getClass() != obj.getClass())
  386.             return false;
  387.         DateTime other = (DateTime) obj;
  388.         return this.c.getTimeInMillis() == other.toTimeInMillis();
  389.     }
  390.  
  391.     @Override
  392.     public int compareTo(DateTime o) {
  393.         return (int) (this.toTimeInMillis() - o.toTimeInMillis());
  394.     }
  395.  
  396. }
StreamReader
  1. package bruce.lib.io;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. /**
  8.  * @author Bruce Tsai
  9.  * 
  10.  */
  11. public class StreamReader {
  12.  
  13.     private InputStream stream;
  14.     public String encoding;
  15.  
  16.     /**
  17.      * @param stream
  18.      */
  19.     public StreamReader(InputStream stream) {
  20.         this.stream = stream;
  21.     }
  22.  
  23.     /**
  24.      * @param stream
  25.      * @param encoding
  26.      */
  27.     public StreamReader(InputStream stream, String encoding) {
  28.         this.stream = stream;
  29.         this.encoding = encoding;
  30.     }
  31.  
  32.     /**
  33.      * @return
  34.      * @throws IOException
  35.      */
  36.     public String readAllText() throws IOException {
  37.         return this.encoding == null || this.encoding.length() == 0 ? new String(readAllBytes()) : new String(readAllBytes(), this.encoding);
  38.     }
  39.  
  40.     /**
  41.      * @return
  42.      * @throws IOException
  43.      */
  44.     public byte[] readAllBytes() throws IOException {
  45.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  46.         byte[] buffer = new byte[128];
  47.         int readed = -1;
  48.         while ((readed = this.stream.read(buffer)) > -1) {
  49.             baos.write(buffer, 0, readed);
  50.         }
  51.         baos.close();
  52.         this.stream.close();
  53.         return baos.toByteArray();
  54.     }
  55. }
ResourceReader
  1. package bruce.lib;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6.  
  7. /**
  8.  * @author NaNashi
  9.  * 
  10.  */
  11. public class ResourceReader {
  12.  
  13.     public static String readText(String path, String encoding) throws IOException {
  14.         InputStream is = ResourceReader.class.getClassLoader().getResourceAsStream(path);
  15.         StreamReader reader = new StreamReader(is, encoding);
  16.         String content = reader.readToEnd();
  17.         is.close();
  18.         return content;
  19.     }
  20.  
  21.     public static byte[] readBytes(String path) throws IOException {
  22.         InputStream is = ResourceReader.class.getClassLoader().getResourceAsStream(path);
  23.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  24.         int value = -1;
  25.         while ((value = is.read()) > -1)
  26.             baos.write(value);
  27.         baos.close();
  28.         is.close();
  29.         return baos.toByteArray();
  30.     }
  31. }
Validator
  1. package bruce.lib;
  2.  
  3. import java.util.regex.Pattern;
  4.  
  5. public class Validator {
  6.     public final static boolean isCreditCard(CharSequence input) {
  7.         return input == null ? false : Pattern.matches("^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\\d{3})\\d{11})$", input);
  8.     }
  9.  
  10.     public final static boolean isDomain(CharSequence input) {
  11.         return input == null ? false : Pattern.matches("^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\\.[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)?$", input);
  12.     }
  13.  
  14.     public final static boolean isFloatingPointNumber(CharSequence input) {
  15.         return input == null ? false : Pattern.matches("[-+]?([0-9]+\\.?[0-9]*|\\.[0-9]+)([eE][-+]?[0-9]+)?", input);
  16.     }
  17.  
  18.     public final static boolean isGUID(CharSequence input) {
  19.         return input == null ? false : Pattern.matches("^[\\d\\w]{8}-[\\d\\w]{4}-[\\d\\w]{4}-[\\d\\w]{4}-[\\d\\w]{12}$", input);
  20.     }
  21.  
  22.     public final static boolean isIPAddress(CharSequence input) {
  23.         return input == null ? false : Pattern.matches("\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b", input);
  24.     }
  25.  
  26.     public final static boolean isLettersOnly(CharSequence input) {
  27.         return input == null ? false : Pattern.matches("^[[:alpha:]]+$", input);
  28.     }
  29.  
  30.     public final static boolean isMailAddress(CharSequence input) {
  31.         return input == null ? false : Pattern.matches("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*", input);
  32.     }
  33.  
  34.     public final static boolean isNumeric(CharSequence input) {
  35.         return input == null ? false : Pattern.matches("^\\d+$", input);
  36.     }
  37.  
  38.     public final static boolean isPhoneNumber(CharSequence input) {
  39.         return input == null ? false : Pattern.matches("^[\\d-]{7,15}$", input);
  40.     }
  41.  
  42.     public final static boolean isSocialID(CharSequence input) {
  43.         return input == null ? false : Pattern.matches("^[a-zA-Z]\\d{9}$", input);
  44.     }
  45.  
  46.     public final static boolean isSymbolIncluded(CharSequence input) {
  47.         return input == null ? false : Pattern.matches("[`~!@#$%^&*\\(\\)_+=-\\|\\[\\]{};':\",\\./<>?]", input);
  48.     }
  49.  
  50.     public final static boolean isUrl(CharSequence input) {
  51.         return input == null ? false : Pattern.matches("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?", input);
  52.     }
  53. }
A simple work
  1. public class Tri {
  2.  
  3.     /**
  4.      * @param args
  5.      */
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.         int last = 5;
  9.         for (int i = 1, n = 1; i <= last; i++) {
  10.             int p = n;
  11.             while (p-- > 0)
  12.                 System.out.print(Integer.toHexString(i));
  13.  
  14.             n = i <= last / 2 ? n + 1 : n - 1;
  15.             System.out.println();
  16.         }
  17.     }
  18.  
  19. }