Tag: java time

java.time中是否有类可比Joda时间间隔的类?

我正在评估将我的项目从Joda-Time的使用迁移到Java 8中的java.time包 。 在Joda-Time,我大量使用了Interval类。 我在java.time中找不到像这样的东西。 有类似的课程吗?

如何将LocalDate转换为SQL Date Java?

如何将LocalDate转换为java.sql.date? 尝试: Record r = new Record(); LocalDate date = new Date(1967, 06, 22); r.setDateOfBirth(new Date(date)); 这失败了,我能find的只有乔达时间的东西。 我正在使用Java 8

Java 8时间API:如何parsing格式为“MM.yyyy”的string到LocalDate

我对Java 8时间API中的parsingdate有点不鼓励。 以前我可以很容易地写: String date = "04.2013"; DateFormat df = new SimpleDateFormat("MM.yyyy"); Date d = df.parse(date); 但是现在,如果我使用LocalDate并像这样做: String date = "04.2013"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.yyyy"); LocalDate ld = LocalDate.parse(date, formatter); 我收到一个exception: java.time.format.DateTimeParseException: Text '04' could not be parsed at index 0 java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948) java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1850) java.time.LocalDate.parse(LocalDate.java:400) java.time.LocalDate.parse(LocalDate.java:385) com.luxoft.ath.controllers.JsonController.region(JsonController.java:38) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:483) org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749) […]

使用Java8中的时区格式LocalDateTime

我有这个简单的代码: DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSSSSS Z"); LocalDateTime.now().format(FORMATTER) 然后我会得到以下例外: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds at java.time.LocalDate.get0(LocalDate.java:680) at java.time.LocalDate.getLong(LocalDate.java:659) at java.time.LocalDateTime.getLong(LocalDateTime.java:720) at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298) at java.time.format.DateTimeFormatterBuilder$OffsetIdPrinterParser.format(DateTimeFormatterBuilder.java:3315) at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2182) at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1745) at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1719) at java.time.LocalDateTime.format(LocalDateTime.java:1746) 如何解决这个问题?

JPA支持Java 8新的date和时间API

我为我的新项目使用Java 8。 我试图在java 8中使用新的date和时间api,但是我不知道JPA 2.1完全支持这个新的date和时间API或不。 请分享您的经验/意见在JPA的支持新的date和时间API在Java 8。 我可以安全地使用JPA 2.1在Java 8中使用新的date和时间api吗? 更新: 我正在使用Hibernate(4.3.5.Final)作为JPA实现。

Spring Boot中的JSON Java 8 LocalDateTime格式

我在Spring Boot应用程序中格式化Java 8 LocalDateTime时遇到了一个小问题。 与“正常”date我没有问题,但LocalDateTime字段转换为以下内容: "startDate" : { "year" : 2010, "month" : "JANUARY", "dayOfMonth" : 1, "dayOfWeek" : "FRIDAY", "dayOfYear" : 1, "monthValue" : 1, "hour" : 2, "minute" : 2, "second" : 0, "nano" : 0, "chronology" : { "id" : "ISO", "calendarType" : "iso8601" } } 虽然我想将其转换为如下所示的内容: "startDate": "2015-01-01" 我的代码如下所示: @JsonFormat(pattern="yyyy-MM-dd") @DateTimeFormat(iso […]

如何将java.sql.timestamp转换为LocalDate(java8)java.time?

在Java 8中,如何将Timestamp (在java.sql )转换为LocalDate (在java.time )?

LocalDate到java.util.Date,反之亦然最简单的转换?

有没有一种简单的方法将LocalDate (随Java 8引入)转换为java.util.Date对象? 通过“简单”,我的意思是比这更简单: Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); 这对我来说似乎有些尴尬。 既然我们只对date部分感兴趣,而且在这两个对象中都没有时区信息,为什么要明确地引入时区呢? 对于转换,午夜时间和系统默认时区应该被隐式地采用。

为什么新的Java 8 Date Time API不具有纳秒精度?

Java 8中新的Date Time API的function之一应该是纳秒精度。 但是,当我打印当前date时间到控制台像这样 DateTimeFormatter formatter = DateTimeFormatter .ofPattern("yyyy-MM-dd'T'HH:mm:ss,nnnnnnnnnZ"); System.out.println(OffsetDateTime.now().format(formatter)); 我只看到毫秒精度:2015-11-02T12:33:26,746000000 + 0100 操作系统似乎支持纳秒精度。 当我通过terminal打印当前的date时间 date -Ins 我看到2015-11-02T12:33:26,746134417 + 0100 如何在Java中获得纳秒精度? 我在Ubuntu 14.04 64位上运行Oracle Java 1.8.0_66

java 8 ZonedDateTime和OffsetDateTime有什么区别?

我已经阅读了文档,但是我仍然无法使用其中的一种: OffsetDateTime ZonedDateTime 根据文档OffsetDateTime应该用于写入数据库的date,但我不明白为什么。