字符串时间转 Long
时间: 2023-09-29 15:11:09 浏览: 80
将字符串时间转换为Long类型的时间戳可以使用Java的SimpleDateFormat类来实现。下面是一个示例代码:
```java
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) {
String dateString = "2022-01-01 12:00:00";
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = format.parse(dateString);
long timestamp = date.getTime();
System.out.println("Long类型时间戳:" + timestamp);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
```
在上述代码中,首先定义了一个字符串时间`dateString`,然后创建了一个`SimpleDateFormat`对象,并指定日期格式为"yyyy-MM-dd HH:mm:ss"。接着使用`parse`方法将字符串时间解析为Date对象,然后调用`getTime`方法获取对应的Long类型时间戳。
你可以根据自己的需求修改字符串时间的格式和具体的实现逻辑。
阅读全文
相关推荐















