SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateStr = dateformat.format(System.currentTimeMillis());
可以获取如“ 2022-09-02 23:02:17 ”这样的一个字符串,但是反过来呢?如果给我们一个“ 2022-09-02 23:02:17 ”字符串,我们能否得到当前日期对应的毫秒值呢? 答案是肯定的。
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
long time = dateformat.parse("2022-09-02 23:02:17").getTime();
System.out.println(time);
}
catch (ParseException e) {
e.printStackTrace();
}
输出:1472828537000;