date-parsing

Java 8 DateTimeFormatter parsing optional sections

£可爱£侵袭症+ 提交于 2021-02-18 22:08:00
问题 I need to parse date-times as strings coming as two different formats: 19861221235959Z 1986-12-21T23:59:59Z The following dateTimeFormatter pattern properly parses the first kind of date strings DateTimeFormatter.ofPattern ("uuuuMMddHHmmss[,S][.S]X") but fails on the second one as dashes, colons and T are not expected. My attempt was to use optional sections as follows: DateTimeFormatter.ofPattern ("uuuu[-]MM[-]dd['T']HH[:]mm[:]ss[,S][.S]X") Unexpectedly, this parses the second kind of date

Java 8 DateTimeFormatter parsing optional sections

倾然丶 夕夏残阳落幕 提交于 2021-02-18 22:04:46
问题 I need to parse date-times as strings coming as two different formats: 19861221235959Z 1986-12-21T23:59:59Z The following dateTimeFormatter pattern properly parses the first kind of date strings DateTimeFormatter.ofPattern ("uuuuMMddHHmmss[,S][.S]X") but fails on the second one as dashes, colons and T are not expected. My attempt was to use optional sections as follows: DateTimeFormatter.ofPattern ("uuuu[-]MM[-]dd['T']HH[:]mm[:]ss[,S][.S]X") Unexpectedly, this parses the second kind of date

DateTimeParseException - could not be parsed at index 0

别说谁变了你拦得住时间么 提交于 2021-02-09 09:25:57
问题 I'm trying to parse the following date "Wed, 26 Feb 2020 03:42:25 -0800" String datePattern = "EEE, dd MMM yyyy HH:mm:ss Z"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePattern); ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateStr, formatter); But i get the following error: java.time.format.DateTimeParseException: Text 'Wed, 26 Feb 2020 03:42:25 -0800' could not be parsed at index 0 Thanks 回答1: You need to provide a Locale that uses the language used by the date String

DateTimeParseException - could not be parsed at index 0

邮差的信 提交于 2021-02-09 09:20:44
问题 I'm trying to parse the following date "Wed, 26 Feb 2020 03:42:25 -0800" String datePattern = "EEE, dd MMM yyyy HH:mm:ss Z"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePattern); ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateStr, formatter); But i get the following error: java.time.format.DateTimeParseException: Text 'Wed, 26 Feb 2020 03:42:25 -0800' could not be parsed at index 0 Thanks 回答1: You need to provide a Locale that uses the language used by the date String

Error converting date with two digits for the Year field

岁酱吖の 提交于 2021-02-08 06:55:31
问题 // input format: dd/MM/yy SimpleDateFormat parser = new SimpleDateFormat("dd/MM/yy"); // output format: yyyy-MM-dd SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(formatter.format(parser.parse("12/1/20"))); // 0020-11-01 I am using the above code but it is giving me year as '0020' instead of '2020'. 回答1: Use java.time for this: public static void main(String[] args) { String dateString = "12/1/20"; LocalDate localDate = LocalDate.parse(dateString,

php date_parse(“Feb 2010”) gives day == 1

谁都会走 提交于 2021-02-07 19:54:59
问题 There is what I would call a bug in date_parse when there is no day. $d = date_parse("Feb 2010") will give $d["day"] == 1 . See the comment on this on the date_parse manual page. Any nice workaround for this problem? :-) UPDATE The date comes from published research reports. Unfortunately this means that they could look in different ways. I want to convert them to more standard ISO format when displaying the references. To help the readers I want always to include just the given fields (years

Java - How to make JSON date validation?

寵の児 提交于 2021-02-05 06:05:51
问题 I have this piece of code: @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") protected Date statusDate; But for somehow it accepts date formats like - "statusDate": "2017-13-27" or "statusDate": "201823-12-12" Is it possible to validate the format within the request (not manually)? 回答1: Well, you need to write a custom date serialisation/de-serialisation class and throw a custom exception if the date format you receive while intercepting the data is not what you'd expected.

Java - How to make JSON date validation?

霸气de小男生 提交于 2021-02-05 06:04:30
问题 I have this piece of code: @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") protected Date statusDate; But for somehow it accepts date formats like - "statusDate": "2017-13-27" or "statusDate": "201823-12-12" Is it possible to validate the format within the request (not manually)? 回答1: Well, you need to write a custom date serialisation/de-serialisation class and throw a custom exception if the date format you receive while intercepting the data is not what you'd expected.

How to process yyyy-mm and yyyy in java8 using DateTimeFormatter

社会主义新天地 提交于 2021-01-29 05:48:26
问题 Am using SimpleDateFormat to format or validate the dates, but I would like to make it thread-safe by using java 8 DateTimeFormatter. I am having trouble to achieve some requirement. My application will accept only three types of formats. "yyyy-MM-dd", "yyyy-MM", "yyyy" Existing Code gives me desired output: /*simple date format to process yyyy-MM-dd format SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd") /*simple date format to process yyyy-MM format SimpleDateFormat