Convert “duration” “Unit” to milliseconds
You can use Duration for this: Duration.of(5, ChronoUnit.SECONDS).toMillis()
You can use Duration for this: Duration.of(5, ChronoUnit.SECONDS).toMillis()
Short answer: The JSR-310-designers don’t want people to do conversions between machine time and human time via static from()-methods in types like ZoneId, ZoneOffset, OffsetDateTime, ZonedDateTime etc. This is explicitly specified if you carefully study the javadoc. Instead use: OffsetDateTime#toInstant():Instant ZonedDateTime#toInstant():Instant Instant#atOffset(ZoneOffset):OffsetDateTime Instant#atZone(ZoneId):ZonedDateTime The problem with the static from()-methods is that otherwise people are able … Read more
Java 9 In Java 9 the TimeUnit API got extended and allows to convert between TimeUnit and ChronoUnit: TimeUnit.toChronoUnit() // returns a ChronoUnit TimeUnit.of(ChronoUnit chronoUnit) // returns a TimeUnit see: JDK-8141452
UPDATE: The answer below is valid if you need to stay on a Hibernate version < 5.0. Hibernate 5.0 supports persisting JSR-310 date/time types out of the box. I.e. if you are on Hibernate 5.0 or newer, Adam’s answer is the way to go. Everyone else, read on. The root cause for this at none … Read more
Bug – Fixed in Java 9 This issue was already reported in JDK-bug-log. Stephen Colebourne mentions as work-around following solution: DateTimeFormatter dtf = new DateTimeFormatterBuilder() .appendPattern(“yyyyMMddHHmmss”) .appendValue(ChronoField.MILLI_OF_SECOND, 3) .toFormatter(); Note: This workaround does not cover your use-case of only two pattern symbols SS. An adjustment might only be to use other fields like MICRO_OF_SECOND (6 … Read more
The default parser can parse your input. So you don’t need a custom formatter and String dateTime = “2012-02-22T02:06:58.147Z”; ZonedDateTime d = ZonedDateTime.parse(dateTime); works as expected.
Parse it to LocalDateTime then format it: LocalDateTime localDateTime = LocalDateTime.parse(“2018-12-14T09:55:00”); DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“dd.MM.yyyy HH:mm”); String output = formatter.format(localDateTime); If this does not work with api21, you can use: SimpleDateFormat parser = new SimpleDateFormat(“yyyy-MM-dd’T’HH:mm:ss”); SimpleDateFormat formatter = new SimpleDateFormat(“dd.MM.yyyy HH:mm”); String output = formatter.format(parser.parse(“2018-12-14T09:55:00”)); or import ThreeTenABP.
The best way is with java.text.DateFormatSymbols DateFormatSymbols symbols = new DateFormatSymbols(new Locale(“it”)); // for the current Locale : // DateFormatSymbols symbols = new DateFormatSymbols(); String[] dayNames = symbols.getShortWeekdays(); for (String s : dayNames) { System.out.print(s + ” “); } // output : dom lun mar mer gio ven sab
Postgres has no such data type as TIMESTAMP. Postgres has two types for date plus time-of-day: TIMESTAMP WITH TIME ZONE and TIMESTAMP WITHOUT TIME ZONE. These types have very different behavior with regard to time zone information. The WITH type uses any offset or time zone information to adjust the date-time to UTC, then disposes … Read more
The Java 8 version (and later) of java.sql.Date has built in support for LocalDate, including toLocalDate and valueOf(LocalDate). To convert from LocalDate to java.sql.Date you can use java.sql.Date.valueOf( localDate ); And to convert from java.sql.Date to LocalDate: sqlDate.toLocalDate(); Time zones: The LocalDate type stores no time zone information, while java.sql.Date does. Therefore, when using the … Read more