java常用类--日期和时间

来源:互联网 发布:淘宝客营销案例 编辑:程序博客网 时间:2024/06/05 18:06

java日期和时间

java.util 包提供了 Date 类来封装当前的日期和时间。 Date 类提供两个构造函数来实例化 Date 对象。

第一个构造函数使用当前日期和时间来初始化对象。

Date()

第二个构造函数接收一个参数,该参数是从1970年1月1日起的毫秒数。

Date(longmillisec)


  /**     * custom format     */    public static final String FORMAT_0 = "yyyy-MM-dd HH:mm:ss";    /**     * get current timeStamp     *     * @return     */    public static long getCurrentStamp() {        return System.currentTimeMillis();    }    /**     * get current time     *     * @return     */    public static String getCurrentDate() {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        String time = sdf.format(new Date());        return time;    }    /**     * from date to stamp     *     * @param s     * @return     * @throws ParseException     */    public static String dateToStamp(String s) throws ParseException {        String res;        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date date = simpleDateFormat.parse(s);        long ts = date.getTime();        res = String.valueOf(ts);        return res;    }    /**     * from stamp to date     *     * @param s     * @return     */    public static String stampToDate(String s) {        String res;        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        long lt = new Long(s);        Date date = new Date(lt);        res = simpleDateFormat.format(date);        return res;    }    /**     * update time     *     * @param ctime submit time     * @return     */    public static String showTime(Date ctime) {        String r = "";        if (ctime == null) return r;        long nowtimelong = System.currentTimeMillis();        long ctimelong = ctime.getTime();        long result = Math.abs(nowtimelong - ctimelong);        if (result < 60000) {// 一分钟内            long seconds = result / 1000;            if (seconds == 0) {                r = "刚刚";            } else {                r = seconds + "秒前";            }        } else if (result >= 60000 && result < 3600000) {// 一小时内            long seconds = result / 60000;            r = seconds + "分钟前";        } else if (result >= 3600000 && result < 86400000) {// 一天内            long seconds = result / 3600000;            r = seconds + "小时前";        } else if (result >= 86400000 && result < 1702967296) {// 一个月内            long seconds = result / 86400000;            r = seconds + "天前";        } else {// 日期格式            String format = "yyyy-MM-dd HH:mm:ss";            SimpleDateFormat df = new SimpleDateFormat(format);            r = df.format(ctime).toString();        }        return r;    }    /**     * The number of days to get two dates     *     * @param newTime     * @param oldTime     * @return     */    public static long getDays(String newTime, String oldTime) {        if (newTime == null || newTime.equals(""))            return 0;        if (oldTime == null || oldTime.equals(""))            return 0;        // 转换为标准时间        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");        Date newDate = null;        Date oldDate = null;        try {            newDate = myFormatter.parse(newTime);            oldDate = myFormatter.parse(oldTime);        } catch (Exception e) {        }        long day = (newDate.getTime() - oldDate.getTime()) / (24 * 60 * 60 * 1000);        return day;    }    private static long midTime;    /**     * countDownTime     *     * @param limitTimeStamp     */    public static void countDownTime(long limitTimeStamp) {        //limitTime:1 calendar        Calendar calendar = Calendar.getInstance();        calendar.set(Calendar.YEAR, 2017);        calendar.set(Calendar.MONTH, 10);// 注意月份的设置,0-11表示1-12        calendar.set(Calendar.DAY_OF_MONTH, 20);        calendar.set(Calendar.HOUR_OF_DAY, 20);        calendar.set(Calendar.MINUTE, 49);        calendar.set(Calendar.SECOND, 0);        long endTime = calendar.getTimeInMillis();        //2 limtStamp        endTime = limitTimeStamp;        Date date = new Date();        long startTime = date.getTime();        midTime = (endTime - startTime) / 1000;        final Timer timer = new Timer();        timer.schedule(new TimerTask() {            public void run() {                if (midTime > 0) {                    midTime--;                    long hh = midTime / 60 / 60 % 60;                    long mm = midTime / 60 % 60;                    long ss = midTime % 60;//                    Message message = new Message();//                    message.obj = "还剩" + hh + "小时" + mm + "分钟" + ss + "";//                    handler.sendMessage(message);                } else {//                    Message message = new Message();//                    message.obj = "还剩" + 0 + "小时" + 0 + "分钟" + 0 + "";//                    handler.sendMessage(message);                    timer.cancel();                }            }        }, 0, 1000);    }

    /*      * 将时间转换为时间戳     */        public static String dateToStamp(String s) throws ParseException{        String res;        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        Date date = simpleDateFormat.parse(s);        long ts = date.getTime();        res = String.valueOf(ts);        return res;    }
复制代码

时间戳转换为时间:

复制代码
    /*      * 将时间戳转换为时间     */    public static String stampToDate(String s){        String res;        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        long lt = new Long(s);        Date date = new Date(lt);        res = simpleDateFormat.format(date);        return res;    }
复制代码
原创粉丝点击