java-time

Java Time API - A better way to get total elapsed time

徘徊边缘 提交于 2019-12-06 09:19:46
This is my first oportunity to play with the "new" java.time package from Java 8. I need to get the total elapsed time, something like: 1 day, 2h:3m:4s 5ms I know that have 2 TemporalAmount implementations for intervals: - Period for years, months and days - Duration for hours, minutes, seconds, milliseconds and nanoseconds There's a way to mix these two or something more straightforward than "do math"? That was the best I could do until now: (Updated with a new improved version) LocalDateTime start = LocalDateTime.now(); // Forcing a long time execution to measure LocalDateTime end = start

@DefaultValue in JAX-RS for dates: now() and MAX

微笑、不失礼 提交于 2019-12-06 08:50:56
I have a query parameter as following: @GET public Response myFunction(@QueryParam("start") final LocalDate start, @QueryParam("end") final LocalDate end) { ... } For this, I created a ParamConverter<LocalDate> which converts a string to a date, and vice versa. Now I want to use @DefaultValue annotation to declare a default value. I have two special default values: today (for start ) the maximum value/infinity (for end ) Is it possible to use the @DefaultValue annotation for this? How? Yes, @DefaultValue value can be used in this situation: @GET public Response foo(@QueryParam("start")

How to persist JSR 310 java.time.LocalDateTime in JPA with Hibernate using usertype

和自甴很熟 提交于 2019-12-06 07:21:12
问题 I'm trying to persist java.time.LocalDateTime in Java 8 in JPA with Hibernate using usertype. I've a property annotated as following. @Type(type = "org.jadira.usertype.dateandtime.threetenbp.PersistentLocalDateTime") private java.time.LocalDateTime createdDateTime; And I've added dependencies for usertype as following. compile 'org.jadira.usertype:usertype.extended:3.2.0.GA' compile 'org.threeten:threetenbp:0.9' However, I'm getting following exceptions Caused by: javax.persistence

java.time YearMonth as a type within entity

假如想象 提交于 2019-12-06 07:17:19
How do you use YearMonth within an entity? I have seen a lot of stack overflow answers about a converter? Is this still needed and if so how? @Basic(optional = false) @NotNull @Column(name = "cc_exp_date") private YearMonth ccExpDate; The exception I am getting is below. Could I just convert this into a date within the set method and convert back to YearMonth within the get method of the entity as a work around? MysqlDataTruncation: Data truncation: Incorrect date value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x06\x0C\x00\x00\x07\xE2\x02x' for column 'cc

Java8 LocalDateTime parsing error

筅森魡賤 提交于 2019-12-06 07:01:55
问题 I am trying to parse the following timestamp string 03-feb-2014 13:16:31 using java.time but it is throwing an error. Here is my code. String timestamp = "03-feb-2014 13:16:31"; DateTimeFormatter format; DateTimeFormatterBuilder formatBuilder = new DateTimeFormatterBuilder(); formatBuilder.parseCaseInsensitive(); formatBuilder.append(DateTimeFormatter.ofPattern("dd-MMM-YYYY HH:mm:ss")); format = formatBuilder.toFormatter(); LocalDateTime localdatetime = LocalDateTime.parse(timestamp, format);

How do I get POSIX time (UTC) from a serial value local date-time with the Java 8 DateTime API

泄露秘密 提交于 2019-12-06 06:19:42
I have a timestamp that is similar to POSIX Time with the sole exception that it is not reckoned in UTC . Instead, it is the number of milliseconds that have elapsed since midnight, Jan 1 1970 in a particular local time zone . In order to make this value into an Instant , I must first know its offset (in milliseconds) to UTC/GMT. So the problem is this: knowing the local time zone id, eg. "America/Chicago" and a count of milliseconds since the local Epoch, how do I make an Instant (which must be constructed with milliseconds since the POSIX Epoch)? It does not seem that any of the java.time

How to create a Bank Calendar with 30 days each month

假装没事ソ 提交于 2019-12-06 04:57:22
For calculating annuity loan with german I need a calendar where every month has exactly 30 days. Every year has 360 days. There are no leap years. The interest is allways calculated on a basis of 30 day (That's the german interest method) I'm using Java 8. What possibilities do I have with the new java.time API to accomplish my requirements? What you describe is a day count not a calendar system. A day count is a mechanism for converting the period between two dates into a year fraction. OpenGamma OG-Commons includes implementations of many day counts , including 30E/360 which may be the

How to handle all Zone Offset in one DateTimeFormater Java 8

拜拜、爱过 提交于 2019-12-06 01:50:25
I need to create a DateTimeFormatter for the following valid dates. String date1 = "2017-06-20T17:25:28"; String date2 = "2017-06-20T17:25:28.477777"; String date3 = "2017-06-20T17:25:28.477777Z"; String date4 = "2017-06-20T17:25:28.477777UTC"; String date5 = "2017-06-20T17:25:28.477777-05"; String date6 = "2017-06-20T17:25:28.477777+05"; String date7 = "2017-06-20T17:25:28.477777+05:30"; String date8 = "2017-06-20T17:25:28.477777-05:30"; String date9 = "2017-06-20T17:25:28.477777+0530"; String date10 = "2017-06-20T17:25:28.477777-0530"; I have tried the following date time formatter, but this

Convert XMLGregorianCalendar in GMT to LocalDateTime Pacific time

白昼怎懂夜的黑 提交于 2019-12-05 21:04:22
I'm trying to convert XMLGregorianCalendar which is sent in GMT/UTC format to Java 8 LocalDateTime in America/Los_Angeles timezone with no luck. Here is what I tried and couldn't get the time converted to Pacific time. //xmlDate is 2017-11-13T00:00:00Z ZonedDateTime zDateTime = xmlDate.toGregorianCalendar().toZonedDateTime().toLocalDateTime().atZone(ZoneId.of("America/Los_Angeles")); LocalDateTime localDateTime = zDateTime.toLocalDateTime(); //Expected localDateTime is 2017-11-12T16:00. But I only get 2017-11-13T00:00 What am I missing? atZone() does not do what you think it does. It merely

DateTimeFormatterBuilder with specified parseDefaulting conflicts for YEAR field

心不动则不痛 提交于 2019-12-05 17:15:51
问题 I have the following formatter: DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendPattern("yyyyMM") .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.HOUR_OF_DAY, 0) .parseDefaulting(ChronoField.DAY_OF_MONTH, 1) .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1) .parseDefaulting(ChronoField.YEAR, ZonedDateTime.now().getYear()) .toFormatter() .withZone(ZoneId.systemDefault()); I try to parse the string