目前分類:Java (13)
- Jul 27 Fri 2018 16:50
[程式][Java][轉貼] Java8 日期/時間(Date Time)API指南
- Jul 12 Thu 2018 15:39
[程式][Java] 移除字串中的空白字元
- Jul 12 Thu 2018 14:59
[程式][Java][API][Apache] 檢查有無包含子字串(不分大小寫)
Java 的 String 有個 API:「java.lang.String.contains(CharSequence s)」
可以用來比對,是否包含傳入的字串。
但是我想不分大小寫比對怎麼辦呢?
- Jul 12 Thu 2018 14:52
[程式][Java] 如何移除字串陣列(或List)中的null和空字串
參考:
List<String> list = new ArrayList<String>(Arrays.asList("", "Hi", null, "How")); System.out.println(list); list.removeAll(Arrays.asList("", null)); System.out.println(list);
來源:arrays - In Java, remove empty elements from a list of Strings - Stack Overflow
- May 16 Wed 2018 15:01
[程式][Java] 判斷 父類別 為 子類別 的實例
例如:
class Car { } class Audi extends Car { } class Seat extends Car { } class Mercedes extends Car { }
Car car = new Audi(); // car is instance of Audi, Seat or Mercedes ?
答:「使用 "instanceof" 關鍵字。」
- May 16 Wed 2018 14:50
[程式][Java] 建構子、初始化 區塊執行順序
public class Test { static { System.out.println("Static"); } { System.out.println("Non-static block"); } public static void main(String[] args) { Test t = new Test(); Test t2 = new Test(); } }
輸出結果:
Static |
- May 16 Wed 2018 10:59
[程式][Java] 字串格式
- May 15 Tue 2018 16:18
[程式][Java] 時區代號清單
Java 8 u171
String[] timeZoneIds = java.util.TimeZone.getAvailableIDs() ; for ( String aTimeZoneId : timeZoneIds ) { System.out.println( aTimeZoneId ); } System.out.println( "Total: " + timeZoneIds.length );
Africa/Abidjan |
- May 07 Mon 2018 08:46
[程式][Java] 日期時間物件之轉換,及輸出字串
java.util.Calendar、java.util.Date、java.sql.timestamp
取得現在時間,"toString()"結果:
- java.util.Calendar
java.util.Calendar cal = java.util.Calendar.getInstance(); System.out.println( cal ); // "java.util.GregorianCalendar[time=1525760873174,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT+08:00",offset=28800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=4,WEEK_OF_YEAR=19,WEEK_OF_MONTH=2,DAY_OF_MONTH=8,DAY_OF_YEAR=128,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=27,SECOND=53,MILLISECOND=174,ZONE_OFFSET=28800000,DST_OFFSET=0]"
- java.util.Date
java.util.Date utilDate = new java.util.Date(); System.out.println( utilDate ); // "Tue May 08 14:24:13 GMT+08:00 2018"
- java.sql.timestamp
Timestamp timestamp = new Timestamp( System.currentTimeMillis() ); System.out.println( sqlTstp ); // "2018-05-08 14:24:13.338"
參考:
- May 03 Thu 2018 17:55
[程式][Java] Exception StackTrace to String
方法1:
String excepStr = org.apache.commons.lang.exception.ExceptionUtils.getStackTrace( throwable ) ;
方法2:
StringWriter errors = new StringWriter(); ex.printStackTrace(new PrintWriter(errors)); return errors.toString();
- May 03 Thu 2018 16:35
[程式][Java] 判斷 繁/簡 中、英、全形/半形 符號 字元 (UTF8)
/** * @author Robert Chen */ public class CharUtil { /** * Ref: http://www.micmiu.com/lang/java/java-check-chinese/ * @param ch * @return */ public static boolean IsChinese( char ch ) { Character.UnicodeBlock ub = Character.UnicodeBlock.of( ch ); if ( ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) { return true; } return false; } /** * Printable characters * @param ch * @return */ public static boolean IsPrintableAsciiChar( char ch ) { if ( 32 <= ch && ch <= 126 ) return true ; return false ; } public static void main( String[] args ) { // Ref: http://tw.gitbook.net/java/lang/character.unicodeblock_of.html // 32~126: Printable characters for ( char ch = 0; ch < 128 ; ++ch ) System.out.println( Character.UnicodeBlock.of( ch ) ); // All is "BASIC_LATIN". System.out.println(); // Ref: https://www.yiibai.com/java/lang/character.unicodeblock_of_codepoint.html#article-start System.out.println( Character.UnicodeBlock.of(950) ); // "GREEK" } }
使用:
- Jul 07 Fri 2017 14:42
[程式][Java] 如何開啟一個檔案 以附加方式 寫入資料?
簡單記錄:
private static final String FILENAME = "E:\\test\\filename.txt"; File file = new File(FILENAME); // if file doesnt exists, then create it if (!file.exists()) { file.createNewFile(); } // true = append file FileWriter fw = new FileWriter(file.getAbsoluteFile(), true); BufferedWriter bw = new BufferedWriter(fw); bw.write(data); bw.close(); fw.close();
來源:Java – How to append content to file
- Mar 29 Wed 2017 09:50
[Java] 快速切換jdk的方法 (Windows)