-3
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.ParseException;
import java.text.DateFormat;
import java.util.Locale;

class Main {

    public static void main(String[] args)throws ParseException{
        Scanner sc=new Scanner(System.in);
        System.out.println("enter the date");
        String p=sc.nextLine();

        SimpleDateFormat d=new SimpleDateFormat("dd/MM/yyyy",Locale.ENGLISH);        
        SimpleDateFormat i=new SimpleDateFormat("dd/MM/yyyy");

        try {
           Date m=d.parse(p);
           System.out.println("The input date is"+i.format(m));
        } catch(ParseException e){
           e.printStackTrace();
        }
    }
}

when I am running this problem I am getting error as

java.text.ParseException:Unparseable date: "27-01-1978" at java.text.DateFormat.parse(DateFormat.java:366) at Main.main(Main.java:19).

What should be done in order to avoid that error.

Evhz
  • 8,852
  • 9
  • 51
  • 69
Divya
  • 1
  • 3
    Both of your `SimpleDateFormat` objects are created with the same pattern (dd/MM/yyyy) - was that intentional? – Jon Skeet Mar 24 '18 at 10:24
  • 2
    You need to provide a format that's matches the date you want to parse. If your date is supposed to have dashes, don't put slashes in your formatter. Voting to close as your issue is down to a typo and thus unlikely to be useful to future visitors. – Joe C Mar 24 '18 at 10:26
  • Already answered in [this link](https://stackoverflow.com/a/4216767/4101906), and yes @JoeC is right! – Rahmat Waisi Mar 24 '18 at 10:30
  • Maybe try input a date formatted as you set in your code, like: `27/01/1978` – Evhz Mar 24 '18 at 10:33
  • 2
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 25 '18 at 10:50

1 Answers1

1

It’s very basic:

    DateTimeFormatter formatterWithHyphens = DateTimeFormatter.ofPattern("dd-MM-uuuu");
    DateTimeFormatter formatterWithSlashes = DateTimeFormatter.ofPattern("dd/MM/uuuu");
    LocalDate date = LocalDate.parse(p, formatterWithHyphens);
    System.out.println("The input date is" + date.format(formatterWithSlashes));

Output

The input date is27/01/1978

I recommend you avoid the classes SimpleDateFormat and Date. They are long outdated, and the former in particular notoriously troublesome. Instead I am using and recommending java.time, the modern Java date and time API. Generally it is so much nicer to work with.

What went wrong in your code

You just needed to use the pattern that you gave yourself in your question title: dd-MM-yyyy. When you input date has hyphens (dashes) in it, then the format pattern string needs to have them too.

Then why did I use uuuu instead of yyyy? I really could have used yyyy with java.time too, for years in the common era (“anno domini”) there is no difference. uuuu is a signed year, so 0 equals 1 BCE, 1 equals 2 BCE, etc. My habit is to use uuuu unless there is a reason for yyyy.

In addition: use more explanatory variable names than d, i, m and p. I believe this will make it easier for yourself.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161