convert String “yyyy-MM-dd” to LocalDateTime

Use LocalDate to create a localDate and then you can add the timepart if you need it: DateTimeFormatter DATEFORMATTER = DateTimeFormatter.ofPattern(“yyyy-MM-dd”); LocalDate ld = LocalDate.parse(“2017-03-13”, DATEFORMATTER); LocalDateTime ldt = LocalDateTime.of(ld, LocalDateTime.now().toLocalTime()); System.out.println(ldt); or LocalDateTime ldt = LocalDateTime.of(ld, LocalDateTime.MIN.toLocalTime()); if you just need an empty timepart EDIT: Look at this solution with this you can build … Read more

How do I parse RFC 3339 datetimes with Java?

tl;dr Instant.parse( “2011-05-03T11:58:01Z” ) ISO 8601 Actually, RFC 3339 is but a mere self-proclaimed “profile” of the actual standard, ISO 8601. The RFC is different in that it purposely violates ISO 8601 to allow a negative offset of zero hours (-00:00) and gives that a semantic meaning of “offset unknown“. That semantic seems like a … Read more

Parse Date String to Some Java Object

Using Joda-Time, take a look at DateTimeFormat; it allows parsing both kind of date strings that you mention (and almost any other arbitrary formats). If your needs are even more complex, try DateTimeFormatterBuilder. To parse #1: DateTimeFormatter f = DateTimeFormat.forPattern(“yyyy-MM-dd HH:mm:ss”); DateTime dateTime = f.parseDateTime(“2012-01-10 23:13:26”); Edit: actually LocalDateTime is a more appropriate type for … Read more

java.time.format.DateTimeParseException: Text could not be parsed at index 3

First of all, check the javadoc. The uppercase D represents the day-of-year field (not the day-of-month as you want), and uppercase Y represents the week-based-year field (not the year as you want). The correct patterns are the lowercase letters d and y. Also, you’re using month names in uppercase letters (JAN and FEB), so your … Read more

Parse date in MySQL

You may want to use the STR_TO_DATE() function. It’s the inverse of the DATE_FORMAT() function. STR_TO_DATE(str,format) This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value … Read more

How to parse a date string into a c++11 std::chrono time_point or similar?

std::tm tm = {}; std::stringstream ss(“Jan 9 2014 12:35:34”); ss >> std::get_time(&tm, “%b %d %Y %H:%M:%S”); auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm)); GCC prior to version 5 doesn’t implement std::get_time. You should also be able to write: std::tm tm = {}; strptime(“Thu Jan 9 2014 12:35:34”, “%a %b %d %Y %H:%M:%S”, &tm); auto tp = std::chrono::system_clock::from_time_t(std::mktime(&tm));

Parsing a string to a date in JavaScript

The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor. Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS. But wait! Just using the “ISO format” doesn’t work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The … Read more