java-time

Formatting a Duration in Java 8 / jsr310

假如想象 提交于 2019-11-26 12:31:05
问题 I am transitioning a project from Joda-Time to java8\'s native time libraries, and I have run into a snag. I have been unable to find a formatter for Duration. I would like to have a custom String format of, for instance, HHH+MM , where a Duration of 75 hours and 15 minutes would format as \"75+15\" . This was easy to do with Joda-Time by converting to period, and using a PeriodFormatter, but I have been unable to find this type of class in Java8. Am I missing something? 回答1: There is no

DateTimeFormatter month pattern letter “L” fails

牧云@^-^@ 提交于 2019-11-26 11:26:41
问题 I noticed that java.time.format.DateTimeFormatter is not able to parse out as expected. See below: import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class Play { public static void tryParse(String d,String f) { try { LocalDate.parse(d, DateTimeFormatter.ofPattern(f)); System.out.println(\"Pass\"); } catch (Exception x) {System.out.println(\"Fail\");} } public static void main(String[] args) { tryParse(\"26-may-2015\",\"dd-L-yyyy\"); tryParse(\"26-May-2015\",\"dd-L

Unable to obtain ZonedDateTime from TemporalAccessor using DateTimeFormatter and ZonedDateTime in Java 8

夙愿已清 提交于 2019-11-26 11:24:22
问题 I recently moved to Java 8 to, hopefully, deal with local and zoned times more easily. However, I\'m facing an, in my opinion, simple problem when parsing a simple date. public static ZonedDateTime convertirAFecha(String fecha) throws Exception { DateTimeFormatter formatter = DateTimeFormatter.ofPattern( ConstantesFechas.FORMATO_DIA).withZone( obtenerZonaHorariaServidor()); ZonedDateTime resultado = ZonedDateTime.parse(fecha, formatter); return resultado; } In my case: fecha is \'15/06/2014\'

JSON Java 8 LocalDateTime format in Spring Boot

。_饼干妹妹 提交于 2019-11-26 11:16:21
I'm having a small problem with formatting a Java 8 LocalDateTime in my Spring Boot Application. With 'normal' dates I have no problem, but the LocalDateTime fields are converted to the following: "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" } } While I would like convert it to something like: "startDate": "2015-01-01" My code looks like this: @JsonFormat(pattern="yyyy-MM-dd") @DateTimeFormat(iso =

Mocking time in Java 8's java.time API

懵懂的女人 提交于 2019-11-26 10:57:06
问题 Joda Time has a nice DateTimeUtils.setCurrentMillisFixed() to mock time. It\'s very practical in tests. Is there an equivalent in Java 8\'s java.time API? 回答1: The closest thing is the Clock object. You can create a Clock object using any time you want (or from the System current time). All date.time objects have overloaded now methods that take a clock object instead for the current time. So you can use dependency injection to inject a Clock with a specific time: public class MyBean {

How can I create a Java 8 LocalDate from a long Epoch time in Milliseconds?

纵饮孤独 提交于 2019-11-26 10:16:06
问题 I have an external API that returns me dates as long s, represented as milliseconds since the beginning of the Epoch. With the old style Java API, I would simply construct a Date from it with Date myDate = new Date(startDateLong) What is the equivalent in Java 8\'s LocalDate / LocalDateTime classes? I am interested in converting the point in time represented by the long to a LocalDate in my current local timezone. 回答1: If you have the milliseconds since the Epoch and want convert them to a

JDK dateformatter parsing DayOfWeek in German locale, java8 vs java9

我是研究僧i 提交于 2019-11-26 09:56:09
问题 I have tried some code in Java 8 (1.8.0_77) and Java 9 (Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)) DateTimeFormatter dtf = DateTimeFormatter.ofPattern(\"eee\", Locale.GERMAN); DayOfWeek mo = dtf.parse(\"Mo\", DayOfWeek::from); System.out.println(\"mo = \" + mo); I am not too familiar with details of those classes, but in Java 8 this works, printing: mo = MONDAY In Java 9, however it fails Exception in thread \"main\" java.time.format.DateTimeParseException: Text \'Mo\' could

Difference between year-of-era and week-based-year?

浪子不回头ぞ 提交于 2019-11-26 09:55:45
问题 Java 8\'s DateTimeFormatter class has a method, ofPattern(String pattern) , that lets you define a format from a string of A-z , a-z letters. The examples don\'t clarify the difference between y , year-of-era and Y , week-based-year . What is it? Symbol Meaning Presentation Examples ------ ------- ------------ ------- y year-of-era year 2004; 04 Y week-based-year year 1996; 96 回答1: That's year value for "year-week" style dates, as in 2006W52. It may be off the year-of-era value by +1 or -1 if

Is there a class in java.time comparable to the Joda-Time Interval?

那年仲夏 提交于 2019-11-26 09:37:12
问题 I\'m evaluating to migrate my project from the usage of Joda-Time to the java.time package in Java 8. In Joda-Time, I heavily used the Interval class. I could not find anything like this in java.time. Is there a comparable class? 回答1: Sorry for you, there is no equivalent in JSR-310 to JodaTime-Interval-class. I have doubts if this will ever come, but project lead Stephen Colebourne considers at least to support it in the scope of his external library Threeten-Extra, see this issue. If you

Why can't I get a duration in minutes or hours in java.time?

喜你入骨 提交于 2019-11-26 09:35:19
问题 Of the Duration class in the new JSR 310 date API (java.time package) available in Java 8 and later, the javadoc says : This class models a quantity or amount of time in terms of seconds and nanoseconds. It can be accessed using other duration-based units, such as minutes and hours .In addition, the DAYS unit can be used and is treated as exactly equal to 24 hours, thus ignoring daylight savings effects. So, why does the following code crash ? Duration duration = Duration.ofSeconds(3000);