时间格式的转换

时间格式的转换

1. Date 转 String

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

2. String 转 Date

SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = "2019-09-19 10:30:20";
Date date = ft.parse(time)

3. 将Date 转为时间戳

Date date = new Date();  
long times = date.getTime();  

4. 给当前时间加上30分钟 ,作为失效时间

//1)设置订单失效时间 //获取当前时间或者获取数据库中存储的时间 转为时间戳
 long time = iaOrder.getCreateTime().getTime();
 //2) 在以上时间的基础上添加30分钟 作为失效时间
 //表示 + 30 分钟 如果是一个小时 则为 60*60*1000  如果+1天 则 24*60*60*1000
 time +=30*60*1000;      

5. 给当前时间 + 5年 作为结束时间,如下:

//DateUtil 工具类来自   import cn.hutool.core.date.DateUtil;
//这里的5 表示5年 写死的 ,也可以动态获取
String offDateYear = DateUtil.format(DateUtil.offset(new Date(), DateField.YEAR, 5), "yyyy-MM-dd");

//工具类pom依赖
<dependency>
  <groupId>cn.hutool</groupId>
  <artifactId>hutool-all</artifactId>
  <version>5.5.7</version>
  <scope>compile</scope>
</dependency>

6. ,MySQL 在SQL中格式化时间

// 1) 转为年月日
//函数 date_format
//transfer_begin_date/transfer_end_date (数据库中字段)  
concat(date_format(transfer_begin_date,'%Y-%m-%d'),'至',date_format(transfer_end_date,'%Y-%m-%d')) as salesTime,
  
// 2) 转为 年月日时分秒
concat(date_format(a.appointment_end_time,'%Y-%m-%d %h-%m-%s')) as appointment_end_time
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值