文章目录
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());