0

I'm trying to make my user input a date in "words" form, then print it as a date.

For example:

System.out.print("Enter Date: ")
String inDate = nextLine()

User will input January 21, 2019, then I will print it as 01/21/2019.

  • How will your code do this? –  Jul 06 '19 at 09:09
  • Always search Stack Overflow thoroughly before posting. You can assume any basic date-time question has already been asked and answered. – Basil Bourque Jul 07 '19 at 22:16
  • Only when you search, avoid pages and answers using the `SimpleDateFormat` class. That class is notoriously troublesome and long outdated, but unfortunately still described in very many places. Instead you need `LocalDate` and `DateTimeFormatter`. You may want to include the class names in your search. – Ole V.V. Jul 08 '19 at 14:05

1 Answers1

0
SimpleDateFormat fmt = new SimpleDateFormat("MMM dd, yyyy");
        SimpleDateFormat fmtOut = null;
        Date date = null;
        try {
            date = fmt.parse("January 21, 2019");
            fmtOut = new SimpleDateFormat("MM/dd/yyyy");
    System.out.println(fmtOut.format(date))
        } catch (ParseException e) {
            e.printStackTrace();
        }
  • 6
    Date is old, please stop giving answer with it, as also it requires a try/catch, java 8 java.time is just better – azro Jul 06 '19 at 09:28
  • @azro can you pls share your code – Murali Krishnan Jul 06 '19 at 10:03
  • 1
    you should take a look at the 2 links pointed as duplicates on the question above ;) – azro Jul 06 '19 at 10:31
  • 1
    @Murali Krishnan we can write this way by using java 8 ............ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d yyyy"); String localDate = LocalDate.parse(indate, formatter) .format(DateTimeFormatter.ofPattern("MM/dd/yyyy")); System.out.println(localDate); – Dhrumil Patel Jul 06 '19 at 10:35
  • 1
    FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 07 '19 at 22:15