java-time

ZonedDateTime from date string in yyyy-mm-dd

牧云@^-^@ 提交于 2021-01-07 02:42:29
问题 Trying to parse a ZonedDateTime from a date string e.g '2020-08-24'. Upon using TemporalAccesor, and DateTimeFormatter.ISO_OFFSET_DATE to parse, I am getting a java.time.format.DateTimeParseException. Am I using the wrong formatter? Even tried adding 'Z' at the end of date string for it to be understood as UTC private ZonedDateTime getZonedDateTime(String dateString) { TemporalAccessor parsed = null; dateString = dateString + 'Z'; try { parsed = DateTimeFormatter.ISO_OFFSET_DATE.parse

Java: parse ISO_DATE / ISO_OFFSET_DATE

非 Y 不嫁゛ 提交于 2021-01-05 12:00:28
问题 For a REST web service, I need to return dates (no time) with a time zone . Apparently there is no such thing as a ZonedDate in Java (only LocalDate and ZonedDateTime ), so I'm using ZonedDateTime as a fallback. When converting those dates to JSON, I use DateTimeFormatter.ISO_OFFSET_DATE to format the date, which works really well: DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE; ZonedDateTime dateTime = ZonedDateTime.now(); String formatted = dateTime.format(formatter); 2018

How can one get a java.time DateTime using clojure.java-time from #inst Date literal?

自古美人都是妖i 提交于 2021-01-04 14:00:47
问题 Using clj-time it is possible to parse an #inst Date literal like so: (require '[clj-time.coerce :as c]) (c/from-date #inst "2012-12-12") ;; => #object[org.joda.time.DateTime 0x4a251269 "2012-12-12T00:00:00.000Z"] How would this be done using the new java.time wrapper clojure.java-time? 回答1: PLEASE NOTE: Joda Time and the clj-time library which wraps it are both deprecated. You should use java.time via Java interop for most tasks. There are also some helper functions here you may find useful.

Saving a LocalTime in MySql TIME column

早过忘川 提交于 2021-01-03 05:27:45
问题 Backstory I recently came across a problem with saving a LocalTime to a TIME column in a MySQL database. Saving a value of 9:00 was causing 8:00 to be saved in the database. This problem did not occur on my dev environment (Windows), but did occur on two Linux machines we tried. My code was the following: preparedStatement.setObject(parameterIndex, localTime, JDBCType.TIME); After investigating, I eventually found that the MySQL JDBC driver was using java.sql.Time.valueOf(localTime) , then

Java 8 epoch-millis time stamp to formatted date, how?

怎甘沉沦 提交于 2020-12-29 10:15:12
问题 Before Java-8 I got accustomed to always keep anything date/time related as milliseconds since Epoch and only ever deal with human readable dates/times on the way out, i.e. in a UI or a log file, or when parsing user generated input. I think this is still safe with Java-8, and now I am looking for the most concise way to get a formatted date out of a milliseconds time stamp. I tried df = Dateformatter.ofPattern("...pattern..."); df.format(Instant.ofEpochMilli(timestamp)) but it bombs out with

Java 8 epoch-millis time stamp to formatted date, how?

时光毁灭记忆、已成空白 提交于 2020-12-29 10:14:40
问题 Before Java-8 I got accustomed to always keep anything date/time related as milliseconds since Epoch and only ever deal with human readable dates/times on the way out, i.e. in a UI or a log file, or when parsing user generated input. I think this is still safe with Java-8, and now I am looking for the most concise way to get a formatted date out of a milliseconds time stamp. I tried df = Dateformatter.ofPattern("...pattern..."); df.format(Instant.ofEpochMilli(timestamp)) but it bombs out with

Set date and time in java.sql.Timestamp

心不动则不痛 提交于 2020-12-27 06:51:10
问题 Please tell me how to set the date (current date minus one day) and time equal to 19:00:00 using such a construction? new java.sql.Timestamp(java.util.Calendar.getInstance.getTime().getTime()) LocalDateTime don't use. 回答1: I recommend you do it using the java.time (the modern date-time API). Solution, purely using the modern API: import java.time.LocalTime; import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class Main { public static void main

Set date and time in java.sql.Timestamp

为君一笑 提交于 2020-12-27 06:49:07
问题 Please tell me how to set the date (current date minus one day) and time equal to 19:00:00 using such a construction? new java.sql.Timestamp(java.util.Calendar.getInstance.getTime().getTime()) LocalDateTime don't use. 回答1: I recommend you do it using the java.time (the modern date-time API). Solution, purely using the modern API: import java.time.LocalTime; import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class Main { public static void main

Java 8 Date time for calculating age in decimals [duplicate]

梦想的初衷 提交于 2020-12-26 05:07:04
问题 This question already has answers here : Java Time period in decimal number of years (3 answers) Closed 3 years ago . I am new in using Java 8 date time API and was wondering how can i able to calculate the age in decimals which returns the double value like 30.5 which means 30 years and 6 months? For example the below sample code gets me the output as 30.0 but not 30.5 which probably am trying for. LocalDate startDate = LocalDate.of(1984, Month.AUGUST, 10); LocalDate endDate = LocalDate.of

“Task not serializable” with java time in Spark-shell (or zeppelin) but not in spark-submit

 ̄綄美尐妖づ 提交于 2020-12-15 08:59:16
问题 Weirdly, I found several times there's difference when running with spark-submit vs running with spark-shell (or zeppelin), though I don't believe it. With some codes, spark-shell (or zeppelin) can throw this exception, while spark-submit just works fine: org.apache.spark.SparkException: Task not serializable at org.apache.spark.util.ClosureCleaner$.ensureSerializable(ClosureCleaner.scala:345) at org.apache.spark.util.ClosureCleaner$.org$apache$spark$util$ClosureCleaner$$clean(ClosureCleaner