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

1 篇文章 0 订阅
1 篇文章 0 订阅

Java js 时间格式互转


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


一.Java

1>创建时间

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

long currentTimeMillis = System.currentTimeMillis(); 

1.2>Date获取时间

Date date = new Date();

1.3>Calendar获取时间

long timeInMillis = Calendar.getInstance().getTimeInMillis();

1.4>创建时间戳对象

1.4.1>通过Date创建

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

1.4.2>通过毫秒数创建

Timestamp timestamp = new Timestamp(System.currentTimeMillis());

1.4.3>通过Calendar创建

Timestamp timestamp = new Timestamp(Calendar.getInstance().getTimeInMillis());

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转

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

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(1516959645037);

4>时间加减

4.1>通过Calendar

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

4.2>通过GregorianCalendar

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

4.3>通过Date

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

4.4>通过毫秒数

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

二.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(2018,8,18,8,18,8);
var time3 = new Date("2018,8,18,8,18,8");

1.3>年月日时分秒

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

1.4>通过毫秒得到

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

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

function formatDate(now) {
    var year = now.getFullYear(); // 年    
    var month = now.getMonth() + 1; // 月
    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 >= 0 && data <= 9) ? ("0" + data) : data;
}

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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值