java-time

Java 8 Time API: how to parse string of format “MM.yyyy” to LocalDate

自闭症网瘾萝莉.ら 提交于 2019-11-26 23:01:09
问题 I'm a bit discouraged with parsing dates in Java 8 Time API . Previously I could easily write: String date = "04.2013"; DateFormat df = new SimpleDateFormat("MM.yyyy"); Date d = df.parse(date); But now if I use LocalDate and do it like this: String date = "04.2013"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.yyyy"); LocalDate ld = LocalDate.parse(date, formatter); I receive an exception: java.time.format.DateTimeParseException: Text '04' could not be parsed at index 0 java

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

[亡魂溺海] 提交于 2019-11-26 22:31:54
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. As already mentioned, Jackson-Datatype-JSR310 provides support for Java 8 Time. Since Jackson 2.6.0 the "old" JSR310Module is deprecated. It is replaced by JavaTimeModule. Maven

How to use java.time.ZonedDateTime / LocalDateTime in p:calendar

北城以北 提交于 2019-11-26 22:17:35
问题 I had been using Joda Time for date-time manipulation in a Java EE application in which a string representation of date-time submitted by the associated client had been converted using the following conversion routine before submitting it to a database i.e. in the getAsObject() method in a JSF converter. org.joda.time.format.DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss a Z").withZone(DateTimeZone.UTC); DateTime dateTime = formatter

How to parse a UTC offset dateformat string into the resulting date separated by | symbol

随声附和 提交于 2019-11-26 22:06:27
问题 I have a very peculiar question where I am trying to parse "2019-12-25T17:00:00-05:00" such that it should give me the result DEC 12 | Thursday | 5:00pm I tried the following code by using DateTimeFormatter and LocalDate DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz", Locale.US); DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM d | E | hh:mm a", Locale.US); LocalDate date = LocalDate.parse("2019-12-25T17:00:00-05:00", inputFormatter)

Force 4-digit-year in localized strings generated from `DateTimeFormatter.ofLocalized…` in java.time

元气小坏坏 提交于 2019-11-26 22:06:16
问题 The DateTimeFormatter class in java.time offers three ofLocalized… methods for generating strings to represent values that include a year. For example, ofLocalizedDate. Locale l = Locale.US ; DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( l ); LocalDate today = LocalDate.now( ZoneId.of( "America/Chicago" ) ); String output = today.format( f ); For the locales I have seen, the year is only two digits in the shorter FormatStyle styles. How to let java

How to handle full period in java.time?

萝らか妹 提交于 2019-11-26 22:01:04
问题 The Period class in java.time handles only the date-oriented potion: years, months, days. What about the time portion: hours, minutes, seconds? How can we parse and generate string representations of full periods as defined in ISO 8601, PnYnMnDTnHnMnS ? For example, a day and a half: P1DT12H . The academic year is nine months, P9M . Every year I get two weeks and 3 days of vacation, P17D . The customer occupied the hotel room for 2 days and seventeen and a half hours, P2DT17H30M . The Period

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

柔情痞子 提交于 2019-11-26 21:44:44
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 Vehicle implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "IDX",

Parsing string to local date doesn't use desired century

吃可爱长大的小学妹 提交于 2019-11-26 21:01:45
I am using this DateTimeFormatter: DateTimeFormatter.ofPattern("ddMMYY") I want to parse the string 150790 and I got this error: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},ISO of type java.time.format.Parsed Obviously, I want to get the following TemporalAccessor : {DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990} Do you know why I got the year 2090 instead of 1990? Thanks for your help Since this question is really about new java.time -package and NOT SimpleDateFormat I will cite following relevant section : Year

Unit testing a class with a Java 8 Clock

百般思念 提交于 2019-11-26 20:15:15
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(); public void method1() { Instant now = Instant.now(clock); // Do something with 'now' } } Now, I want to

Convert between LocalDate and sql.Date [duplicate]

こ雲淡風輕ζ 提交于 2019-11-26 20:04:54
This question already has an answer here: How to convert LocalDate to SQL Date Java? 3 answers What's the correct way to convert between java.sql.Date and LocalDate (Java8)? 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(); Time zones: The LocalDate type stores no time zone information, while java.sql.Date does. Therefore, when using the above