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

It makes sense: your input is not really a date because it does not have a day information. You should parse it as a YearMonth and use that result if you don’t care about the day. String date = “04.2013”; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“MM.yyyy”); YearMonth ym = YearMonth.parse(date, formatter); If you do need to apply … Read more

JPA support for Java 8 new date and time API

For Hibernate 5.X just add <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-java8</artifactId> <version>${hibernate.version}</version> </dependency> and @NotNull @Column(name = “date_time”, nullable = false) protected LocalDateTime dateTime; will work without any additional effort. See https://hibernate.atlassian.net/browse/HHH-8844 UPDATE: Please have a look at Jeff Morin comment: since Hibernate 5.2.x it is enough <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.1.Final</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-…</artifactId> <version>4.3.1.RELEASE</version> </dependency> See … Read more

Java 8 – DateTimeFormatter and ISO_INSTANT issues with ZonedDateTime

The ISO_INSTANT formatter is documented here – “This is a special case formatter intended to allow a human readable form of an Instant”. As such, this formatter is intended for use with an Instant not a ZonedDateTime. Formatting When formatting, ISO_INSTANT can format any temporal object that can provide ChronoField.INSTANT_SECONDS and ChronoField.NANO_OF_SECOND. Both Instant and … Read more

Convert between LocalDate and XMLGregorianCalendar

Converting from LocalDate to XMLGregorianCalendar: LocalDate date = LocalDate.now(); GregorianCalendar gcal = GregorianCalendar.from(date.atStartOfDay(ZoneId.systemDefault())); XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal); Converting back is simpler: xcal.toGregorianCalendar().toZonedDateTime().toLocalDate();

How to create Java time instant from pattern?

The string “9999-12-31” only contains information about a date. It does not contain any information about the time-of-day or offset. As such, there is insufficient information to create an Instant. (Other date and time libraries are more lenient, but java.time avoids defaulting these values) Your first choice is to use a LocalDate instead of an … Read more

Java 8 LocalDate – How do I get all dates between two dates?

Assuming you mainly want to iterate over the date range, it would make sense to create a DateRange class that is iterable. That would allow you to write: for (LocalDate d : DateRange.between(startDate, endDate)) … Something like: public class DateRange implements Iterable<LocalDate> { private final LocalDate startDate; private final LocalDate endDate; public DateRange(LocalDate startDate, LocalDate … Read more

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

This does not work because your input (and your Formatter) do not have time zone information. A simple way is to parse your date as a LocalDate first (without time or time zone information) then create a ZonedDateTime: public static ZonedDateTime convertirAFecha(String fecha) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“dd/MM/yyyy”); LocalDate date = LocalDate.parse(fecha, formatter); ZonedDateTime resultado … Read more