java-time

Persist Java 8 LocalTime in JPA

五迷三道 提交于 2019-12-01 09:08:19
I have a Java 8 LocalTime in one of my entity . private final LocalTime departureTime; It is a Spring Boot 1.3.6 application with Spring Data Rest . I use Jsr310JpaConverters to support Java 8 time module for JPA . When I save the LocalTime variable to MySql , date at which the LocalTime variable is saved is also being persisted to the database . Suppose if I save 18:00:00 on 1st Jan 2016 , it is persisted as 2016-01-01 18:00:00 . I want to save only the time . ie 18:00:00 . Any solutions ? Thanks in Advance Davijr This exemple runs with: java 8, JPA 2.1, Hibernate 4.3.5. You can implement a

How to set precision of LocalDateTime to nanoseconds in Java.time?

怎甘沉沦 提交于 2019-12-01 08:59:15
问题 According to the java.time documentation, java.time should be able to present a LocalDateTime or LocalTime with nanoseconds precision, but when I run LocalDateTime.now() and print out, it only shows 3 digits instead of 9. Like this: 2016-08-11T22:17:35.031 Is there a way to get a higher precision? 回答1: I am assuming you are just using LocalDateTime.toString() , in which case the documentation reads: The format used will be the shortest that outputs the full value of the time where the omitted

DateTimeFormatter week-based-year diff

僤鯓⒐⒋嵵緔 提交于 2019-12-01 08:54:34
问题 I'm migrating my application from Joda-Time to the Java 8 java.time . One of the things I ran into is the printing of a week-based-year using a pattern in the DateTimeFormatter. NOTE: I have seen this question: Java Time's week-of-week-based-year pattern parsing with DateTimeFormatter According to the documentation y year-of-era year 2004; 04 Y week-based-year year 1996; 96 Yet when I try these two it seems the Y is always returning the same as y . My testing code: DateTimeFormatter yearF =

Java how to get list of time zone ID’s for the given time zone abbreviation

China☆狼群 提交于 2019-12-01 07:55:04
问题 Is it possible to find the list of time zone ID's for a given time zone abbreviation? For example, for the abbreviation IST , the time zone ID's are Asia/Jerusalem , Asia/Kolkata and Europe/Dublin . 回答1: Interesting question. Since the abbreviations aren’t standardized, there cannot be an authoritative answer nor a bulletproof way to get such an answer. Off the top of my head I thought of two approaches: Get them from your JVM. Find them on the net. Getting the zones from your JVM: String

“plusDays” doesn't advance LocalDate in Java 8

给你一囗甜甜゛ 提交于 2019-12-01 07:45:39
问题 Why is LocalDate not changing even though there is no error during running? DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDate date = LocalDate.parse("2005-12-12", formatter); date.plusDays(3); System.out.println(date.toString()); Output: 2005-12-12 Anything I missed out? 回答1: LocalDate is immutable date = date.plusDays(3); 回答2: As a String , it doesn't have an effect calling a method on it without assigning the result : date = date.plusDays(3); Read More 来源:

Parsing 2-digit years: Setting the pivot date with an unknown date pattern

人走茶凉 提交于 2019-12-01 07:38:11
问题 The user will input dates in different patterns to my application. For 2-digit years, he must also determine the pivot date. Example: pattern = yy-MM-dd pivot date = 70 (I add the current millennium and the last century for more dynamics programmatically => 1970 ) 69-04-22 becomes 2069-04-22 70-04-22 becomes 1970-04-22 Like described in this answer it's easy to build a DateTimeFormatter when the date pattern (here ddMMyy ) is known. But since the user inputs the pattern, I'm not sure how to

Parsing with Java 8 DateTimeFormatter and Spanish month names

非 Y 不嫁゛ 提交于 2019-12-01 07:37:08
With the 'old', pre-Java 8 SimpleDateFormat I can do: new java.text.SimpleDateFormat("MMM yyyy", new java.util.Locale("es", "ES")).parse("Mayo 2017") to get the Date object of a date with Spanish month names. How can I achieve the same with Java 8 and DateTimeFormatter ? I tried: DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(new Locale("es", "ES")).ofPattern("MMM yyyy").parse("Mayo 2017") but only get a java.time.format.DateTimeParseException . The call to ofLocalizedDateTime() can be removed, because in the end you call ofPattern() , creating another formatter with a

Issue with DateTimeParseException when using STRICT resolver style

百般思念 提交于 2019-12-01 07:24:55
问题 I am trying to parse a date string using the following pattern: yyMMdd and the STRICT resolver as follows: DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat).withResolverStyle(ResolverStyle.STRICT); LocalDate.parse(expiryDate, formatter); I get the following DateTimeParseException : java.time.format.DateTimeParseException: Text '160501' could not be parsed: Unable to obtain LocalDate from TemporalAccessor: {YearOfEra=2016, MonthOfYear=5, DayOfMonth=1},ISO of type java.time

Java: DateTimeFormatter fail to parse time string when seconds and milliseconds are all 0s?

老子叫甜甜 提交于 2019-12-01 07:18:46
问题 Basically, I am using the following code to parse string as LocalDateTime, which works fine most of the time. DateTimeFormatter dtformatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"); However, I encounter cases where the seconds and millseconds are 00000 and this is when the parser fails and print a LocalDateTime 2018-03-01T09:16 instead of 2018-03-01T09:16:00.000 . System.out.println(LocalDateTime.parse("20180301091600000",dtformatter)); (Note that in my code, I have to parse string

Persist Java 8 LocalTime in JPA

China☆狼群 提交于 2019-12-01 06:25:51
问题 I have a Java 8 LocalTime in one of my entity . private final LocalTime departureTime; It is a Spring Boot 1.3.6 application with Spring Data Rest . I use Jsr310JpaConverters to support Java 8 time module for JPA . When I save the LocalTime variable to MySql , date at which the LocalTime variable is saved is also being persisted to the database . Suppose if I save 18:00:00 on 1st Jan 2016 , it is persisted as 2016-01-01 18:00:00 . I want to save only the time . ie 18:00:00 . Any solutions ?