天天看点

java js 字符串String时间日期Date时间戳互转 格式化时间日期Java js 时间格式互转一.Java二.js

Java js 时间格式互转

tip: 为做到方便套用,上下代码未关联;以下导入的包都是常规jar包,不展示导入的jar包;为保持简洁不做控制台打印

一.Java

1>创建时间

1.1>system获取时间(毫秒数)

1.2>Date获取时间

Date date = new Date();
           

1.3>Calendar获取时间

1.4>创建时间戳对象

1.4.1>通过Date创建

Timestamp timestamp = new Timestamp(new Date().getTime());
           

1.4.2>通过毫秒数创建

1.4.3>通过Calendar创建

2>时间转字符串

2.1>通过Date转

String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
           

2.2>通过微秒数转

String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());
           

2.3>通过Calendar转

2.4>Date转毫秒数

String ateStr = new Date().getTime() + "";
           

3>字符串、毫秒数转Date

3.1>时间格式字符串转Date

try {
    Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2018-01-26 10:04:01");
} catch (ParseException e) {
    e.printStackTrace();
}
           

3.2>毫秒数字符串转Date

Date date = new Date();
date.setTime(Long.parseLong("1516959645037"));
           

3.3>毫秒数(Long型)转Date

Date date = new Date();
date.setTime();
           

4>时间加减

4.1>通过Calendar

Calendar calendar = Calendar.getInstance();
// 可以设置年月日时分秒
calendar.set(, , , , , );
// key值可设置为年月日时分秒,还有其他月份以及周几
calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + ); // 一天后
           

4.2>通过GregorianCalendar

GregorianCalendar gregorianCalendar = new GregorianCalendar();
gregorianCalendar.setTime(new Date()); 
// add后第一个参数,取1一年,取2一月,取3一周,取5加一天,取9半天,取10一小时,取12一分钟,取13一秒
// add后第二个参数,为正则往后,为负则往前
gregorianCalendar.add(, ); // 2年后
           

4.3>通过Date

Date d = new Date(); 
d.setTime(d.getTime() +  *  *  *  * ); // 一天后
           

4.4>通过毫秒数

String longStr = "1526375153796";
Date date = new Date();
date.setTime(Long.parseLong(longStr) +  *  *  *  * ); // 一天后
           

二.js

1>创建时间

1.1>当前日期和时间

var today = new Date();
           

1.2>年月日时分秒

var time1 = new Date("Tue May 18 2018 17:42:20");
var time2 = new Date(,,,,,);
var time3 = new Date("2018,8,18,8,18,8");
           

1.3>年月日时分秒

var time1 = new Date("Tue May 18 2018");
var time2 = new Date(,,);
var time3 = new Date("2018,8,18");
var time4 = new Date("8/18/2018");
           

1.4>通过毫秒得到

var ms = ;
var newTime = new Date(ms);
           

2>格式化时间(包含时间加减)

function formatDate(now) {
    var year = now.getFullYear(); // 年    
    var month = now.getMonth() + ; // 月
    month = formatDateBasic(month);
    var date = now.getDate(); // 日
    date = formatDateBasic(date);
    var hour = now.getHours(); // 时
    hour = formatDateBasic(hour);
    var minute = now.getMinutes(); // 分
    minute = formatDateBasic(minute);
    var second = now.getSeconds(); // 秒
    second = formatDateBasic(second);
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;     
    // 上述获取的年月日时分秒即可进行时间加减操作,在用1>创建时间中的方法就可以得到加减后的时间
} 
// 格式数据
function formatDateBasic(data) {
    return (data >=  && data <= ) ? ("0" + data) : data;
}
           

后言: 现在关于java时间日期的教程太多,五花八门,现自己整理一下,供参考,如有意见建议请提出!!!