介绍
可以使用 Java 的 SimpleDateFormat
、Timestamp
和 Date
类将字符串日期转换为时间戳。本实验将介绍两种将日期字符串转换为时间戳的方法。
可以使用 Java 的 SimpleDateFormat
、Timestamp
和 Date
类将字符串日期转换为时间戳。本实验将介绍两种将日期字符串转换为时间戳的方法。
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
import java.util.Date;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// date in string format
String stringDate = "2021-01-07 02:02:16.172";
try {
// creating date format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
// parsing string to date using parse() method
Date parsedDate = dateFormat.parse(stringDate);
// finally creating a timestamp
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println(timestamp);
} catch(Exception e) {
System.out.println(e);
}
}
}
要运行此程序,请打开终端并导航到 ~/project/
目录。
这是另一种使用 java.sql.Timestamp
类的 valueOf()
方法将日期字符串转换为时间戳的方式。这是一种简单的方法,无需编写额外的代码。
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
import java.util.Date;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// date in string format
String stringDate = "2021-01-07 02:02:16.172";
try {
// converting string date to timestamp using valueOf() method
java.sql.Timestamp timestamp = java.sql.Timestamp.valueOf(stringDate);
System.out.println(timestamp);
} catch(Exception e) {
System.out.println(e);
}
}
}
打开终端并导航到 ~/project/
目录。使用以下命令编译并运行代码:
javac StringToTimestamp.java && java StringToTimestamp
如果字符串的格式不正确,你会得到一个异常错误:java.text.ParseException: Unparseable date:
。为了处理这个错误,我们可以添加一个 catch 块来显示错误信息。
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
import java.util.Date;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// date in string format
String stringDate = "2021-01-07 02:02:16";
try {
// creating date format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
// parsing string to date using parse() method
Date parsedDate = dateFormat.parse(stringDate);
// finally creating a timestamp
Timestamp timestamp = new java.sql.Timestamp(parsedDate.getTime());
System.out.println(timestamp);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
使用以下代码将 Epoch 时间转换为时间戳:
import java.sql.Timestamp;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// epoch time
long epochTime = 1609934536172L;
try {
// create Timestamp object
Timestamp timestamp = new Timestamp(epochTime);
System.out.println(timestamp);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
使用 SimpleDateFormat
将时间戳转换为字符串格式。
import java.text.SimpleDateFormat;
import java.sql.Timestamp;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// timestamp
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
try {
// create SimpleDateFormat object
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
// format Timestamp object
String formattedDate = dateFormat.format(timestamp);
// print formatted date
System.out.println(formattedDate);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
除了 SimpleDateFormat
,我们还可以使用 Calendar
类将字符串日期转换为时间戳。
import java.util.Calendar;
import java.sql.Timestamp;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// string date
String stringDate = "2021-01-07 02:02:16.172";
try {
// create Calendar object
Calendar calendar = Calendar.getInstance();
// set calendar date with string date
calendar.setTime(new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS").parse(stringDate));
// convert calendar to timestamp
java.sql.Timestamp timestamp = new java.sql.Timestamp(calendar.getTimeInMillis());
System.out.println(timestamp);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
我们也可以使用 ZonedDateTime
类将字符串日期转换为时间戳。
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.sql.Timestamp;
public class StringToTimestamp {
public static void main(String[] args) throws Exception {
// string date
String stringDate = "2021-01-07 02:02:16.172";
try {
// create DateTimeFormatter object and specify the format of the string date
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
// create ZonedDateTime object and parse the string date
ZonedDateTime zonedDateTime = ZonedDateTime.parse(stringDate, formatter.withZone(ZoneId.systemDefault()));
// convert ZonedDateTime to timestamp
Timestamp timestamp = Timestamp.valueOf(zonedDateTime.toLocalDateTime());
System.out.println(timestamp);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
打开终端并导航到 ~/project/
目录。使用以下命令编译并运行代码:
javac StringToTimestamp.java && java StringToTimestamp
在本实验中,我们学习了如何使用不同的方法将字符串日期转换为 Java 中的时间戳,包括 SimpleDateFormat
、Timestamp
、Date
、Calendar
和 ZonedDateTime
类。其中,SimpleDateFormat
和 Timestamp
方法最容易实现,而 ZonedDateTime
方法虽然最复杂,但提供了最大的灵活性。