您当前的位置: 首页 > 

liaowenxiong

暂无认证

  • 1浏览

    0关注

    1171博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Date/Timestamp/String/LocalDate/LocalDateTime

liaowenxiong 发布时间:2022-08-14 17:07:55 ,浏览量:1

文章目录
  • String 转成 Date
  • Date 转成 String
  • String 转成 Timestamp
  • 获取系统当前的毫秒数
  • 获取系统当前的日期时间
  • 毫秒数转成 Timestamp
  • 毫秒数转成 Date
  • Timestamp 转成 String
  • Date 转成 Timestamp
  • Timestamp 转成 Date
  • java.util.Date 转成 java.sql.Date
  • 将带T的日期时间转成正常的日期时间
  • String 转成 LocalDateTime
  • LocalDateTime 转成 Date

String 转成 Date
String str = "2010/05/04 12:34:23";
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = df.parse(str);
Date 转成 String
Date date =new Date();
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String str = df.format(date);
String 转成 Timestamp
String str ="2011-05-09 11:49:45";// 字符串日期时间格式为yyyy-mm-dd hh:mm:ss
Timestamp timestamp = Timestamp.valueOf(str);
获取系统当前的毫秒数
System.currentTimeMillis()
获取系统当前的日期时间
Date date = new Date();
毫秒数转成 Timestamp
Timestamp ts = new Timestamp(System.currentTimeMillis());
毫秒数转成 Date
Date date = new Date(System.currentTimeMillis());
Timestamp 转成 String
Timestamp ts =new Timestamp(System.currentTimeMillis());
String str = ts.toString();

或者

DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Timestamp ts =new Timestamp(System.currentTimeMillis());
String str = df.format(ts);
Date 转成 Timestamp
Date date = new Date();
Timestamp ts = new Timestamp(date.getTime())
Timestamp 转成 Date
Timestamp ts =newTimestamp(System.currentTimeMillis());
Date date = ts; // Timestamp是Date的子类

或者

Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date = new Date(ts.getTime());
java.util.Date 转成 java.sql.Date
java.util.Date date1 = new java.util.Date();
java.sql.Date date2 = new  java.sql.Date(date1.getTime());// 时间会丢失
将带T的日期时间转成正常的日期时间
String str = "2022-08-13T13:25:35";
str = str.replaceAll("T", " ") + ":00"
String 转成 LocalDateTime
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String str = "2022-08-15 13:55:35";
LocalDateTime localDateTime = LocalDateTime.parse(str,df);
Systom.out.print(localDateTime);// 日期时间带T
LocalDateTime 转成 Date
Date date = Date.from(localDateTime.atZone( ZoneId.systemDefault()).toInstant());
关注
打赏
1661566967
查看更多评论
立即登录/注册

微信扫码登录

0.0503s