Which one is recommended: Instant.now().toEpochMilli() or System.currentTimeMillis()

Both are fine. And neither is recommended except for a minority of purposes. What do you need milliseconds since the epoch for? In Java, we can have many different ways to get the current timestamp, For current timestamp just use Instant.now(). No need to convert to milliseconds. Many methods from the first years of Java, … Read more

Calculate days, hours and minutes between two instants

To calculate days, hours and/or minutes between two Instant objects: Instant instant1 = Instant.parse(“2019-02-14T18:42:00Z”); Instant instant2 = Instant.parse(“2019-04-21T05:25:00Z”); // If you only need one of them System.out.println(ChronoUnit.DAYS.between(instant1, instant2)); // prints: 65 System.out.println(ChronoUnit.HOURS.between(instant1, instant2)); // prints: 1570 System.out.println(ChronoUnit.MINUTES.between(instant1, instant2)); // prints: 94243 // Or use alternate syntax (it’s the same thing) System.out.println(instant1.until(instant2, ChronoUnit.DAYS)); // prints: 65 … Read more

Convert java.time.Instant to java.sql.Timestamp without Zone offset

I changed my computer’s time zone to Europe/Bucharest for an experiment. This is UTC + 2 hours like your time zone. Now when I copy your code I get a result similar to yours: Instant now = Instant.now(); System.out.println(now); // prints 2017-03-14T06:16:32.621Z Timestamp current = Timestamp.from(now); System.out.println(current); // 2017-03-14 08:16:32.621 Output is given in comments. … Read more

Converting string to ‘Instant’

tl;dr Fix your formatting pattern for unpadded month and day. Use only java.time classes, never the legacy classes. Contrived example: LocalDateTime.parse( // Parse as an indeterminate `LocalDate`, devoid of time zone or offset-from-UTC. NOT a moment, NOT a point on the timeline. “04:30 PM, Sat 5/12/2018” , // This input uses a poor choice of … Read more

How to convert from Instant to LocalDate

Java 9+ LocalDate.ofInstant(…) arrived in Java 9. Instant instant = Instant.parse(“2020-01-23T00:00:00Z”); ZoneId zone = ZoneId.of(“America/Edmonton”); LocalDate date = LocalDate.ofInstant(instant, zone); See code run live at IdeOne.com. Notice the date is 22nd rather than 23rd as that time zone uses an offset several hours before UTC. 2020-01-22 Java 8 If you are using Java 8, then … 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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)