The reason for the inaccuracy is because you are not using LocalDateTime#parse() or even ZonedDateTime#parse(). Also, if the string happens to contain a time zone part), then you will need to parse a String in a certain pattern into a LocalDateTime. Refer below for more clarification:-
String oldstring = "2011-01-18 00:00:00.0"; LocalDateTime datetime = LocalDateTime.parse(oldstring, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"));
If you're not on Java 8 yet, the simple use of SimpleDateFormat#parse() to parse a String in a certain pattern into a Date as shown below will help you:-
String oldstring = "2011-01-18 00:00:00.0"; Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(oldstring);
Trying incorporating the SimpleDateFormat#format() to format a Date into a String in a certain pattern as shown below:-
String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date); System.out.println(newstring); // 2011-01-18