java-time

Localize string from `MonthDay` or `YearMonth` classes in java.time? [duplicate]

自闭症网瘾萝莉.ら 提交于 2019-11-29 12:51:36
This question already has an answer here: How to format YearMonth and MonthDay depending on a Locale? 3 answers The java.time classes built into Java 8 and later offer the MonthDay and YearMonth classes. Their toString and parse methods use standard ISO 8601 formats ( --MM-DD & YYYY-MM ), which is wise. For presentation to humans, the standard formats may not be suitable. Is there anyway to generate an automatically localized string to represent the values in objects of either MonthDay or YearMonth ? For example, in the United States users might typically want MM/DD for month-day and MM/YY for

How to unify date format using DateTimeFormatter

前提是你 提交于 2019-11-29 12:14:57
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? Just to complement @Flown's answer (which works perfectly BTW), you can also use optional patterns (delimited by [] ): DateTimeFormatter parser = new DateTimeFormatterBuilder() // optional ISO8601 date/time and offset .appendOptional

How to parse ZonedDateTime with default zone?

和自甴很熟 提交于 2019-11-29 12:03:44
问题 How to parse ZoneDateTime from string that doesn't contain zone and others fields? Here is test in Spock to reproduce: import spock.lang.Specification import spock.lang.Unroll import java.time.ZoneId import java.time.ZoneOffset import java.time.ZonedDateTime import java.time.format.DateTimeFormatter @Unroll class ZonedDateTimeParsingSpec extends Specification { def "DateTimeFormatter.ISO_ZONED_DATE_TIME parsing incomplete date: #value #expected"() { expect: ZonedDateTime.parse(value,

Parse clock time in java 8

人盡茶涼 提交于 2019-11-29 11:29:50
I wonder is it possible to parse clock time hour:minute:second in Java 8? e.g. final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); final String str = "12:22:10"; final LocalDateTime dateTime = LocalDateTime.parse(str, formatter); I tried but get this exception: Exception in thread "main" java.time.format.DateTimeParseException: Text '12:22:10' could not be parsed: Unable to obtain LocalDateTime from TemporalAccessor: {},ISO resolved to 12:22:10 of type java.time.format.Parsed greg-449 Since you only have a time use the LocalTime class: DateTimeFormatter formatter =

Java Time's week-of-week-based-year pattern parsing with DateTimeFormatter

喜你入骨 提交于 2019-11-29 11:06:35
I need to output the current date in the format week-based-year - week-of-week-based-year , i.e. using the ISO week date where the week always starts on a Monday and the first week of the year is the first one that has at least four days in January (so the week with the first Thursday in January). Since 31 December 2015 was a Thursday, the Friday through Sunday, i.e. 1st through 3rd of January 2016, all belong in the 53rd week of 2015 (a "long year"), and the first week of 2016 starts on Monday, 4 January. From the DateTimeFormatter spec , I would have expected that I can just use the pattern

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

做~自己de王妃 提交于 2019-11-29 10:38:14
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? Petter Friberg 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 solution is to convert java.time.LocalDate and java.time.LocalDateTime Converting to java.util.Date

UnsupportedOperationException - Why can't you call toInstant() on a java.sql.Date?

梦想与她 提交于 2019-11-29 10:33:19
问题 The java.util.Date class has a method called toInstant() that converts the Date instance to a java.time.Instant . The java.sql.Date class extends the java.util.Date class, but when I attempt to call toInstant() on a java.sql.Date , I receive an UnsupportedOperationException . Why is toInstant() an unsupported operation on java.sql.Date ? And what is the "correct" way to convert a java.sql.Date to a java.time.Instant ? 回答1: Check the JavaDoc Since sql.Date does not have a time component, there

Deserializing LocalDateTime with Jackson JSR310 module

牧云@^-^@ 提交于 2019-11-29 09:17:47
I'm using the library described the Jackson Datatype JSR310 page but I'm still having difficulty getting it to work. I have configured the following bean: @Bean @Primary public ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new JSR310Module()); return mapper; } When I call my REST API the date format output is yyyy-MM-dd'T'HH:ss.SSSSSS , e.g. 2015-04-11T00:10:38.905847 . This gets handled by my AngularJS code just fine. When I want to submit something to the REST API the date is posted as yyyy-MM-dd'T'HH:mm:ss.SSS'Z' , e.g. 2015-04-09T08:30:00

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

孤人 提交于 2019-11-29 07:35:36
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: Unable to obtain LocalDate from TemporalAccessor: {WeekOfWeekBasedYear[WeekFields[SUNDAY,1]]=1, Year=2015}

Lenient Java 8 Date parsing

天涯浪子 提交于 2019-11-29 07:21:04
I'd like to parse '2015-10-01' with LocalDateTime . What I have to do is LocalDate localDate = LocalDate.parse('2015-10-01'); LocalDateTime localDateTime = localDateTime.of(localDate, LocalTime.MIN); But I'd like to parse it in one pass like // throws DateTimeParseException LocalDateTime date = LocalDateTime.parse('2015-10-01', DateTimeFormatter.ISO_LOCAL_DATE); Also a small difference of a string throws the exception as well. // throws DateTimeParseException LocalDate localDate = LocalDate.parse("2015-9-5", DateTimeFormatter.ISO_LOCAL_DATE); Can I parse date strings leniently with Java 8 Date