public static void main(String[] args) throws ParseException {
long DAY = 1000 * 60 * 60 * 24;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date yDate = simpleDateFormat.parse("2020-06-12 23:58:33");
System.out.println(yDate.getTime() - (yDate.getTime() % DAY));
Date date = simpleDateFormat.parse("2020-06-13 00:29:38");
System.out.println(date.getTime() - date.getTime() % DAY);
}
Run Code Online (Sandbox Code Playgroud)
控制台打印:1591920000000 1591920000000
这是一个解决您的问题的示例,java.time而不是使用过时的类java.util:
public static void main(String[] args) {
// define an up-to-date(-time) formatter and specify the output language
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss",
Locale.ENGLISH);
// define two timezones to be used later
ZoneId shanghai = ZoneId.of("Asia/Shanghai");
ZoneId utc = ZoneId.of("UTC");
/*
* Parse the datetimes to a ZonedDateTime each, because timezones matter here...
* This uses two different ones in order to highlight how timezones matter here
*/
ZonedDateTime zdtShanghaiOne = LocalDateTime.parse("2020-06-12 23:58:33", dtf)
.atZone(shanghai);
ZonedDateTime zdtShanghaiTwo = LocalDateTime.parse("2020-06-13 00:29:38", dtf)
.atZone(shanghai);
ZonedDateTime zdtUtcOne = LocalDateTime.parse("2020-06-12 23:58:33", dtf)
.atZone(utc);
ZonedDateTime zdtUtcTwo = LocalDateTime.parse("2020-06-13 00:29:38", dtf)
.atZone(utc);
/*
* Print the millis as a result of subtracting the time information.
* Acutally, this way to get a datetime of the form "yyyy-MM-dd'T'00:00:00.0'Zone'"
* takes the datetime and returns the same date with a time of 0 hours, minutes, seconds...
*/
System.out.println("Shanghai millis day one:\t"
+ zdtShanghaiOne.with(LocalTime.MIN).toInstant().toEpochMilli());
System.out.println("Shanghai millis day two:\t"
+ zdtShanghaiTwo.with(LocalTime.MIN).toInstant().toEpochMilli());
System.out.println("UTC millis day one:\t\t"
+ zdtUtcOne.with(LocalTime.MIN).toInstant().toEpochMilli());
System.out.println("UTC millis day two:\t\t"
+ zdtUtcTwo.with(LocalTime.MIN).toInstant().toEpochMilli());
}
Run Code Online (Sandbox Code Playgroud)
这导致输出
public static void main(String[] args) {
// define an up-to-date(-time) formatter and specify the output language
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss",
Locale.ENGLISH);
// define two timezones to be used later
ZoneId shanghai = ZoneId.of("Asia/Shanghai");
ZoneId utc = ZoneId.of("UTC");
/*
* Parse the datetimes to a ZonedDateTime each, because timezones matter here...
* This uses two different ones in order to highlight how timezones matter here
*/
ZonedDateTime zdtShanghaiOne = LocalDateTime.parse("2020-06-12 23:58:33", dtf)
.atZone(shanghai);
ZonedDateTime zdtShanghaiTwo = LocalDateTime.parse("2020-06-13 00:29:38", dtf)
.atZone(shanghai);
ZonedDateTime zdtUtcOne = LocalDateTime.parse("2020-06-12 23:58:33", dtf)
.atZone(utc);
ZonedDateTime zdtUtcTwo = LocalDateTime.parse("2020-06-13 00:29:38", dtf)
.atZone(utc);
/*
* Print the millis as a result of subtracting the time information.
* Acutally, this way to get a datetime of the form "yyyy-MM-dd'T'00:00:00.0'Zone'"
* takes the datetime and returns the same date with a time of 0 hours, minutes, seconds...
*/
System.out.println("Shanghai millis day one:\t"
+ zdtShanghaiOne.with(LocalTime.MIN).toInstant().toEpochMilli());
System.out.println("Shanghai millis day two:\t"
+ zdtShanghaiTwo.with(LocalTime.MIN).toInstant().toEpochMilli());
System.out.println("UTC millis day one:\t\t"
+ zdtUtcOne.with(LocalTime.MIN).toInstant().toEpochMilli());
System.out.println("UTC millis day two:\t\t"
+ zdtUtcTwo.with(LocalTime.MIN).toInstant().toEpochMilli());
}
Run Code Online (Sandbox Code Playgroud)
真的建议使用java.time不必依赖自己的计算,这些计算可能会因缺少时区信息(编码和/或编码器)而失败。