zoneddatetime

Getting time range between midnight and current time JDK 8

廉价感情. 提交于 2019-12-05 12:36:20
I have this method to calculate midnigt and current time as long values: /** * Returns the time range between the midnight and current time in milliseconds. * * @param zoneId time zone ID. * @return a {@code long} array, where at index: 0 - midnight time; 1 - current time. */ public static long[] todayDateRange(ZoneId zoneId) { long[] toReturn = new long[2]; LocalTime midnight = LocalTime.MIDNIGHT; LocalDate today = LocalDate.now(zoneId); LocalDateTime todayMidnight = LocalDateTime.of(today, midnight); ZonedDateTime todayMidnightZdt = todayMidnight.atZone(zoneId); toReturn[0] =

ZonedDateTimeDeserializer is missing in jackson jsr310

ε祈祈猫儿з 提交于 2019-12-05 09:09:41
I'm parsing a ZonedDateTime using like this: @JsonSerialize(using = ZonedDateTimeSerializer.class) private ZonedDateTime expirationDateTime; I need to be able to properly deserialize this date. However, there is no deserializer for this that is provided by jackson: com.fasterxml.jackson.datatype.jsr310.deser Is there a reason why it's missing? What is the most common workaround? Updated : Here is my scenario: I create ZonedDateTime like this: ZonedDateTime.of(2017, 1, 1, 1, 1, 1, 1, ZoneOffset.UTC) Then I serialize the object that contains the date like this: public static String toJSON(Object

Jackson serializes a ZonedDateTime wrongly in Spring Boot

落花浮王杯 提交于 2019-12-04 09:56:04
问题 I have a simple application with Spring Boot and Jetty. I have a simple controller returning an object which has a Java 8 ZonedDateTime : public class Device { // ... private ZonedDateTime lastUpdated; public Device(String id, ZonedDateTime lastUpdated, int course, double latitude, double longitude) { // ... this.lastUpdated = lastUpdated; // ... } public ZonedDateTime getLastUpdated() { return lastUpdated; } } In my RestController I simply have: @RequestMapping("/devices/") public

ZonedDateTime to UTC with offset applied?

孤人 提交于 2019-12-04 05:56:09
I am using Java 8 This is what my ZonedDateTime looks like 2013-07-10T02:52:49+12:00 I get this value as z1.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME) where z1 is a ZonedDateTime . I wanted to convert this value as 2013-07-10T14:52:49 How can I do that? Is this what you want? This converts your ZonedDateTime to a LocalDateTime with a given ZoneId by converting your ZonedDateTime to an Instant before. LocalDateTime localDateTime = LocalDateTime.ofInstant(z1.toInstant(), ZoneOffset.UTC); Or maybe you want the users system-timezone instead of hardcoded UTC: LocalDateTime localDateTime =

Convert ZonedDateTime to LocalDateTime at time zone

微笑、不失礼 提交于 2019-12-03 10:42:47
I have an object of ZonedDateTime that is constructed like this ZonedDateTime z = ZonedDateTime.of(LocalDate.now().atTime(11, 30), ZoneOffset.UTC); How can I convert it to LocalDateTime at time zone of Switzerland? Expected result should be 16 april 2018 13:30 . How can I convert it to LocalDateTime at time zone of Switzerland? You can convert the UTC ZonedDateTime into a ZonedDateTime with the time zone of Switzerland, but maintaining the same instant in time, and then get the LocalDateTime out of that, if you need to. I'd be tempted to keep it as a ZonedDateTime unless you need it as a

Java 8: How to create a ZonedDateTime from an Epoch value?

ぐ巨炮叔叔 提交于 2019-12-03 06:28:16
问题 Java 8's LocalDateTime has an ofEpochSecond method. Unfortunately, there is no such method in the ZonedDateTime . Now, I have an Epoch value and an explicit ZoneId given. How do I get a ZonedDateTime out of these? 回答1: You should be able to do this via the Instant class, which can represent a moment given the epoch time. If you have epoch seconds, you might create something via something like Instant i = Instant.ofEpochSecond(t); ZonedDateTime z = ZonedDateTime.ofInstant(i, zoneId); 来源: https

Jackson serializes a ZonedDateTime wrongly in Spring Boot

前提是你 提交于 2019-12-03 05:27:27
I have a simple application with Spring Boot and Jetty. I have a simple controller returning an object which has a Java 8 ZonedDateTime : public class Device { // ... private ZonedDateTime lastUpdated; public Device(String id, ZonedDateTime lastUpdated, int course, double latitude, double longitude) { // ... this.lastUpdated = lastUpdated; // ... } public ZonedDateTime getLastUpdated() { return lastUpdated; } } In my RestController I simply have: @RequestMapping("/devices/") public @ResponseBody List<Device> index() { List<Device> devices = new ArrayList<>(); devices.add(new Device("321421521"

Setting future date in Java 8

浪子不回头ぞ 提交于 2019-12-02 11:48:42
问题 In Joda we have setCurrentMillisFixed method which can be used to set future date In one my test cases , i pass a DateTime (joda) parameter to current system time: DateTime created = new DateTime() DateTimeUtils.setCurrentMillisSystem(created.toInstant().toEpochMilli());); In Java 8 i am trying : I pass ZonedDateTime instead of DateTime, I tried setting the date as below: ZonedDateTime.now(Clock.fixed(Instant.now().plusMillis(created.toInstant().toEpochMilli()),ZoneId.systemDefault())); But

java.time.format.DateTimeParseException: Unable to obtain ZonedDateTime from TemporalAccessor on format ddMMyyyyhhmmss [duplicate]

送分小仙女□ 提交于 2019-12-02 08:22:01
问题 This question already has answers here : Unable to obtain ZonedDateTime from TemporalAccessor when parsing a Date (2 answers) Closed 8 months ago . I'm having trouble in formatting a string to a ZonedDateTime. My customer wants to have the date in a format such as ddMMyyyyhhmmss, with no separators or stuff like that. This is what I've done so far import java.time.format.DateTimeFormatter; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; public class

String to ZonedDateTime is changing format

 ̄綄美尐妖づ 提交于 2019-12-02 08:12:40
问题 String ip="2011-05-01T06:47:35.422-05:00"; ZonedDateTime mzt = ZonedDateTime.parse(ip).toInstant().atZone(ZoneOffset.UTC); System.out.println(mzt); System.out.println("-----"); String ip2="2011-05-01T00:00:00.000-05:00"; ZonedDateTime mzt2 = ZonedDateTime.parse(ip2).toInstant().atZone(ZoneOffset.UTC); System.out.println(mzt2); Output: 2011-05-01T11:47:35.422Z ----- 2011-05-01T05:00Z Why is the date format getting changed in case 2? I am getting SQLServer Database error due to this. 回答1: This