java-time

java.util.Date Calculate difference in days

旧街凉风 提交于 2019-11-28 00:12:41
I tried to calculate the difference between two dates and I noticed one thing. When calculating only the days, the start of daylight saving time is included in the interval, so the result will be shorter with 1 day. To obtain accurate results, the value of hours also must be considered. For example: SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy"); Date dfrom = format.parse("03-29-2015"); Date dto = format.parse("03-30-2015"); long diff = dto.getTime() - dfrom.getTime(); System.out.println(diff); System.out.println("Days: "+diff / (24 * 60 * 60 * 1000)); System.out.println("Hours:

cannot resolve symbol 'java.time.LocalDate' error in android studio

廉价感情. 提交于 2019-11-27 23:31:29
I am trying to use java.time.LocalDate in my android app, however if I try to import it, then any class from java.time gives a cannot resolve symbol error in Android studio 1.1 The only reason I could figure out for this is that it doesn't support java 8. Is that really the case? Android API level 26 Android API level 26 gained an implementation of java.time including your LocalDate class. Earlier Android For Android <26, alternatives include: ThreeTen-Backport is a back-port of much of the java.time functionality to Java 6 & 7. ThreeTenABP adapts ThreeTen-Backport to Android. Joda-Time is

Java 8 DateTimeFormatter two digit year 18 parsed to 0018 instead of 2018?

你离开我真会死。 提交于 2019-11-27 22:18:01
问题 With Java 8, the code below parses "18" into year "0018" instead of "2018". DateTimeFormatter formatter = DateTimeFormatter.ofPattern("M/d/y"); return LocalDate.parse(date, formatter); input date is "01/05/18". 1) why the result is "0018"? Does DateTimeFormatter not follow the 80-20 rule? 2) How to control SimpleDateFormat parse to 19xx or 20xx? talked about SimpleDateFormat.set2DigitYearStart(Date) can be used to fix the year. Is there something similar to that for DateTimeFormatter ? I was

Java 8 - DateTimeFormatter and ISO_INSTANT issues with ZonedDateTime

China☆狼群 提交于 2019-11-27 20:50:53
问题 So I would expect this code to work under the new Java 8 date/time package since all it does is to convert a given ZonedDateTime to string and back using the same built-in DateTimeFormatter instance (ISO_INSTANT): ZonedDateTime now = ZonedDateTime.now(); System.out.println(ZonedDateTime.parse( now.format(DateTimeFormatter.ISO_INSTANT), DateTimeFormatter.ISO_INSTANT)); But apparently it doesn't: Exception in thread "main" java.time.format.DateTimeParseException: Text '2014-09-01T19:37:48.549Z'

Getting the date from a ResultSet for use with java.time classes

折月煮酒 提交于 2019-11-27 20:38:23
Is there anyway to get a java.time (new in Java 8) compatible time class out of a ResultSet ? I am aware you can use ResultSet 's getDate or getTimestamp but these method return java.sql.Date / java.sql.Timestamp objects which are now deprecated so it seems like bad practice to use them in order to create a ZonedDateTime or similar. Meno Hochschild Most database vendors don't support JDBC 4.2 yet. This specification says that the new java.time -types like LocalDate will/should be supported using the existing methods setObject(...) and getObject() . No explicit conversion is required and

How to get a java.time object from a java.sql.Timestamp without a JDBC 4.2 driver?

人走茶凉 提交于 2019-11-27 19:42:00
When retrieving a java.sql.Timestamp from a database via JDBC 4.1 or earlier, how does one obtain/convert to a java.time object? Neither of the open-source JDBC drivers for Postgres is JDBC 4.2 compliant yet, so I'm looking for a way to use use java.time with JDBC 4.1. pickypg New Methods On Old Classes By using the driver with Java 8 and later, you should automatically pick up some methods on your java.sql.Timestamp object for free. Both java.sql.Time and java.sql.Date have similar conversion methods. Namely, to convert from java.sql to java.time you are looking for: Timestamp::toInstant()

How can I mock java.time.LocalDate.now()

杀马特。学长 韩版系。学妹 提交于 2019-11-27 19:01:05
In my test case, I need test time sensitive method, in that method we're using java 8 class LocalDate, it is not Joda . What can I do to change time, when I'm running test assylias In your code, replace LocalDate.now() with LocalDate.now(clock); . You can then pass Clock.systemDefaultZone() for production and a fixed clock for testing. This is an example : First, inject the Clock . If you are using spring boot just do a : @Bean public Clock clock() { return Clock.systemDefaultZone(); } Second, call LocalDate.now(clock) in your code : @Component public class SomeClass{ @Autowired private Clock

What's the difference between java 8 ZonedDateTime and OffsetDateTime?

自闭症网瘾萝莉.ら 提交于 2019-11-27 16:57:30
I've read the documentation, but I still can't get when I should use one or the other: OffsetDateTime ZonedDateTime According to documentation OffsetDateTime should be used when writing date to database, but I don't get why. Q: What's the difference between java 8 ZonedDateTime and OffsetDateTime? The javadocs say this: " OffsetDateTime , ZonedDateTime and Instant all store an instant on the time-line to nanosecond precision. Instant is the simplest, simply representing the instant. OffsetDateTime adds to the instant the offset from UTC/Greenwich, which allows the local date-time to be

Grouping items by date

↘锁芯ラ 提交于 2019-11-27 16:23:38
问题 I have items in my list and the items have a field, which shows the creation date of the item. and I need to group them based on a "compression", which the user gives. The options are Day , Week , Month and Year . If the user selects day compression, I need to group my items as such that the items, which are created in the same day, will be groupped. In my example above, only item 1 and item 2 are created in the same day. The others are also groups but they will have only one item because at

Unable to obtain OffsetDateTime from TemporalAccessor

狂风中的少年 提交于 2019-11-27 15:30:45
When I do this String datum = "20130419233512"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneId.of("Europe/Berlin")); OffsetDateTime datetime = OffsetDateTime.parse(datum, formatter); I get the following exception: java.time.format.DateTimeParseException: Text '20130419233512' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1366407312},ISO,Europe/Berlin resolved to 2013-04-19T23:35:12 of type java.time.format.Parsed How can I parse my datetime string so that it is interpreted as always being from the