java-time

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

拥有回忆 提交于 2019-11-27 04:57:47
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' ConstantesFechas.FORMATO_DIA is 'dd/MM/yyyy' obtenerZonaHorariaServidor returns ZoneId.systemDefault() So

Find next occurrence of a day-of-week in JSR-310

柔情痞子 提交于 2019-11-27 04:45:19
问题 Given a JSR-310 object, such as LocalDate , how can I find the date of next Wednesday (or any other day-of-week? LocalDate today = LocalDate.now(); LocalDate nextWed = ??? 回答1: The answer depends on your definition of "next Wednesday" ;-) JSR-310 provides two options using the TemporalAdjusters class. The first option is next(): LocalDate input = LocalDate.now(); LocalDate nextWed = input.with(TemporalAdjusters.next(DayOfWeek.WEDNESDAY)); The second option is nextOrSame(): LocalDate input =

Mocking time in Java 8's java.time API

﹥>﹥吖頭↗ 提交于 2019-11-27 03:54:52
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 ? 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 { private Clock clock; // dependency inject ... public void process(LocalDate eventDate) { if (eventDate.isBefore

Java 8 Date equivalent to Joda's DateTimeFormatterBuilder with multiple parser formats?

蓝咒 提交于 2019-11-27 03:08:11
问题 I currently have a Joda date parser that uses the DateTimeFormatterBuilder with half a dozen different date formats that I may receive. I'm migrating to Java 8's Date routines and don't see an equivalent. How can I do something like this using Java 8 Dates? DateTimeParser[] parsers = { DateTimeFormat.forPattern( "yyyy/MM/dd HH:mm:ss.SSSSSS" ).getParser() , DateTimeFormat.forPattern( "yyyy-MM-dd HH:mm:ss" ).getParser() , DateTimeFormat.forPattern( "ddMMMyyyy:HH:mm:ss.SSS Z" ).getParser() ,

Is `Locale` needed for parsing date-time strings in Java?

十年热恋 提交于 2019-11-27 02:19:33
问题 Under what conditions do I need a Locale for parsing date-time strings in Java? What does Locale have to do with time zone? Sometimes I see Questions & Answers where the Locale was needed for the solution to a parsing problem. Yet in others there is no mention of Locale. 回答1: Locale & Time Zone are unrelated Locale and time zone are separate, orthogonal issues with respect to date-time handling. Locale Language Human language, such as Arabic, French, Farsi. Text of the names of day-of-week,

Can JAXB handle java.time objects?

左心房为你撑大大i 提交于 2019-11-27 02:10:46
问题 I know JAXB (Java Architecture for XML Binding) can marshal/unmarshal java.util.Date objects as seen in this answer by Blaise Doughan. But what about the new java.time package objects in Java 8, such as ZonedDateTime? Has JAXB been updated to handle this newly built-in data type? 回答1: In Java SE 8, JAXB has not been updated yet to support the java.time types. Indeed, there is an issue related to this in the reference implementation. You need to create and use an XmlAdapter to handle those

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

a 夏天 提交于 2019-11-27 02:03:33
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 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 the week in question straddles year boundary. See http://en.wikipedia.org/wiki/ISO_8601#Week_dates Each field

Formatting a Duration in Java 8 / jsr310

↘锁芯ラ 提交于 2019-11-27 02:03:27
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? There is no period/duration-formatter in jsr-310, different from JodaTime. Not every feature of JodaTime was ported to JSR-310

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

北城以北 提交于 2019-11-27 01:23:28
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); System.out.println(duration.get(ChronoUnit.MINUTES)); This raises an UnsupportedTemporalTypeException :

How to get a java.time object from a java.sql.Timestamp without a JDBC 4.2 driver?

China☆狼群 提交于 2019-11-27 01:16:13
问题 When retrieving a java.sql.Timestamp from a database via JDBC 4.1 or earlier, how does one obtain/convert to a java.time object? Neither of the open-source JDBC drivers for Postgres is JDBC 4.2 compliant yet, so I'm looking for a way to use use java.time with JDBC 4.1. 回答1: New Methods On Old Classes By using the driver with Java 8 and later, you should automatically pick up some methods on your java.sql.Timestamp object for free. Both java.sql.Time and java.sql.Date have similar conversion