java-time

Count days between two dates with Java 8 while ignoring certain days of week

旧巷老猫 提交于 2019-12-03 08:11:58
Below I have 3 methods. The first is very simple. It just counts the total number of days. The second, however, will not only count the days, but will ignore the days of the week that are passed in to the method. My problem is that the third method is not always correct. It should match the second method. I am guessing it has something to do with leap years, because the difference is usually +=3|4 when it is incorrect. Additional Info I am attempting to mock Excel's weekday(serial_number,[return_type]) formula in a way. serial_number = startDate:Date - daysOfWeekToInclude:Array<Integer>

Java 8 java.time: Adding TemporalUnit in Instant vs LocalDateTime

江枫思渺然 提交于 2019-12-03 06:44:27
问题 I'm playing around with the new java.time package in Java 8. I have a legacy database that gives me java.util.Date , which I convert to Instant . What I am trying to do is add a period of time that is based off of another database flag. I could be adding days, weeks, months, or years. I don't want to have to care what I am adding, and I would like to be able to add more options in the future. My first thought was Instant.plus() , but that gives me an UnsupportedTemporalTypeException for

How to use LocalDateTime RequestParam in Spring? I get “Failed to convert String to LocalDateTime”

非 Y 不嫁゛ 提交于 2019-12-03 06:30:33
问题 I use Spring Boot and included jackson-datatype-jsr310 with Maven: <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.7.3</version> </dependency> When I try to use a RequestParam with a Java 8 Date/Time type, @GetMapping("/test") public Page<User> get( @RequestParam(value = "start", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start) { //... } and test it with this URL: /test?start

How to convert a LocalDate to an Instant?

点点圈 提交于 2019-12-03 04:40:26
问题 I work with the new DateTime API of Java 8. How to convert a LocalDate to an Instant? I get an exception with LocalDate date = LocalDate.of(2012, 2, 2); Instant instant = Instant.from(date); and I don't understand why. 回答1: In order to convert it to an instant you need to have a LocalDateTime instance, e.g.: LocalDate.now().atStartOfDay().toInstant(ZoneOffset.UTC) 回答2: The Instant class represents an instantaneous point on the time-line. Conversion to and from a LocalDate requires a time-zone

LocalDateTime to ZonedDateTime

好久不见. 提交于 2019-12-03 04:14:19
问题 I have Java 8 Spring web app that will support multiple regions. I need to make calendar events for a customer location. So lets say my web and Postgres server is hosted in MST timezone (but I guess it could be anywhere if we go cloud). But the customer is in EST. So, following some best practices I read, I thought I would store all date times in UTC format. All date time fields in the database are declared as TIMESTAMP. So here is how I take a LocalDateTime and convert to UTC: ZonedDateTime

Format a date using the new date time API

安稳与你 提交于 2019-12-03 04:07:59
I was playing with the new date time API but when running this: public class Test { public static void main(String[] args){ String dateFormatted = LocalDate.now() .format(DateTimeFormatter .ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println(dateFormatted); } } It throws: Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: HourOfDay at java.time.LocalDate.get0(LocalDate.java:680) at java.time.LocalDate.getLong(LocalDate.java:659) at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:298) at java.time.format

Java 8 Time API: how to parse string of format “MM.yyyy” to LocalDate

岁酱吖の 提交于 2019-12-03 00:55:08
I'm a bit discouraged with parsing dates in Java 8 Time API . Previously I could easily write: String date = "04.2013"; DateFormat df = new SimpleDateFormat("MM.yyyy"); Date d = df.parse(date); But now if I use LocalDate and do it like this: String date = "04.2013"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.yyyy"); LocalDate ld = LocalDate.parse(date, formatter); I receive an exception: java.time.format.DateTimeParseException: Text '04' could not be parsed at index 0 java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1948) java.time.format

Why is ZoneOffset.UTC != ZoneId.of(“UTC”)?

五迷三道 提交于 2019-12-03 00:38:49
问题 Why does ZonedDateTime now = ZonedDateTime.now(); System.out.println(now.withZoneSameInstant(ZoneOffset.UTC) .equals(now.withZoneSameInstant(ZoneId.of("UTC")))); print out false ? I would expect the both ZonedDateTime instances to be equal. 回答1: The answer comes from the javadoc of ZoneId (emphasis mine) ... A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime. There are two distinct types of ID: Fixed offsets - a fully resolved offset from UTC

Extremely slow parsing of time zone with the new java.time API

こ雲淡風輕ζ 提交于 2019-12-02 21:57:31
I was just migrating a module from the old java dates to the new java.time API, and noticed a huge drop in performance. It boiled down to parsing of dates with timezone (I parse millions of them at a time). Parsing of date string without a time zone ( yyyy/MM/dd HH:mm:ss ) is fast - about 2 times faster than with the old java date, about 1.5M operations per second on my PC. However, when the pattern contains a time zone ( yyyy/MM/dd HH:mm:ss z ), the performance drops about 15 times with the new java.time API, while with the old API it is about as fast as without a time zone. See the

How to use LocalDateTime RequestParam in Spring? I get “Failed to convert String to LocalDateTime”

折月煮酒 提交于 2019-12-02 20:06:39
I use Spring Boot and included jackson-datatype-jsr310 with Maven: <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-jsr310</artifactId> <version>2.7.3</version> </dependency> When I try to use a RequestParam with a Java 8 Date/Time type, @GetMapping("/test") public Page<User> get( @RequestParam(value = "start", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start) { //... } and test it with this URL: /test?start=2016-10-8T00:00 I get the following error: { "timestamp": 1477528408379, "status": 400, "error": "Bad