是否可以使用 Java 8 将日期截断为月份?
Is it possible to truncate date to Month with Java 8?
我想将毫秒截断为几天,我可以使用
Instant.now().truncatedTo(ChronoUnit.DAYS).toEpochMilli()
但我无法截断为 ChronoUnit.MONTH
(它会引发异常)。我需要使用 a Calendar 吗?
一种方法是手动将日期设置为每月的第一天:
import static java.time.ZoneOffset.UTC;
import static java.time.temporal.ChronoUnit.DAYS;
ZonedDateTime truncatedToMonth = ZonedDateTime.now(UTC).truncatedTo(DAYS).withDayOfMonth(1);
System.out.println(truncatedToMonth); //prints 2015-06-01T00:00Z
long millis = truncatedToMonth.toInstant().toEpochMilli();
System.out.println(millis); // prints 1433116800000
或者带有 LocalDate
的替代方案,这可能更清洁:
LocalDate firstOfMonth = LocalDate.now(UTC).withDayOfMonth(1);
long millis = firstOfMonth.atStartOfDay(UTC).toEpochSecond() * 1000;
//or
long millis = firstOfMonth.atStartOfDay(UTC).toInstant().toEpochMilli();
一个简单的方法:
Calendar cal = new GregorianCalendar();
System.out.println(cal.getTime());
cal.set(Calendar.DAY_OF_MONTH,1);
System.out.println(cal.getTime());
cal.set(Calendar.HOUR_OF_DAY,0);
System.out.println(cal.getTime());
cal.set(Calendar.MINUTE,0);
System.out.println(cal.getTime());
cal.set(Calendar.SECOND,0);
System.out.println(cal.getTime());
cal.set(Calendar.MILLISECOND,0);
System.out.println(cal.getTime());
输出为:
Thu Jun 11 05:36:17 EDT 2015
Mon Jun 01 05:36:17 EDT 2015
Mon Jun 01 00:36:17 EDT 2015
Mon Jun 01 00:00:17 EDT 2015
Mon Jun 01 00:00:00 EDT 2015
Mon Jun 01 00:00:00 EDT 2015
这就是 java.time.temporal.TemporalAdjusters
的用途。
date.with(TemporalAdjusters.firstDayOfMonth()).truncatedTo(ChronoUnit.DAYS);
我在使用瞬间时遇到了同样的问题,然后下面的代码解决了我的问题:
Instant instant = Instant.ofEpochSecond(longTimestamp);
instant = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()).with(TemporalAdjusters.firstDayOfMonth())
.truncatedTo(ChronoUnit.DAYS).toInstant();
我想将毫秒截断为几天,我可以使用
Instant.now().truncatedTo(ChronoUnit.DAYS).toEpochMilli()
但我无法截断为 ChronoUnit.MONTH
(它会引发异常)。我需要使用 a Calendar 吗?
一种方法是手动将日期设置为每月的第一天:
import static java.time.ZoneOffset.UTC;
import static java.time.temporal.ChronoUnit.DAYS;
ZonedDateTime truncatedToMonth = ZonedDateTime.now(UTC).truncatedTo(DAYS).withDayOfMonth(1);
System.out.println(truncatedToMonth); //prints 2015-06-01T00:00Z
long millis = truncatedToMonth.toInstant().toEpochMilli();
System.out.println(millis); // prints 1433116800000
或者带有 LocalDate
的替代方案,这可能更清洁:
LocalDate firstOfMonth = LocalDate.now(UTC).withDayOfMonth(1);
long millis = firstOfMonth.atStartOfDay(UTC).toEpochSecond() * 1000;
//or
long millis = firstOfMonth.atStartOfDay(UTC).toInstant().toEpochMilli();
一个简单的方法:
Calendar cal = new GregorianCalendar();
System.out.println(cal.getTime());
cal.set(Calendar.DAY_OF_MONTH,1);
System.out.println(cal.getTime());
cal.set(Calendar.HOUR_OF_DAY,0);
System.out.println(cal.getTime());
cal.set(Calendar.MINUTE,0);
System.out.println(cal.getTime());
cal.set(Calendar.SECOND,0);
System.out.println(cal.getTime());
cal.set(Calendar.MILLISECOND,0);
System.out.println(cal.getTime());
输出为:
Thu Jun 11 05:36:17 EDT 2015
Mon Jun 01 05:36:17 EDT 2015
Mon Jun 01 00:36:17 EDT 2015
Mon Jun 01 00:00:17 EDT 2015
Mon Jun 01 00:00:00 EDT 2015
Mon Jun 01 00:00:00 EDT 2015
这就是 java.time.temporal.TemporalAdjusters
的用途。
date.with(TemporalAdjusters.firstDayOfMonth()).truncatedTo(ChronoUnit.DAYS);
我在使用瞬间时遇到了同样的问题,然后下面的代码解决了我的问题:
Instant instant = Instant.ofEpochSecond(longTimestamp);
instant = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault()).with(TemporalAdjusters.firstDayOfMonth())
.truncatedTo(ChronoUnit.DAYS).toInstant();