1

im getting a date including (- or / or space) in a format dd-mm-yyyy or dd.MMM.yy for example

dd.MM.yyyy
dd-MM-yy
dd MM yyyy

how to get this to my format dd MM yyyy even if the user put last two digit for the year I have to convert it to 4 digit number?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • This is a similar question http://stackoverflow.com/questions/4024544/how-to-parse-dates-in-multiple-formats-using-simpledateformat – BobTheBuilder Aug 19 '13 at 04:48

3 Answers3

0

So you are wanting:

replace all / with space replace all - with space replace after last space with 4 digits assume this century 13 -> 2013

Then use SimpleDateFormat.parse

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

You could take a look at Apache Commons Lang DateUtils which allows you to parse a date String using a series of different date patterns.

The simple uses the SimpleDateFormat to do the actual work, but makes it easier when you want to try using multiple formats rather then having to loop it self.

Once you have it back to Date value, you can use SimpleDateFormat to format the Date just about anyway you want it

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Try something like this.

    Scanner sc=new Scanner(System.in);
    System.out.println("Enter your date: ");
    String[] format={"dd.MM.yyyy", "dd-MM-yy","dd MM yyyy"};
        for (String i : format)
        {
            try
            {
                Date date=new SimpleDateFormat(i).parse(sc.nextLine());
                System.out.println(new SimpleDateFormat("dd MM yyyy").format(date) );
            }
            catch (ParseException e) {

            }
        }

Live Demo here.

You can add what ever the SimpleDateFormat to format array. Then this will convert to "dd MM yyyy" format.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • i need a lil bit more help how do i compare this date with the calendar so i will able to get the day_of_week and also how do i calculate leap year in the calendar? – Ashifur rahman Aug 19 '13 at 08:15