-3

I'm trying to use Java 8's DateTimeFormatter to turn strings such as "17/01/2019" into dates of exactly the same format.

I'm currently using:

DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.ENGLISH);

LocalDateTime dExpCompletionDate = LocalDateTime.parse(sExpCompletionDate, format);
LocalDateTime dExpCommencementDate = LocalDateTime.parse(sExpCommencementDate, format);

and getting the error:

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

Which would suggest there's something wrong with my format.

Currently, I've tried using the default format as well as using LocalDate instead of LocalDateTime

Xander
  • 991
  • 1
  • 13
  • 32
  • 5
    The error message tells you exactly what's wrong: "Text '' could not be parsed ...". You are trying to parse an empty string, and not a string containing "01/01/2019". Make sure that the string you are trying to parse actually contains the date. – Jesper Jan 17 '19 at 10:15
  • 2
    Also a string `01/01/2019` is not a valid `LocalDateTime` as you're missing the time completly. You probably wanted to use `LocalDate` – Lino Jan 17 '19 at 10:16
  • The text that could not be parsed is between the two apostrophes in the error message. In your case there is no text there! You are trying to parse the empty string. That fails. – Ole V.V. Jan 17 '19 at 10:53

3 Answers3

0

You're trying to obtain LocalDateTime instead of LocalDate:

LocalDateTime dExpCompletionDate = LocalDateTime.parse(sExpCompletionDate, format);

Here is a small example with LocalDate:

public static void main(String[] args) {
    String sExpCompletionDate = "17/01/2019";

    DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.ENGLISH);

    LocalDate dExpCompletionDate = LocalDate.parse(sExpCompletionDate, format);
    // Converts LocalDate into LocalDateTime
    LocalDateTime dExpCompletionDate2 = LocalDate.parse(sExpCompletionDate, format).atStartOfDay();
    System.out.println(dExpCompletionDate);
    System.out.println(dExpCompletionDate2);
}

Output:

2019-01-17
2019-01-17T00:00

Here is an example with LocalDateTime:

public static void main(String[] args) {
    String sExpCompletionDate = "17/01/2019 14:22:11";

    DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss", Locale.ENGLISH);

    LocalDateTime dExpCompletionDate = LocalDateTime.parse(sExpCompletionDate, format);
    System.out.println(dExpCompletionDate);
}

Output:

2019-01-17T14:22:11
J-Alex
  • 6,881
  • 10
  • 46
  • 64
0

Because "dd/MM/yyyy" is date pattern, you can't parse to DateTime with it. What you can do is, parse to Date and then get StartOfDay as DateTime

DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDateTime dExpCompletionDate = LocalDate.parse("01/01/2019", format).atStartOfDay();
shakhawat
  • 2,639
  • 1
  • 20
  • 36
-2

Use SimpleDateFormat. Here is a working example:

import java.text.SimpleDateFormat;  
import java.util.Date;  
public class StringToDateExample1 {  
public static void main(String[] args)throws Exception {  
    String sDate1="31/12/1998";  
    Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1);  
    System.out.println("Date is : "+date1);  
}  
}  

For a more comprehensive answer, refer to reply by BalusC.

  • 2
    Don't encourage to switch from the great modern java.time.* classes to the obsolete java.util.Date and associated classes. This won't solve any problem and will only be worse. – kumesana Jan 17 '19 at 10:41
  • @kumesana Yes, you're right. Since I'd worked on a lot of pre-Java 8 code recently, I'd forgotten of the `java.time` package. I didn't notice that part in the question, just the part about converting strings to dates. – Tejas Chandrashekhar Jan 17 '19 at 11:04