Why does java.time.Clock has zone information? From the Clock you only can get an Instant when calling the instant() method – which is a time without zone info. Is the only purpose to have the zone available in the clock to e.g. create a ZonedDateTime like this? ZonedDateTime.ofInstant(clock().instant(), clock().getZone()) Wouldn’t it then make sense to have a method zonedDateTime() in
Tag: java-time
Jackson , java.time , ISO 8601 , serialize without milliseconds
I’m using Jackson 2.8 and need to communicate with an API that doesn’t allow milliseconds within ISO 8601 timestamps. The expected format is this: “2016-12-24T00:00:00Z” I’m using Jackson’s JavaTimeModule with WRITE_DATES_AS_TIMESTAMPS set to false. But this will print milliseconds. So I tried to use objectMapper.setDateFormat which didn’t change anything. My current workaround is this: I’m overriding the default serializer for
What’s the difference between ZonedDateTime and OffsetDateTime?
I’ve read the documentation, but I still can’t get when I should use one or the other: OffsetDateTime ZonedDateTime According to documentation OffsetDateTime should be used when writing date to database, but I don’t get why. Answer Q: What’s the difference between java 8 ZonedDateTime and OffsetDateTime? The javadocs say this: “OffsetDateTime, ZonedDateTime and Instant all store an instant on
JSR-310 – parsing seconds fraction with variable length
Is there a way how to create JSR-310 formatter that is able to parse both following date/times with variable length of seconds fraction? or Example code: Answer This solves the problem: The answer by JiriS is incorrect, as it uses appendValue whereas the correct way is to use DateTimeFormatterBuilder.appendFraction (which also handles the decimal point). The difference can be seen
Set the first day of the week in JavaFX’s DatePicker
Is it possible to change the first day of week on JavaFX’s DatePicker when applying Arabic locale? I need to change it from Saturday to Sunday. Answer I was able to change the first date of the week by injecting the following class within my application: Do not forget to change the default locale: EDIT: The previous solution does not
Unit testing a class with a Java 8 Clock
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
Convert java.util.Date to java.time.LocalDate
What is the best way to convert a java.util.Date object to the new JDK 8/JSR-310 java.time.LocalDate? Answer Short answer Explanation Despite its name, java.util.Date represents an instant on the time-line, not a “date”. The actual data stored within the object is a long count of milliseconds since 1970-01-01T00:00Z (midnight at the start of 1970 GMT/UTC). The equivalent class to java.util.Date
How to get last month/year in java?
How do I find out the last month and its year in Java? e.g. If today is Oct. 10 2012, the result should be Month = 9 and Year = 2012. If today is Jan. 10 2013, the result should be Month = 12 and Year = 2012. Answer Your solution is here but instead of addition you need to
How to get localized short day-in-week name (Mo/Tu/We/Th…)
Can I get localized short day-in-week name (Mo/Tu/We/Th/Fr/Sa/Su for English) in Java? Answer The best way is with java.text.DateFormatSymbols
How to round time to the nearest quarter hour in java?
Given today’s time e.g. 2:24PM, how do I get it to round to 2:30PM? Similarly if the time was 2:17PM, how do I get it to round to 2:15PM? Answer Rounding You will need to use modulo to truncate the quarter hour: As pointed out by EJP, this is also OK (replacement for the last line, only valid if the