1. 程式人生 > >android時間戳轉換成日期(與php後臺),日期轉換成時間戳小問題記錄

android時間戳轉換成日期(與php後臺),日期轉換成時間戳小問題記錄

之前常用的是,和java開發的後臺對接資料,正常也沒有遇到過這個問題,又一次在和php對接的後臺做專案是,時間轉換出了問題,我看了些許時間的程式碼,都沒有問題。最後發現返回時間戳的位數不對,才確認到,問題是出現在這裡

PHP和Java時間戳存在三位位差!!!

所以,在工具類裡面,對於接收伺服器的時間戳,轉換之前,添加了一個判斷,以方便“以防萬一”。

程式碼塊:

   public static String getFetureDate(long expire) {
        //PHP和Java時間戳存在三位位差,用000補齊
        if (String.valueOf(expire).
length() == 10) { expire = expire * 1000; } Date date = new Date(expire); SimpleDateFormat format = new SimpleDateFormat("MM月dd日"); String result = format.format(date); if (result.startsWith("0")) { result = result.substring(1); } return
result; }

以上,則可以列為常用時間轉換的工具類裡的常用方法。
至於其他的時間轉換格式方法,附上一個網路上“遍地都是”的工具類一份也方便大家使用。

// by zhenqi  http://blog.csdn.net/w630886916 
public class DateUtils {  

    public static String getTodayDateTime() {  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",  
                Locale.getDefault());  
        return
format.format(new Date()); } /** * 掉此方法輸入所要轉換的時間輸入例如("2014年06月14日16時09分00秒")返回時間戳 * * @param time * @return */ public String data(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒", Locale.CHINA); Date date; String times = null; try { date = sdr.parse(time); long l = date.getTime(); String stf = String.valueOf(l); times = stf.substring(0, 10); } catch (Exception e) { e.printStackTrace(); } return times; } public static String getTodayDateTimes() { SimpleDateFormat format = new SimpleDateFormat("MM月dd日", Locale.getDefault()); return format.format(new Date()); } /** * 獲取當前時間 * * @return */ public static String getCurrentTime_Today() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); return sdf.format(new java.util.Date()); } /** * 調此方法輸入所要轉換的時間輸入例如("2014-06-14-16-09-00")返回時間戳 * * @param time * @return */ public static String dataOne(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.CHINA); Date date; String times = null; try { date = sdr.parse(time); long l = date.getTime(); String stf = String.valueOf(l); times = stf.substring(0, 10); } catch (Exception e) { e.printStackTrace(); } return times; } public static String getTimestamp(String time, String type) { SimpleDateFormat sdr = new SimpleDateFormat(type, Locale.CHINA); Date date; String times = null; try { date = sdr.parse(time); long l = date.getTime(); String stf = String.valueOf(l); times = stf.substring(0, 10); } catch (Exception e) { e.printStackTrace(); } return times; } /** * 呼叫此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出("2014年06月14日16時09分00秒") * * @param time * @return */ public static String times(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } /** * 呼叫此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出("2014-06-14 16:09:00") * * @param time * @return */ public static String timedate(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } /** * 呼叫此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出("2014年06月14日16:09") * * @param time * @return */ public static String timet(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日 HH:mm"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } /** * @param time斜槓分開 * @return */ public static String timeslash(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy/MM/dd,HH:mm"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } /** * @param time斜槓分開 * @return */ public static String timeslashData(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy/MM/dd"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); // int i = Integer.parseInt(time); String times = sdr.format(new Date(lcc * 1000L)); return times; } /** * @param time斜槓分開 * @return */ public static String timeMinute(String time) { SimpleDateFormat sdr = new SimpleDateFormat("HH:mm"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } public static String tim(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyyMMdd HH:mm"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } public static String time(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } // 呼叫此方法輸入所要轉換的時間戳例如(1402733340)輸出("2014年06月14日16時09分00秒") public static String times(long timeStamp) { SimpleDateFormat sdr = new SimpleDateFormat("MM月dd日 # HH:mm"); return sdr.format(new Date(timeStamp)).replaceAll("#", getWeek(timeStamp)); } private static String getWeek(long timeStamp) { int mydate = 0; String week = null; Calendar cd = Calendar.getInstance(); cd.setTime(new Date(timeStamp)); mydate = cd.get(Calendar.DAY_OF_WEEK); // 獲取指定日期轉換成星期幾 if (mydate == 1) { week = "週日"; } else if (mydate == 2) { week = "週一"; } else if (mydate == 3) { week = "週二"; } else if (mydate == 4) { week = "週三"; } else if (mydate == 5) { week = "週四"; } else if (mydate == 6) { week = "週五"; } else if (mydate == 7) { week = "週六"; } return week; } // 並用分割符把時間分成時間陣列 /** * 呼叫此方法輸入所要轉換的時間戳輸入例如(1402733340)輸出("2014-06-14-16-09-00") * * @param time * @return */ public String timesOne(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } public static String timesTwo(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); return times; } /** * 並用分割符把時間分成時間陣列 * * @param time * @return */ public static String[] timestamp(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒"); @SuppressWarnings("unused") long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); String[] fenge = times.split("[年月日時分秒]"); return fenge; } /** * 根據傳遞的型別格式化時間 * * @param str * @param type * 例如:yy-MM-dd * @return */ public static String getDateTimeByMillisecond(String str, String type) { Date date = new Date(Long.valueOf(str)); SimpleDateFormat format = new SimpleDateFormat(type); String time = format.format(date); return time; } /** * 分割符把時間分成時間陣列 * * @param time * @return */ public String[] division(String time) { String[] fenge = time.split("[年月日時分秒]"); return fenge; } /** * 輸入時間戳變星期 * * @param time * @return */ public static String changeweek(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒"); long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); Date date = null; int mydate = 0; String week = null; try { date = sdr.parse(times); Calendar cd = Calendar.getInstance(); cd.setTime(date); mydate = cd.get(Calendar.DAY_OF_WEEK); // 獲取指定日期轉換成星期幾 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if (mydate == 1) { week = "星期日"; } else if (mydate == 2) { week = "星期一"; } else if (mydate == 3) { week = "星期二"; } else if (mydate == 4) { week = "星期三"; } else if (mydate == 5) { week = "星期四"; } else if (mydate == 6) { week = "星期五"; } else if (mydate == 7) { week = "星期六"; } return week; } /** * 獲取日期和星期 例如:2014-11-13 11:00 星期一 * * @param time * @param type * @return */ public static String getDateAndWeek(String time, String type) { return getDateTimeByMillisecond(time + "000", type) + " " + changeweekOne(time); } /** * 輸入時間戳變星期 * * @param time * @return */ public static String changeweekOne(String time) { SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); long lcc = Long.valueOf(time); int i = Integer.parseInt(time); String times = sdr.format(new Date(i * 1000L)); Date date = null; int mydate = 0; String week = null; try { date = sdr.parse(times); Calendar cd = Calendar.getInstance(); cd.setTime(date); mydate = cd.get(Calendar.DAY_OF_WEEK); // 獲取指定日期轉換成星期幾 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if (mydate == 1) { week = "星期日"; } else if (mydate == 2) { week = "星期一"; } else if (mydate == 3) { week = "星期二"; } else if (mydate == 4) { week = "星期三"; } else if (mydate == 5) { week = "星期四"; } else if (mydate == 6) { week = "星期五"; } else if (mydate == 7) { week = "星期六"; } return week; } /** * 獲取當前時間 * * @return */ public static String getCurrentTime() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); return sdf.format(new java.util.Date()); } /** * 輸入日期如(2014年06月14日16時09分00秒)返回(星期數) * * @param time * @return */ public String week(String time) { Date date = null; SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒"); int mydate = 0; String week = null; try { date = sdr.parse(time); Calendar cd = Calendar.getInstance(); cd.setTime(date); mydate = cd.get(Calendar.DAY_OF_WEEK); // 獲取指定日期轉換成星期幾 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if (mydate == 1) { week = "星期日"; } else if (mydate == 2) { week = "星期一"; } else if (mydate == 3) { week = "星期二"; } else if (mydate == 4) { week = "星期三"; } else if (mydate == 5) { week = "星期四"; } else if (mydate == 6) { week = "星期五"; } else if (mydate == 7) { week = "星期六"; } return week; } /** * 輸入日期如(2014-06-14-16-09-00)返回(星期數) * * @param time * @return */ public String weekOne(String time) { Date date = null; SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); int mydate = 0; String week = null; try { date = sdr.parse(time); Calendar cd = Calendar.getInstance(); cd.setTime(date); mydate = cd.get(Calendar.DAY_OF_WEEK); // 獲取指定日期轉換成星期幾 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if (mydate == 1) { week = "星期日"; } else if (mydate == 2) { week = "星期一"; } else if (mydate == 3) { week = "星期二"; } else if (mydate == 4) { week = "星期三"; } else if (mydate == 5) { week = "星期四"; } else if (mydate == 6) { week = "星期五"; } else if (mydate == 7) { week = "星期六"; } return week; } }

Demo就不寫了,直接建立類,貼上複製即可。