java-time

DateTimeFormatter Support for Single Digit Day of Month and Month of Year

六眼飞鱼酱① 提交于 2019-11-27 12:55:27
问题 DateTimeFormmater doesn't seem to handle single digit day of the month: String format = "MM/dd/yyyy"; String date = "5/3/1969"; System.out.println(new SimpleDateFormat(format).parse(date)); System.out.println(LocalDate.parse(date, DateTimeFormatter.ofPattern(format))); In this example, SimpleDateFormat correctly parses the date, but DateTimeFormatter throws an exception. If I were to use zero padded dates, e.g., "05/03/1969", both work. However, if either the day of month or the month of year

Convert between LocalDate and XMLGregorianCalendar

。_饼干妹妹 提交于 2019-11-27 12:42:26
问题 What's the best way to convert between LocalDate from Java 8 and XMLGregorianCalendar ? 回答1: Converting from LocalDate to XMLGregorianCalendar : LocalDate date = LocalDate.now(); GregorianCalendar gcal = GregorianCalendar.from(date.atStartOfDay(ZoneId.systemDefault())); XMLGregorianCalendar xcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal); Converting back is simpler: xcal.toGregorianCalendar().toZonedDateTime().toLocalDate(); 回答2: The LocalDate stores only year/month/day

How to convert LocalDate to SQL Date Java?

﹥>﹥吖頭↗ 提交于 2019-11-27 11:52:06
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 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 like this: Date date = r.getDate(); LocalDate localD = date.toLocalDate(); r is the record you're using in JOOQ

Is there any way to convert ZoneId to ZoneOffset in java 8?

自作多情 提交于 2019-11-27 11:51:27
问题 I have an epoch second and a zoneId,by method1.It can be convert to LocalDateTime with system default zoneId,but I don't find the way to convert epoch second to LocalDateTime by method2,because there is no ZoneOffset.systemDefault .I think it's obscure. import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset} val epochSecond = System.currentTimeMillis() / 1000 LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), ZoneId.systemDefault())//method1 LocalDateTime.ofEpochSecond

How can I create a Java 8 LocalDate from a long Epoch time in Milliseconds?

走远了吗. 提交于 2019-11-27 10:37:40
I have an external API return me dates as long s, represented as milliseconds since Epoch. With the old style Java API, I would simply construct a Date from it with Date myDate = new Date(startDateLong) What is the equivalent in Java 8's LocalDate / LocalDateTime classes? I am interested in converting the point in time represented by the long to a LocalDate in my current local timezone. If you have the milliseconds since the Epoch and want convert them to a local date using the current local timezone, you can use LocalDate date = Instant.ofEpochMilli(longValue).atZone(ZoneId.systemDefault())

How to get milliseconds from LocalDateTime in Java 8

不羁岁月 提交于 2019-11-27 10:28:27
I am wondering if there is a way to get current milliseconds since 1-1-1970 (epoch) using the new LocalDate , LocalTime or LocalDateTime classes of Java 8. The known way is below: long currentMilliseconds = new Date().getTime(); or long currentMilliseconds = System.currentTimeMillis(); Stuart Marks I'm not entirely sure what you mean by "current milliseconds" but I'll assume it's the number of milliseconds since the "epoch," namely midnight, January 1, 1970 UTC. If you want to find the number of milliseconds since the epoch right now, then use System.currentTimeMillis() as Anubian Noob has

Java 8 Convert given time and time zone to UTC time

拈花ヽ惹草 提交于 2019-11-27 10:00:10
问题 I have a time with string type like: "2015-01-05 17:00" and ZoneId is "Australia/Sydney" . How can I convert this time information to the corresponding to UTC time using Java 8 datetime API? Also need to considering DST stuff. 回答1: You are looking for ZonedDateTime class in Java8 - a complete date-time with time-zone and resolved offset from UTC/Greenwich. In terms of design, this class should be viewed primarily as the combination of a LocalDateTime and a ZoneId . The ZoneOffset is a vital,

Parsing ISO_INSTANT and similar Date Time Strings

橙三吉。 提交于 2019-11-27 09:29: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, MilliOfSecond=0, MicroOfSecond=0},ISO of type java.time.format.Parsed at java.time.LocalDate.from

How to get UTC+0 date in Java 8?

烂漫一生 提交于 2019-11-27 09:23:59
问题 I have problems with Date class in Java. Date class returns local machine date but i need UTC-0. I have googled and found great solution for JavaScript but for Java nothing useful. How to get UTC+0 date in Java 8? 回答1: With Java 8 you can write: OffsetDateTime utc = OffsetDateTime.now(ZoneOffset.UTC); To answer your comment, you can then convert it to a Date (unless you depend on legacy code I don't see any reason why) or to millis since the epochs: Date date = Date.from(utc.toInstant());

How to change the base date for parsing two letter years with Java 8 DateTimeFormatter?

余生颓废 提交于 2019-11-27 09:06:20
If I use a pattern like d/M/yy for creating a Java 8 DateTimeFormatter (e.g. using DateTimeFormatter.ofPattern(pattern); (which I will only use for parsing, not formatting), it will interpret all two-letter years as 20xx, e.g. parsing a string like 13/5/99 to be interpreted as 2099-05-13 , which in my case is wrong (it was meant to be in the year 1999). In my application, I'm trying to parse dates from OCR'd documents, which could e.g. still be from the 90ies, so having the old SimpleDateFormat behavior of interpreting the date to be within 80 years before and 20 years after the current date