android字串轉換為時間戳
阿新 • • 發佈:2019-02-04
// 時間轉換 public static String transform(String from) { String to = ""; SimpleDateFormat simple = new SimpleDateFormat("HH:mm:ss"); // SimpleDateFormat simple = new // SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 本地時區 Calendar nowCal = Calendar.getInstance(); TimeZone localZone = nowCal.getTimeZone(); // 設定SDF的時區為本地 simple.setTimeZone(localZone); SimpleDateFormat simple1 = new SimpleDateFormat("HH:mm:ss"); // SimpleDateFormat simple1 = new // SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 設定 DateFormat的時間區域為GMT simple1.setTimeZone(TimeZone.getTimeZone("GMT")); // 把字串轉化為Date物件,然後格式化輸出這個Date Date fromDate = new Date(); try { // 時間string解析成GMT時間 try { fromDate = simple1.parse(from); } catch (java.text.ParseException e) { e.printStackTrace(); } // GMT時間轉成當前時區的時間 to = simple.format(fromDate); } catch (ParseException e1) { e1.printStackTrace(); } return to; }
安卓開發、ios開發、QT移動開發技術交流學習群 347131054
。歡迎有志做移動開發的同行加入!
347131054347131054
347131054