java-time

How to get full name of month from date in Java 8 while formatting

≯℡__Kan透↙ 提交于 2019-12-22 07:04:32
问题 Using new Java 8 java.time API, I need to convert LocalDate and get full name of month and day. Like March (not Mar), and Monday (not Mon). Friday the 13th March should be formatted like Friday, 13 March.. not Fri, 13 Mar. 回答1: The string you are looking for is MMMM . Source: DateTimeFormatter Javadoc 回答2: tl;dr Use automatic localization. No need to specify formatting pattern. localDate.format( DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ) .withLocale( Locale.UK ) ) Monday, 23

Can I create only one static instance of DateTimeFormatter

三世轮回 提交于 2019-12-22 06:00:29
问题 Is it possible to create only one static instance of DateTimeFormatter and use it everywhere in my project, instead of creating it multiple times? public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); Can there be thread-safety issues in such cases? 回答1: Yes, it is possible. Your variable is Thread Safe because it is immutable - No other thread can change it. Other solutions are also possible in your specific case, but I am always in favour of immutable

How to get the current hour with new Date-Time API in Java 8?

情到浓时终转凉″ 提交于 2019-12-22 03:54:06
问题 Before Java 8 the common method was the Calendar API: Calendar cal = Calendar.getInstance(); int hour = cal.get(Calendar.HOUR_OF_DAY); How can I get the current hour, but using the new Date-Time API provided in Java 8 ? 回答1: This all depends, but something like... LocalTime now = LocalTime.now(); System.out.println(now.getHour()); Should work just fine... Take a closer look at LocalTime#getHour for more details 来源: https://stackoverflow.com/questions/25539891/how-to-get-the-current-hour-with

How to get next MonthDay (next Christmas) with java 8 time API?

怎甘沉沦 提交于 2019-12-21 21:33:15
问题 Let's say I want to know how many days are until Christmas with a method that works any day of any year so next Christmas may be this year or next year that I don't know if it is a leap year or not. I might calculate the next Christmas date and then calculate the days from now until then. I can represent Christmas Day as MonthDay.of(12, 25) but I can't find how that helps. I found it is easy to calculate the date of next Monday this way: ZonedDateTime nextMonday = ZonedDateTime.now() .with

Java 8 LocalDate in hibernate wrongly mapped to TIMESTAMP

左心房为你撑大大i 提交于 2019-12-21 09:33:24
问题 I use Spring boot 1.4.2, which brings hibernate 5.0.11 (JPA 2.1). I want to use the Java 8 time classes in my entities, and so included hibernate-java8 . My entity defines a LocalDate field. @Entity @Table(name = "my_alarms_timeline", indexes = {...}) public class MyAlarm { ... @Column(name = "validity_date") @NotNull private LocalDate validityDate; } I expect this to be mapped to a DATE in my H2 database. In my DB I declare this as validity_date DATE NOT NULL, . When I try to run tests, I

Converting java.time to Calendar

房东的猫 提交于 2019-12-21 07:21:29
问题 What is the simplest way of getting a Calendar object from a java.time.Instant or java.time.ZonedDateTime ? 回答1: Getting a Calendar instant from ZonedDateTime is pretty straight-forward, provided you know that there exists a GregorianCalendar#from(ZonedDateTime) method. There was a discussion in Threeten-dev mail group, about why that method is not in Calendar class. Not a very deep discussion though. However, there is no direct way to convert from an Instant to Calendar . You've to have an

Jackson: parse custom offset date time

不问归期 提交于 2019-12-20 10:47:31
问题 I have a model which has a timestamp property: class Model { @JsonProperty("timestamp") private OffsetDateTime timestamp; } The format of the timestamp is as following: 2017-09-17 13:45:42.710576+02 OffsetDateTime is unable to parse this: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type java.time.OffsetDateTime from String "2017-09-17 13:45:42.710576+02": Text '2017-09-17 13:45:42.710576+02' could not be parsed at index 10 How can I fix this? 回答1:

DateTimeParseException: Text '2019-06-07 12:18:16' could not be parsed

試著忘記壹切 提交于 2019-12-20 07:47:06
问题 I have following code to convert an Instant to String then convert it back to I String timestampString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")); LOGGER.info("timestampString: " + timestampString); Instant instant = LocalDateTime.parse(timestampString, DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss")).toInstant(ZoneOffset.UTC); it print the timestampString as: 2019-06-07 12:45:57 and failed at parse the string: java.time.format.DateTimeParseException:

How to covert Joda-Time's DateTimeFormat.forStyle() to JSR 310 JavaTime?

旧巷老猫 提交于 2019-12-19 11:27:12
问题 I working on convertion Grails Joda-Time plugin to JavaTime. And I've old Joda time code like this: def style switch (type) { case LocalTime: style = '-S' break case LocalDate: style = 'S-' break default: style = 'SS' } Locale locale = LocaleContextHolder.locale return DateTimeFormatter.ofPattern(style, locale).withResolverStyle(ResolverStyle.LENIENT) How can I conver it to JSR 310? I can't find anything similar to method forStyle(String style) which accepts style. UPD I found workaround:

Parsing with Java 8 DateTimeFormatter and Spanish month names

时光毁灭记忆、已成空白 提交于 2019-12-19 09:03:57
问题 With the 'old', pre-Java 8 SimpleDateFormat I can do: new java.text.SimpleDateFormat("MMM yyyy", new java.util.Locale("es", "ES")).parse("Mayo 2017") to get the Date object of a date with Spanish month names. How can I achieve the same with Java 8 and DateTimeFormatter ? I tried: DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL).withLocale(new Locale("es", "ES")).ofPattern("MMM yyyy").parse("Mayo 2017") but only get a java.time.format.DateTimeParseException . 回答1: The call to