java-time

Convert java.time.LocalDate into java.util.Date type

六眼飞鱼酱① 提交于 2019-11-26 02:40:52
问题 I want to convert java.time.LocalDate into java.util.Date type. Because I want to set the date into JDateChooser . Or is there any date chooser that supports java.time dates? 回答1: Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant()); That assumes your date chooser uses the system default timezone to transform dates into strings. 回答2: Here's a utility class I use to convert the newer java.time classes to java.util.Date objects and vice versa: import java.time

Any difference between java.time.Clock.systemDefaultZone().getZone() vs java.util.TimeZone.getDefault().toZoneId()?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 02:25:32
问题 Is there any difference in between the two, given that both java.time.Clock.systemDefaultZone().getZone() and java.util.TimeZone.getDefault().toZoneId() return the same output. For instance this code import java.time.Clock; import java.util.TimeZone; public class Main { public static void main(String[] args) { System.out.println(\"Clock.systemDefaultZone().getZone() : \" + Clock.systemDefaultZone().getZone()); System.out.println(\"TimeZone.getDefault().toZoneId() : \" + TimeZone.getDefault()

JSON Java 8 LocalDateTime format in Spring Boot

ⅰ亾dé卋堺 提交于 2019-11-26 01:52:45
问题 I\'m having a small problem with formatting a Java 8 LocalDateTime in my Spring Boot Application. With \'normal\' dates I have no problem, but the LocalDateTime fields are converted to the following: \"startDate\" : { \"year\" : 2010, \"month\" : \"JANUARY\", \"dayOfMonth\" : 1, \"dayOfWeek\" : \"FRIDAY\", \"dayOfYear\" : 1, \"monthValue\" : 1, \"hour\" : 2, \"minute\" : 2, \"second\" : 0, \"nano\" : 0, \"chronology\" : { \"id\" : \"ISO\", \"calendarType\" : \"iso8601\" } } While I would like

Unix epoch time to Java Date object

混江龙づ霸主 提交于 2019-11-26 01:40:04
问题 I have a string containing the UNIX Epoch time, and I need to convert it to a Java Date object. String date = \"1081157732\"; DateFormat df = new SimpleDateFormat(\"\"); // This line try { Date expiry = df.parse(date); } catch (ParseException ex) { ex.getStackTrace(); } The marked line is where I\'m having trouble. I can\'t work out what the argument to SimpleDateFormat() should be, or even if I should be using SimpleDateFormat(). 回答1: How about just: Date expiry = new Date(Long.parseLong

Convert java.util.Date to what “java.time” type?

余生颓废 提交于 2019-11-26 01:38:02
问题 I have a java.util.Date object, or a java.util.Calendar object. How do I convert that to the right type in java.time framework? I have heard that we should now be doing the bulk of our business logic with java.time types. When working with old code not yet updated for java.time I need to be able to convert back and forth. What types map to java.util.Date or java.util.Calendar ? 回答1: Yes, you definitely should be using the java.time framework whenever possible. Avoid old date-time classes The

serialize/deserialize java 8 java.time with Jackson JSON mapper

末鹿安然 提交于 2019-11-26 00:36:52
问题 How do I use Jackson JSON mapper with Java 8 LocalDateTime? org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDateTime] from JSON String; no single-String constructor/factory method (through reference chain: MyDTO[\"field1\"]->SubDTO[\"date\"]) 回答1: There's no need to use custom serializers/deserializers here. Use jackson-modules-java8's datetime module: Datatype module to make Jackson recognize Java 8 Date & Time API data

Converting between java.time.LocalDateTime and java.util.Date

耗尽温柔 提交于 2019-11-26 00:35:11
问题 Java 8 has a completely new API for date and time. One of the most useful classes in this API is LocalDateTime , for holding a timezone-independent date-with-time value. There are probably millions of lines of code using the legacy class java.util.Date for this purpose. As such, when interfacing old and new code there will be a need for converting between the two. As there seems to be no direct methods for accomplishing this, how can it be done? 回答1: Short answer: Date in = new Date();

Convert java.util.Date to java.time.LocalDate

一笑奈何 提交于 2019-11-25 23:26:41
问题 What is the best way to convert a java.util.Date object to the new JDK 8/JSR-310 java.time.LocalDate ? Date input = new Date(); LocalDate date = ??? 回答1: Short answer Date input = new Date(); LocalDate date = input.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); Explanation Despite its name, java.util.Date represents an instant on the time-line, not a "date". The actual data stored within the object is a long count of milliseconds since 1970-01-01T00:00Z (midnight at the start of

How to parse/format dates with LocalDateTime? (Java 8)

不打扰是莪最后的温柔 提交于 2019-11-25 22:35:14
问题 Java 8 added a new java.time API for working with dates and times (JSR 310). I have date and time as string (e.g. \"2014-04-08 12:30\" ). How can I obtain a LocalDateTime instance from the given string? After I finished working with the LocalDateTime object: How can I then convert the LocalDateTime instance back to a string with the same format as shown above? 回答1: Parsing date and time To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It

serialize/deserialize java 8 java.time with Jackson JSON mapper

半腔热情 提交于 2019-11-25 18:49:18
How do I use Jackson JSON mapper with Java 8 LocalDateTime? org.codehaus.jackson.map.JsonMappingException: Can not instantiate value of type [simple type, class java.time.LocalDateTime] from JSON String; no single-String constructor/factory method (through reference chain: MyDTO["field1"]->SubDTO["date"]) There's no need to use custom serializers/deserializers here. Use jackson-modules-java8's datetime module : Datatype module to make Jackson recognize Java 8 Date & Time API data types (JSR-310). This module adds support for quite a few classes: Duration Instant LocalDateTime LocalDate