java-time

Is java.time failing to parse fraction-of-second?

牧云@^-^@ 提交于 2019-12-17 02:37:54
问题 With the first release of Java 8 (b132) on Mac OS X (Mavericks), this code using the new java.time package works: String input = "20111203123456"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "yyyyMMddHHmmss"); LocalDateTime localDateTime = LocalDateTime.parse( input, formatter ); Rendering: 2011-12-03T12:34:56 But when I add "SS" for fraction-of-second (and "55" as input), as specified in the DateTimeFormatter class doc, an exception is thrown: java.time.format

Spring web: @RequestBody Json conversion for Java8 LocalTime not working

早过忘川 提交于 2019-12-14 01:08:43
问题 In my Spring REST webapp, I'm trying to get a controller working with Java8 LocalTime. I'm sending this Json in a POST request { "hour": "0", "minute": "0", "second": "0", "nano": "0" } and I get a HttpMessageNotReadableException with the following Jackson error Could not read JSON: No suitable constructor found for type [simple type, class java.time.LocalTime]: can not instantiate from JSON object (need to add/enable type information?) at [Source: org.apache.catalina.connector

Parse date-only value in ISO 8601 format in java.time (YYYY-MM-DD)

时间秒杀一切 提交于 2019-12-13 22:38:59
问题 How do I parse YYYY-MM-DD dates in modern Java? I have a Java String of a standard ISO 8601 date in the YYYY-MM-DD format, such as 2016-03-21 . How can I parse this into the modern Date-Time types in Java, the java.time classes? Note: This is intended to be a canonical post for a common question. 回答1: LocalDate.parse The java.time classes can parse ISO 8601 strings directly, with no need to specify a formatting pattern. For a date-only value, without a time of day or time zone, use the

Weekend filter for Java 8 LocalDateTime

三世轮回 提交于 2019-12-13 11:58:51
问题 I want to write a boolean valued function which returns true if the given LocalDateTime falls between two specific points in time, false otherwise. Specifically I want to have a LocalDateTime filter if a given date is between 22:00 GMT Friday and 23:00 GMT Sunday. A skeleton could look like this: public boolean isWeekend(LocalDateTime dateTime) { //Checks if dateTime falls in between Friday's 22:00 GMT and Sunday's 23:00 GMT //return ...??? } This is basically a weekend filter and I'm

Remaining days to a date is not showing correctly

大兔子大兔子 提交于 2019-12-12 18:37:48
问题 Ok, so I've created a function to show the number of days until a date in the future... It is correct until the amount of days is over 9 days.. if over it seems to show a random number of days... Please see my code below: public String daysTillExpire() { String daysLeft = ""; int position = 0 ; String inputDateString = UIDM.get(position).date; Calendar calCurr = Calendar.getInstance(); Calendar day = Calendar.getInstance(); try { day.setTime(new SimpleDateFormat("dd-MM-yyyy").parse

Java: Unable to obtain LocalDate from TemporalAccessor

烈酒焚心 提交于 2019-12-12 10:41:34
问题 I am trying to change the format of a String date from EEEE MMMM d to MM/d/yyyy by, first, converting it into a LocalDate and then applying a formatter of a different pattern to the LocalDate before parsing it into String again. Here's my code: private String convertDate(String stringDate) { //from EEEE MMMM d -> MM/dd/yyyy DateTimeFormatter formatter = new DateTimeFormatterBuilder() .parseCaseInsensitive() .append(DateTimeFormatter.ofPattern("EEEE MMMM d")) .toFormatter(); LocalDate

@DefaultValue in JAX-RS for dates: now() and MAX

半世苍凉 提交于 2019-12-12 10:07:35
问题 I have a query parameter as following: @GET public Response myFunction(@QueryParam("start") final LocalDate start, @QueryParam("end") final LocalDate end) { ... } For this, I created a ParamConverter<LocalDate> which converts a string to a date, and vice versa. Now I want to use @DefaultValue annotation to declare a default value. I have two special default values: today (for start ) the maximum value/infinity (for end ) Is it possible to use the @DefaultValue annotation for this? How? 回答1:

How to parse non-standard month names with DateTimeFormatter

二次信任 提交于 2019-12-12 07:49:18
问题 I need to parse (German) dates that come in the following form: 10. Jan. 18:14 8. Feb. 19:02 1. Mär. 19:40 4. Apr. 18:55 2. Mai 21:55 5. Juni 08:25 5. Juli 20:09 1. Aug. 13:42 [...] As you can see, the month names are cut if the month has more than 4 characters. Even weirder, don't aks me why, the month of March is shortened to Mär. although the whole name is März . How can I parse this with java.time ? (The dates are formatted based on the localization of the android device that creates the

How to apply timezone when formatting DateTime?

限于喜欢 提交于 2019-12-12 05:16:11
问题 I have a datetime as 2011-01-11 01:51:10 and timezone as America/Los_Angeles I want to get a localised date time for this value. This is what I do val formatter1: DateTimeFormatter = DateTimeFormatter.ofPattern("y-M-d H:m:s"); val m1: LocalDateTime = LocalDateTime.parse("2011-01-11 01:51:10", formatter1); println("DateTime: " + m1.atZone(ZoneId.of("America/Los_Angeles"))) The value that I get is DateTime: 2011-01-11T01:51:10-08:00[America/Los_Angeles] How do I convert it into localized

java.time.Instant.plus(long amountToAdd, TemporalUnit unit) Unsupported unit

女生的网名这么多〃 提交于 2019-12-11 17:13:32
问题 I trying to add few years to current time. My code looks like: // ten yeas ago int backYears = 10; Instant instant = ChronoUnit.YEARS.addTo(Instant.now(), -backYears); But I got an exception: java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years at java.time.Instant.plus(Instant.java:862) When I opened the method Instant.plus I see the following: @Override public Instant plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit)