时间戳,时间格式,日期的转换


    //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();
}

 

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