Java时间和时间戳的相互转换(点击查看原文

时间转换为时间戳:

按 Ctrl+C 复制代码
/* 
     * 将时间转换为时间戳
     */    
    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;
    }

 


按 Ctrl+C 复制代码

时间戳转换为时间:

按 Ctrl+C 复制代码
/* 
     * 将时间戳转换为时间
     */
    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;
    }

 


按 Ctrl+C 复制代码

 

根据第几天去算某年某月某日:

按 Ctrl+C 复制代码
formatDate(year,day){//根据第几天去算某年某月某日
	var dateStr = ‘’;
	var time = new Date (‘${year}-1-1’).getTime() +(3600*1000*24*day);
	console.log(time);
	var m = new Date(time).getMonth() + 1;
	var d = new Date(time).getDate() + 1;
	dateStr = ${year}/${m}/${d};
return dateStr;
}

 


按 Ctrl+C 复制代码

 

posted on 2017-05-11 10:54  破孩筑梦  阅读(295)  评论(0编辑  收藏  举报