java-time

How to persist JSR-310 types with Spring Data JPA?

ぃ、小莉子 提交于 2019-11-26 08:04:42
问题 I am trying to use Spring Data JPA 1.8 with the Java 8 Date/Time API JSR-310. Everything seems to work, until I try to get all Vehicles between two LocalDateTimes. The number of Entities returned seems to only have a loose correlation with the number it should. Entity @Repository public interface VehicleRepository extends JpaRepository<Vehicle, Long> { List<Vehicle> findByDateTimeBetween(LocalDateTime begin, LocalDateTime end); } Repository @Entity @Table(name = \"VEHICLE\") public class

Is there a jackson datatype module for JDK8 java.time?

时间秒杀一切 提交于 2019-11-26 07:41:35
问题 I\'m looking for a module for the new JDK8 java.time classes. I have looked through the FasterXML GitHub Project Listing and presently found none. As I understand Jackson is still being compiled against JDK6 so can not use these classes directly and must have this built as a separate module, as was required with Joda. I don\'t mind starting the project, though looking to see if any other efforts were already underway. 回答1: As already mentioned, Jackson-Datatype-JSR310 provides support for

LocalDate to java.util.Date and vice versa simplest conversion? [duplicate]

心已入冬 提交于 2019-11-26 06:57:58
问题 This question already has an answer here: Convert java.time.LocalDate into java.util.Date type 11 answers Is there a simple way to convert a LocalDate (introduced with Java 8) to java.util.Date object? By \'simple\', I mean simpler than this: Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); which seems a bit awkward to me. Since we are interested only in the date portion and there is no timezone information in neither of the objects, why introduce time zones

java DateTimeFormatterBuilder fails on testtime

こ雲淡風輕ζ 提交于 2019-11-26 06:49:57
问题 I have a simple jUnit test for DateTimeFormatterBuilder . At runtime it works, when some String comes on Spring-MVC hanlder ( @RequestParam ) At testtime it fails with the same String value. Tested value: 25-May-2018 11:10 Method to be tested: public void getTimeDifference(@RequestParam String startDate, @RequestParam String endDate) { DateTimeFormatter DATE_TIME_FORMAT = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern(\"dd-MMM-yyyy HH:mm\").toFormatter(); LocalDateTime

Unit testing a class with a Java 8 Clock

南楼画角 提交于 2019-11-26 06:06:36
问题 Java 8 introduced java.time.Clock which can be used as an argument to many other java.time objects, allowing you to inject a real or fake clock into them. For example, I know you can create a Clock.fixed() and then call Instant.now(clock) and it will return the fixed Instant you provided. This sounds perfect for unit testing! However, I\'m having trouble figuring out how best to use this. I have a class, similar to the following: public class MyClass { private Clock clock = Clock.systemUTC();

`uuuu` versus `yyyy` in `DateTimeFormatter` formatting pattern codes in Java?

我的梦境 提交于 2019-11-26 05:39:53
问题 The DateTimeFormatter class documentation says about its formatting codes for the year: u year year 2004; 04 y year-of-era year 2004; 04 … Year: The count of letters determines the minimum field width below which padding is used. If the count of letters is two, then a reduced two digit form is used. For printing, this outputs the rightmost two digits. For parsing, this will parse using the base value of 2000, resulting in a year within the range 2000 to 2099 inclusive. If the count of letters

Convert between LocalDate and sql.Date [duplicate]

家住魔仙堡 提交于 2019-11-26 04:45:05
问题 This question already has answers here : How to convert LocalDate to SQL Date Java? (3 answers) Closed 4 years ago . What\'s the correct way to convert between java.sql.Date and LocalDate (Java8)? 回答1: The Java 8 version (and later) of java.sql.Date has built in support for LocalDate , including toLocalDate and valueOf(LocalDate). To convert from LocalDate to java.sql.Date you can use java.sql.Date.valueOf( localDate ); And to convert from java.sql.Date to LocalDate : sqlDate.toLocalDate();

Differences between Java 8 Date Time API (java.time) and Joda-Time

房东的猫 提交于 2019-11-26 04:30:29
问题 I know there are questions relating to java.util.Date and Joda-Time. But after some digging, I couldn\'t find a thread about the differences between the java.time API (new in Java 8, defined by JSR 310) and Joda-Time. I have heard that Java 8’s java.time API is much cleaner and can do much more than Joda-Time. But I cannot find examples comparing the two. What can java.time do that Joda-Time cannot? What can java.time do better than Joda-Time? Is the performance better with java.time? 回答1:

How do I add one month to current date in Java?

不想你离开。 提交于 2019-11-26 02:57:10
问题 In Java how can I add one month to the current date? 回答1: Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, 1); 回答2: Java 8 LocalDate futureDate = LocalDate.now().plusMonths(1); 回答3: tl;dr LocalDate::plusMonths Example: LocalDate.now( ) .plusMonths( 1 ); Better to specify time zone. LocalDate.now( ZoneId.of( "America/Montreal" ) .plusMonths( 1 ); java.time The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as

JPA support for Java 8 new date and time API

只谈情不闲聊 提交于 2019-11-26 02:54:48
问题 I\'m using Java 8 for my new project. I\'m trying to use new date and time api in java 8 however I don\'t know if JPA 2.1 fully supports this new Date and Time API or not. Please share your experience/opinion in JPA`s supports for new date and time API in Java 8. Can I use new date and time api in Java 8 safely with JPA 2.1? UPDATE: I\'m using Hibernate (4.3.5.Final) as JPA implementation. 回答1: JPA 2.1 is a spec that came out before Java 1.8, so doesn't mandate any support for it. Obviously