Unix epoch time to Java Date object

How about just: Date expiry = new Date(Long.parseLong(date)); EDIT: as per rde6173’s answer and taking a closer look at the input specified in the question , “1081157732” appears to be a seconds-based epoch value so you’d want to multiply the long from parseLong() by 1000 to convert to milliseconds, which is what Java’s Date constructor … Read more

How to convert LocalDate to SQL Date Java?

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 … Read more

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

Here is how you can get ZoneOffset from ZoneId: Instant instant = Instant.now(); //can be LocalDateTime ZoneId systemZone = ZoneId.systemDefault(); // my timezone ZoneOffset currentOffsetForMyZone = systemZone.getRules().getOffset(instant); NB: ZoneId can have different offset depending on point in time and the history of the particular place. So choosing different Instants would result in different offsets. NB2: … Read more

How to use LocalDateTime RequestParam in Spring? I get “Failed to convert String to LocalDateTime”

TL;DR – you can capture it as a string with just @RequestParam, or you can have Spring additionally parse the string into a java date / time class via @DateTimeFormat on the parameter as well. the @RequestParam is enough to grab the date you supply after the = sign, however, it comes into the method … Read more

How to get UTC+0 date in Java 8?

tl;dr Instant.now() java.time The troublesome old date-time classes bundled with the earliest versions of Java have been supplanted by the java.time classes built into Java 8 and later. See Oracle Tutorial. Much of the functionality has been back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP. Instant An Instant … Read more

How to extract epoch from LocalDate and LocalDateTime?

The classes LocalDate and LocalDateTime do not contain information about the timezone or time offset, and seconds since epoch would be ambigious without this information. However, the objects have several methods to convert them into date/time objects with timezones by passing a ZoneId instance. LocalDate LocalDate date = …; ZoneId zoneId = ZoneId.systemDefault(); // or: … Read more