java-time

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

岁酱吖の 提交于 2019-12-17 16:32:24
问题 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? 回答1: 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

How to format YearMonth and MonthDay depending on a Locale?

ぐ巨炮叔叔 提交于 2019-12-17 13:03:47
问题 Formatting a LocalDate in Java 8 using a specific Locale can be achieved like this: DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(myLocale).format(value); DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).withLocale(myLocale).format(value); DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG).withLocale(myLocale).format(value); DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(myLocale).format(value); Assuming value = LocalDate.now() this would result into: /

LocalDateTime to java.sql.Date in java 8?

泄露秘密 提交于 2019-12-17 11:43:23
问题 How to convert LocalDateTime to java.sql.Date in java-8? My search on internet mostly give me Timestamp related code or LocalDate to java.sql.Date . I'm looking for LocalDateTime to java.sql.Date . 回答1: There is no direct correlation between LocalDateTime and java.sql.Date , since former is-a timestamp, and latter is-a Date. There is, however, a relation between LocalDate and java.sql.Date , and conversion can be done like this: LocalDate date = //your local date java.sql.Date sqlDate = java

Format LocalDateTime with Timezone in Java8

二次信任 提交于 2019-12-17 10:32:55
问题 I have the this simple code: DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss.SSSSSS Z"); LocalDateTime.now().format(FORMATTER) Then I will get following exception: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: OffsetSeconds at java.time.LocalDate.get0(LocalDate.java:680) at java.time.LocalDate.getLong(LocalDate.java:659) at java.time.LocalDateTime.getLong(LocalDateTime.java:720) at java.time.format.DateTimePrintContext.getValue

Parsing ISO_INSTANT and similar Date Time Strings

拈花ヽ惹草 提交于 2019-12-17 09:56:38
问题 I created this wonderful static method yesterday, and it worked just fine - yesterday However, today it gives me this error. I guess it is from too many 0s before the Z. Can anyone recommend how to parse in a concise way (Java 8) this type of String format date - keeping in mind that it worked yesterday too, so ISO_INSTANT is also a valid format for the String ? Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {NanoOfSecond=0, InstantSeconds=1443451604

Parsing ISO_INSTANT and similar Date Time Strings

那年仲夏 提交于 2019-12-17 09:56:14
问题 I created this wonderful static method yesterday, and it worked just fine - yesterday However, today it gives me this error. I guess it is from too many 0s before the Z. Can anyone recommend how to parse in a concise way (Java 8) this type of String format date - keeping in mind that it worked yesterday too, so ISO_INSTANT is also a valid format for the String ? Caused by: java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {NanoOfSecond=0, InstantSeconds=1443451604

How to convert LocalDate to SQL Date Java?

醉酒当歌 提交于 2019-12-17 07:15:45
问题 How do I convert a LocalDate to a java.sql.Date ? Attempt: Record r = new Record(); LocalDate date = new Date(1967, 06, 22); r.setDateOfBirth(new Date(date)); This fails (won't compile) and all I can find is Joda time stuff. I'm using Java 8 回答1: The answer is really simple; import java.sql.Date; ... LocalDate locald = LocalDate.of(1967, 06, 22); Date date = Date.valueOf(locald); // Magic happens here! r.setDateOfBirth(date); If you would want to convert it the other way around, you do it

Parsing string to local date doesn't use desired century

丶灬走出姿态 提交于 2019-12-17 05:12:32
问题 I am using this DateTimeFormatter: DateTimeFormatter.ofPattern("ddMMYY") I want to parse the string 150790 and I got this error: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},ISO of type java.time.format.Parsed Obviously, I want to get the following TemporalAccessor : {DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990} Do you know why I got the year 2090 instead of 1990? Thanks for your help 回答1: Since this question

Parsing string to local date doesn't use desired century

走远了吗. 提交于 2019-12-17 05:12:06
问题 I am using this DateTimeFormatter: DateTimeFormatter.ofPattern("ddMMYY") I want to parse the string 150790 and I got this error: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=15, MonthOfYear=7, WeekBasedYear[WeekFields[MONDAY,4]]=2090},ISO of type java.time.format.Parsed Obviously, I want to get the following TemporalAccessor : {DayOfMonth=15, MonthOfYear=7, WeekBasedYear=1990} Do you know why I got the year 2090 instead of 1990? Thanks for your help 回答1: Since this question

Calculate days between two dates in Java 8

孤街浪徒 提交于 2019-12-17 02:58:39
问题 I know there are lots of questions on SO about how to get, but I want and example using new Java 8 Date api. I also know JodaTime library, but I want a work way without external libraries. Function needs to complain with these restrictions: Prevent errors from date savetime Input are two Date's objects (without time, I know localdatetime, but I need to do with date instances) 回答1: If you want logical calendar days , use DAYS.between() method from java.time.temporal.ChronoUnit: LocalDate