java-time

How to unify date format using DateTimeFormatter

爷,独闯天下 提交于 2019-11-28 05:58:36
问题 I need to parse different time format into BASIC_ISO_DATE . Right now, there are 4 types of date format: 2016-10-01 (ISO_LOCAL_DATE) 2016T 201610T 2016-02-07T22:03:39.937Z (ISO 8601) Need to parse to 20161001 and print out, with default day is 01 , default month Jan . Examples: 2016T -> 20160101 201610T -> 20161001 How can I use DateTimeFormatter to achieve this? 回答1: Just to complement @Flown's answer (which works perfectly BTW), you can also use optional patterns (delimited by [] ):

Hibernate with Java 8 LocalDate & LocalDateTime in Database

假装没事ソ 提交于 2019-11-28 05:27:47
My requirement is to store all dates & date-times in UTC timezone in the database. I am using Java 8's LocalDate & LocalDateTime in my Hibernate entities. Is that correct as LocalDate & LocalDateTime doesn't have timezone associated with them? If not, should I fall back to using good old (or legacy?) Date & Timestamp ? Or should I be using Java 8's Instant ? If using Instant , will there be a possibility to store only the date part, without time? The database is MySQL & SQL Server and this is a Spring Boot application. The “Local…” types purposely have no concept of time zone. So they do not

Format LocalDateTime with Timezone in Java8

Deadly 提交于 2019-11-28 04:48:10
I have the this simple code: DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSSSSS Z"); LocalDateTime.now().format(FORMATTER) Then I will get following exception: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds at java.time.LocalDate.get0(LocalDate.java:680) at java.time.LocalDate.getLong(LocalDate.java:659) at java.time.LocalDateTime.getLong(LocalDateTime.java:720) at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298) at java.time.format.DateTimeFormatterBuilder$OffsetIdPrinterParser.format

How to format java.time.LocalDateTime and java.time.LocalDate with pattern?

大憨熊 提交于 2019-11-28 04:05:27
问题 In the following snipped the property $F is of class java.time.LocalDateTime or java.time.LocalDate . <textField pattern="EE. dd.MM.yyyy"> <reportElement...> </reportElement> <textFieldExpression><![CDATA[$F{theLocalDateTime}]]></textFieldExpression> </textField> How can I format this property with textField pattern in jasper reports? 回答1: To use the pattern attribute in current version of jasper-report for Date/Time object you need a java.util.Date class or one of it's subclasses. The

How to parse a UTC offset dateformat string into the resulting date separated by | symbol

五迷三道 提交于 2019-11-28 02:05:53
I have a very peculiar question where I am trying to parse "2019-12-25T17:00:00-05:00" such that it should give me the result DEC 12 | Thursday | 5:00pm I tried the following code by using DateTimeFormatter and LocalDate DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz", Locale.US); DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM d | E | hh:mm a", Locale.US); LocalDate date = LocalDate.parse("2019-12-25T17:00:00-05:00", inputFormatter); String formattedDate = outputFormatter.format(date); contentTextView.setText(formattedDate); but it

Force 4-digit-year in localized strings generated from `DateTimeFormatter.ofLocalized…` in java.time

♀尐吖头ヾ 提交于 2019-11-28 02:04:36
The DateTimeFormatter class in java.time offers three ofLocalized… methods for generating strings to represent values that include a year. For example, ofLocalizedDate . Locale l = Locale.US ; DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.SHORT ).withLocale( l ); LocalDate today = LocalDate.now( ZoneId.of( "America/Chicago" ) ); String output = today.format( f ); For the locales I have seen, the year is only two digits in the shorter FormatStyle styles. How to let java.time localize yet force the years to be four digits rather than two? I suspect the Answer lies in

Adding and subtracting Period from LocalDate doesn't produce the same date

与世无争的帅哥 提交于 2019-11-28 01:47:35
问题 i use java 8 LocalDate and Period classes to add and remove years, months and days. Why in some cases if add Period to date and remove the same period java 8 return another date? LocalDate date = LocalDate.of(2023, 1, 30); Period period = Period.of(6, 1, 1); System.out.println(date.plus(period).minus(period)); why the result is 2023-01-31 not 2023-01-30 回答1: Why in some cases if add Period to date and remove the sane period java 8 return another date? Because that's how calendrical arithmetic

How to parse date from string with year and week using java.time

做~自己de王妃 提交于 2019-11-28 01:03:59
问题 In old java I can do it in that way: System.out.println(new SimpleDateFormat("yyyy w", Locale.UK).parse("2015 1")); // shows Mon Dec 29 00:00:00 CET 2014 System.out.println(new SimpleDateFormat("yyyy w", Locale.US).parse("2015 1")); // shows Mon Dec 28 00:00:00 CET 2014 I would like to use java.time in Java 8. System.out.println( LocalDate.parse("2015 1", DateTimeFormatter.ofPattern("yyyy w", Locale.US))); Result: java.time.format.DateTimeParseException: Text '2015 1' could not be parsed:

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

纵然是瞬间 提交于 2019-11-28 00:53:10
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 = ??? 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 = LocalDate.now(); LocalDate nextWed = input.with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY)); The two

Get original pattern String given a JDK 8 DateTimeFormatter?

随声附和 提交于 2019-11-28 00:41:21
Related to my question here - how do I get the original pattern String given a DateTimeFormatter ? It's been asked on the mailing list and the answer is that it is not possible because the original pattern is not retained. The same thread suggests using a DateTimeFormatterBuilder which does have the information. It may not be a direct answer to your question but it may help. If you know the parameters of how it the formatter was constructed you can call the static method: DateTimeFormatterBuilder.getLocalizedDateTimePattern(FormatStyle dateStyle, FormatStyle timeStyle, Chronology chrono,