編輯:Android 程式設計入門
做過 Android 與 PHP 後端對接的人,大概都遇過這個問題:Java 里 Date.getTime() 拿到的是 13 位毫秒級時間戳,而很多 PHP 接口返回的是 10 位秒級時間戳。兩者混用,時間就會莫名跳到 1970 年。下面這份工具類把常見場景都封裝好了。
所有轉換的基礎,就是把 13 位截成 10 位:
/**
* Java 時間戳是 13 位,PHP 後台是 10 位,因此需要截斷
*/
private static long timestampConvent(long stamp) {
String temp = stamp + "";
return Long.parseLong(temp.substring(0, 10));
}
/** 獲取今天的時間戳 */
public static long getNowTimeStamp() {
return timestampConvent(new Date().getTime());
}
/** 獲取明天的時間戳 */
public static long getTomorrowTimeStamp() {
Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DAY_OF_MONTH, 1);
return timestampConvent(tomorrow.getTimeInMillis());
}
/**
* 獲取指定時間字符串對應的時間戳
* @param time 時間字符串
* @param format 格式,需與 time 的實際格式一致,例如 yyyy-MM-dd HH:mm:ss
*/
public static long getTimeStamp(String time, String format) {
DateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
try {
return timestampConvent(sdf.parse(time).getTime());
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
社交類 App 常見的需求:今天顯示「下午 15:20」,昨天顯示「昨天 09:12」,更早則顯示「10月18日 21:03」。思路是先和今天比對,再逐級往前推。
/**
* 把時間戳轉為閱讀友好的字符串
* @param time 10 位時間戳;服務端傳過來的是 10 位,這里 *1000 補償到 13 位
*/
public static String getFormatTime(long time) {
Calendar target = Calendar.getInstance();
target.setTime(new Date(time * 1000));
String format = "yyyy年MM月dd日 HH:mm:ss";
Calendar today = Calendar.getInstance();
boolean sameDay = today.get(Calendar.YEAR) == target.get(Calendar.YEAR)
&& today.get(Calendar.MONTH) == target.get(Calendar.MONTH)
&& today.get(Calendar.DAY_OF_MONTH) == target.get(Calendar.DAY_OF_MONTH);
if (sameDay) {
// 同一天,再按時段加前綴
int hour = target.get(Calendar.HOUR_OF_DAY);
if (hour < 3) format = "凌晨 HH:mm:ss";
else if (hour < 12) format = "上午 HH:mm:ss";
else if (hour < 18) format = "下午 HH:mm:ss";
else format = "晚上 HH:mm:ss";
} else {
// 加一天後若等於今天,說明目標是昨天
target.add(Calendar.DAY_OF_MONTH, 1);
boolean isYesterday = today.get(Calendar.YEAR) == target.get(Calendar.YEAR)
&& today.get(Calendar.MONTH) == target.get(Calendar.MONTH)
&& today.get(Calendar.DAY_OF_MONTH) == target.get(Calendar.DAY_OF_MONTH);
if (isYesterday) {
format = "昨天 HH:mm:ss";
} else if (target.get(Calendar.YEAR) == today.get(Calendar.YEAR)) {
format = "MM月dd日 HH:mm:ss"; // 同一年省略年份
}
}
return timestampToString(time + "", format);
}
把字符串時間戳按指定模板格式化,統一從這里出去,好處是後續要加「前 3 秒」這類相對時間只需要改一個方法。
/** 10 位時間戳轉換為指定格式的時間字符串 */
public static String timestampToString(String time, String format) {
if (time == null || time.length() <= 3) {
return "";
}
long temp = Long.parseLong(time) * 1000; // 10 位補償到 13 位
Timestamp ts = new Timestamp(temp);
try {
return new SimpleDateFormat(format, Locale.CHINA).format(ts);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
小結一下:只要牢牢記住「Java 13 位、數據庫與部分後端 10 位,進出各乘除 1000 一次」,這類時間錯亂的問題基本就不會再困擾你了。
Android 進階學習:四大組件、異步、跨進程通信與系統框架
本文系統梳理 Android 進階知識,涵蓋四大組件、異步消息處理、跨進程通信、自定義控件以及系統分層架構。 一、Android 四大組件 1. Activity 生命
Android 輸入法開發:最小可行版本
本文以 Android Studio 建立一個最簡單的輸入法,重點在於理解輸入法的基本結構,而非完整鍵盤體驗。建立專案時,可選擇不新增 Activity,因為輸入法主要透
動手實現 Android 圖片輪播控件
1. 輪播控件的組成部分 我們以知乎日報 Android 客戶端的輪播控件為例,拆解一下它的主要組成。 首先,我們需要一個用來顯示圖片的 View 對象。觀察上圖底部中
Android Studio啟動時ADB無響應?手把手教你解決
啟動 Android Studio 時,如果跳出以下提示: adb not responding. you can wait more, or kill adb.exe