我想检查一下日期是否包含二月二十九日.
private static final MonthDay LEAP = MonthDay.of(Month.FEBRUARY, 29);
Run Code Online (Sandbox Code Playgroud)
我试过了:
if (LEAP.isAfter(beginDate) && LEAP.isBefore(maturity)) {
}
Run Code Online (Sandbox Code Playgroud)
但beginDate并maturity是从类型LocalDate
这样的方法中.isAfter,并.isBefore不能使用.
例
beginDate是15.01.2012
maturity是二○一三年一月二十〇日
在此期间,2月29日存在
解
我终于找到了解决方案:
for (int i = beginDate.getYear(); i <= maturity.getYear(); i++) {
final Year year = Year.of(i);
if (year.isLeap()) {
if (beginDate.compareTo(LEAP.atYear(i)) <= 0 || maturity.compareTo(LEAP.atYear(i)) >= 0) {
minMaturity.plusDays(1);
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud) 给定一个Clock,一个Instant和当前的Thread,Java库中是否有一些(实用)方法使当前线程休眠直到给定时钟到达瞬间?
就像是
public static void sleepUntil(Instant instant, Clock clock)
throws InterruptedException;
Run Code Online (Sandbox Code Playgroud)
?
我在测试环境中需要这个,我正在使用自定义减速时钟.
我知道它很容易实现,但我更喜欢标准的解决方案(如果可用的话)(但到目前为止还没有找到).
如果无法解析简单的日期时间,为什么可以将LocalDateTime对象传递给ZonedDateTime.from()方法,如下所示?
@Test
public void test() throws Exception {
GregorianCalendar.from(ZonedDateTime.from(LocalDateTime.parse("2016-08-31T23:00:59")));
}
Run Code Online (Sandbox Code Playgroud)
结果:
java.time.DateTimeException: Unable to obtain ZonedDateTime from TemporalAccessor: 2016-08-31T23:00:59 of type java.time.LocalDateTime
at java.time.ZonedDateTime.from(ZonedDateTime.java:565)
Run Code Online (Sandbox Code Playgroud) 我得到了以下代码:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
String now = new ObjectMapper().writeValueAsString(new SomeClass(LocalDateTime.now()));
System.out.println(now);
Run Code Online (Sandbox Code Playgroud)
我得到了这个:
{ "时间":{ "小时":20, "分钟":49, "第二":42, "纳米":99000000, "DAYOFYEAR":19 "一周中的某天": "星期四", "月":"JANUARY ", "请将dayOfMonth":19, "年":2017年, "monthValue":1, "年表":{ "ID": "ISO", "calendarType": "ISO8601"}}}
我想要实现的是ISO8601中的字符串
2017-01-19T18:36:51Z
使用Java 8,下面的代码将"18"解析为年"0018"而不是"2018".
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/y");
return LocalDate.parse(date, formatter);
Run Code Online (Sandbox Code Playgroud)
输入日期为"01/05/18".
1)为什么结果是"0018"?难道DateTimeFormatter没有按照80-20规则?
2)如何控制SimpleDateFormat解析为19xx或20xx?谈到SimpleDateFormat.set2DigitYearStart(Date)可以用来修复这一年.有没有类似的东西DateTimeFormatter?
我希望"M/d/y"能解析2到4位数年份.
"M/d/yy"抛出4位数的异常并将"01/05/97"解析为"2097-01-05".理想情况下,这应解析为"1997-01-05".
"M/d/yyyy"抛出2位数年份的例外情况.
我正在尝试创建一个DateTimeFormatter匹配以下示例(它实际上稍微复杂一些,但这应该无关紧要).
20180302-17:45:21
Run Code Online (Sandbox Code Playgroud)
我写了以下内容但它导致了一个例外:
new DateTimeFormatterBuilder()
.append(DateTimeFormatter.BASIC_ISO_DATE)
.appendLiteral('-')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter()
.parse("20180302-17:45:21");
Run Code Online (Sandbox Code Playgroud)
例外是:
Exception in thread "main" java.time.format.DateTimeParseException: Text '20180302-17:45:21' could not be parsed at index 11
at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1988)
at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1816)
Run Code Online (Sandbox Code Playgroud)
它似乎是在结肠之间失败17:45,DateTimeFormatterBuilder.appendLiteral并没有提供任何线索.
如果我将文字更改为另一个字符,让我们说m,那么它工作正常:
new DateTimeFormatterBuilder()
.append(DateTimeFormatter.BASIC_ISO_DATE)
.appendLiteral('m')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter()
.parse("20180302m17:45:21");
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?假设我无法更改格式,我该如何解决?
评论表明这可能取决于版本.我正在使用JDK 9.0.1,它已在9.0.4上复制.
使用带有Jackson JSON解析后端的Spring RestControllers,前端使用AngularJS.我正在寻找一种有效的方法让Jackson序列化一个Instant作为纪元毫秒,以便随后使用JavaScript代码.(在浏览器方面,我希望通过Angular的日期过滤器提供epoch ms :{{myInstantVal | date:'short' }}为我所需的日期格式.)
在Java方面,杰克逊将使用的吸气剂就是:
public Instant getMyInstantVal() { return myInstantVal; }
Run Code Online (Sandbox Code Playgroud)
序列化不会按原样运行,因为对于Instant ,jackson-datatype-jsr310默认不返回Epoch毫秒.我看着将@JsonFormat添加到上面的getter中以将Instant变形为前端可以使用的东西,但它遇到两个问题:(1)我可以提供它的模式显然仅限于SimpleDateFormat,它不提供"epoch milliseconds"选项,以及(2)当我尝试将Instant作为格式化日期发送到浏览器时,Jackson会抛出异常,因为@JsonFormat注释需要Instants的TimeZone属性,我不想硬编码因为它会因用户而异.
到目前为止我的解决方案(并且工作正常)是使用@JsonGetter创建替换的getter ,这会导致Jackson使用此方法来序列化myInstantVal:
@JsonGetter("myInstantVal")
public long getMyInstantValEpoch() {
return myInstantVal.toEpochMilli();
}
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?或者是否有一个很好的注释,我错过了我可以放在getMyInstantVal()所以我不必创建这些额外的方法?
生成的解析器DateTimeFormatter.ofPattern表现出以下有趣的行为,这阻止了我编写模式来解析像这样的字符串20150100:
System.out.println(DateTimeFormatter.ofPattern("yyyyMM").parse("201501", YearMonth::from)); // works
System.out.println(DateTimeFormatter.ofPattern("yyyyMM'aa'").parse("201501aa", YearMonth::from)); // works
System.out.println(DateTimeFormatter.ofPattern("yyyyMM'00'").parse("20150100", YearMonth::from));
// java.time.format.DateTimeParseException: Text '20150100' could not be parsed at index 0
Run Code Online (Sandbox Code Playgroud)
我调试了代码,似乎问题是由超出字符串末尾的年份字段解析引起的(三个 y 和更多的最大宽度始终为 19)。但是,我不明白它如何在没有'00'文字的情况下适用于模式。
有没有办法解决这个问题而不必使用格式化程序构建器?
编辑:
由于下面的 Jarrod 确认它有问题,我做了更多的谷歌搜索,终于找到了错误报告:
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8031085
http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8032491
两者都只在 Java 9 中修复......
我使用Spring boot 1.4.2,它带来了hibernate 5.0.11(JPA 2.1).我想在我的实体中使用Java 8时间类,因此包括在内hibernate-java8.
我的实体定义了LocalDate字段.
@Entity
@Table(name = "my_alarms_timeline", indexes = {...})
public class MyAlarm {
...
@Column(name = "validity_date")
@NotNull
private LocalDate validityDate;
}
Run Code Online (Sandbox Code Playgroud)
我希望这会映射到H2数据库中的DATE.
在我的数据库中,我将其声明为validity_date DATE NOT NULL,.
当我尝试运行测试时,出现以下错误:
validity_date TIMESTAMP NOT NULL,
令我惊讶的是,如果我将数据库定义更改为" hibernate-java8 我得到错误"
AttributeConverter<LocalDate, java.sql.Date>
这只是前一个的反向信息.
我也试过,而不是包括LocalDateTime,使用一个,hibernate-java8但这会产生相同的错误结果.
我该怎么做,以便我的LocalDate正确映射到数据库中的DATE?
我也试过一个validity_date DATE NOT NULL,映射到TIMESTAMP 的字段,这没有问题......
我得到的LocalDateTime时间比实际时间少了两个小时。如何使用以下代码获取德国柏林的偏移时间?
LocalDateTime dateTime = LocalDateTime.ofEpochSecond(seconds, 0, ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss", Locale.ENGLISH);
Run Code Online (Sandbox Code Playgroud)