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

Parsing date and time To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern. String str = “1986-04-08 12:30”; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“yyyy-MM-dd HH:mm”); LocalDateTime dateTime = LocalDateTime.parse(str, formatter); Formatting date and … Read more

DateTimeFormatter month pattern letter “L” fails

“stand-alone” month name I believe ‘L’ is meant for languages that use a different word for the month itself versus the way it is used in a date. For example: Locale russian = Locale.forLanguageTag(“ru”); asList(“MMMM”, “LLLL”).forEach(ptrn -> System.out.println(ptrn + “: ” + ofPattern(ptrn, russian).format(Month.MARCH)) ); Output: MMMM: марта LLLL: Март There shouldn’t be any reason … Read more

How to round time to the nearest quarter hour in java?

Rounding You will need to use modulo to truncate the quarter hour: Date whateverDateYouWant = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(whateverDateYouWant); int unroundedMinutes = calendar.get(Calendar.MINUTE); int mod = unroundedMinutes % 15; calendar.add(Calendar.MINUTE, mod < 8 ? -mod : (15-mod)); As pointed out by EJP, this is also OK (replacement for the last line, only … Read more

Convert between LocalDate and sql.Date [duplicate]

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

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 milliseconds from LocalDateTime in Java 8

I’m not entirely sure what you mean by “current milliseconds” but I’ll assume it’s the number of milliseconds since the “epoch,” namely midnight, January 1, 1970 UTC. If you want to find the number of milliseconds since the epoch right now, then use System.currentTimeMillis() as Anubian Noob has pointed out. If so, there’s no reason … Read more