[Java SE] Three types of data conversion of string, timestamp, and Date

Three types of data conversion of string, timestamp, and Date in Java


  • 时间戳Convert to字符串
// 时间戳 --> 字符串
String times = "18461145832";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
String time = sdf.format(Long.parseLong(times));
System.out.println(time);
1970/08/03 00:05

  • 字符串Type conversion toDate类型
// str --> data
String str = "12/29/2000 0:36";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy H:mm");
Date date = sdf.parse(str);
System.out.println(date);
Fri Dec 29 00:36:00 CST 2000

  • Date类型转Replace with字符串
// data -->str
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
String time = sdf.format(date);
System.out.println(time);
2021/03/15 18:05

  • Date类型转Convert to时间戳
// date --> 时间戳
Date date = new Date();
long l = date.getTime();
System.out.println(l);
1615802721784

Reference: Three types of conversion of string, timestamp, and Date in Java


Guess you like

Origin blog.csdn.net/qq_45797116/article/details/114842735