java常用类--日期和时间

java日期和时间

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

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

Date ( )

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

Date ( long millisec )


  /**
     * 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;
    }
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值