時間戳,時間格式,日期的轉換


    //yyyy-MM-dd HH:mm:ss轉時間戳
    public static long getStringToDate(String dateString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try {
            date = dateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date.getTime();
    }

    //時間轉換格式
    private static String formatData(String time, String format) {
        String result = time;
        try {
            SimpleDateFormat formatFrom = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            SimpleDateFormat formatTo = new SimpleDateFormat(format);
            Date resultDate = formatFrom.parse(time);
            result = formatTo.format(resultDate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    //獲取今天1分鐘 1小時 明天
    public static String getTimeStamp(String dateString) {
        long toDate = getStringToDate(dateString);
        long time = (getStringToDate(dateString) - System.currentTimeMillis()) / 1000;//轉秒 今天或明天...
        long minute = time / 60;//分鐘
        long hour = time / 60 / 60;//轉小時
        long second = time / 60 / 60 / 24;//轉天
        if (time >= 0) {
            if (second == 0) {//今天
                if (hour == 60)
                    return "1小時內";
                if (hour < 60)
                    return minute + "分鐘內";
                if (minute < 60)
                    return "1分鐘內";
                return formatData(dateString, "hh:mm");
            }
        } else {
            if (hour / 24 > 2) {//明天
                return "明天" + formatData(dateString, "hh:mm");
            }
        }
        return "1分鐘內";
    }
/**
 * 第二天早晨8點的毫秒時間戳
 * @return
 */
private Long getMillisNextEarlyMorning() {
    Calendar cal = Calendar.getInstance();
    //日期加1
    cal.add(Calendar.DAY_OF_YEAR, 1);
    //時間設定到8點整
    cal.set(Calendar.HOUR_OF_DAY, 8);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTimeInMillis();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章