Android之時間戳轉今天、明天、後天、周幾

/**
     * 用於顯示時間 
     */
public static final String TODAY = "今天";
public static final String YESTERDAY = "昨天";
public static final String TOMORROW = "明天";
public static final String BEFORE_YESTERDAY = "前天";
public static final String AFTER_TOMORROW = "後天";
    /**
     * 獲取對應時間戳轉換的今天、明天。。。。的日期
     * @param time
     * @return
     */
 public static String getToday(String time)  {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);

        Calendar cal = Calendar.getInstance();
        Date date = null;
        try{

            date = new Date(Long.parseLong(time) * 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        cal.setTime(date);

        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

               return showDateDetail(diffDay, time);

        }
        return time;
    }

  /**
     * 將日期差顯示爲今天、明天或者星期
     * @param diffDay
     * @param time
     * @return
     */
    private static String showDateDetail(int diffDay, String time){
        switch(diffDay){
            case 0:
                return TODAY;
            case 1:
                return TOMORROW;
            case 2:
                return AFTER_TOMORROW;
            case -1:
                return YESTERDAY;
            case -2:
                return BEFORE_YESTERDAY;
            default:
               return  getWeek(time);
        }
    }
/**
     * 計算周幾
     */
    public static String getWeek(String data) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");
        long lcc = Long.valueOf(data);
        int i = Integer.parseInt(data);
        String times = sdr.format(new Date(i * 1000L));
        Date date = null;
        int mydate = 0;
        String week = "";
        try {
            date = sdr.parse(times);
            Calendar cd = Calendar.getInstance();
            cd.setTime(date);
            mydate = cd.get(Calendar.DAY_OF_WEEK);
            // 獲取指定日期轉換成星期幾
        } catch (ParseException 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;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章