1

I need to obtain a Date object from this String:This is a example)

M-27\nJUN-2012  DayOfWeek-dd\nMONTH-yyyy

Its impossible? I dont know how to use simpledateformat for this..The \n is a line jump.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
rbrlnx
  • 286
  • 7
  • 23

2 Answers2

3

The documentation for SimpleDateFormat doesn't seem to do days of the week as single letters. I would take a substring that removes that letter and the dash with it:

27\nJUN-2012

Then I would use dd\nMMM-yyyy as the string you use in the constructor of the SimpleDateFormat.

This should do exactly what you want. Since you have the day of the month as a number, the day of the week doesn't matter that much to you to create the date object. If you really want to know the day of the week, then see here: How to determine day of week by passing specific date?

Community
  • 1
  • 1
carmenism
  • 1,087
  • 3
  • 12
  • 31
1

Try following SimpleDateFormat:

String dateS = "M-27\nJUN-2012";  // your date string
SimpleDateFormat fmt = new SimpleDateFormat("dd\nMMM-yyyy"); // formatter
Date date = fmt.parse(dateS.substring(2));
System.out.println(date);

You can really drop out the day of week, because day, month and year defines this day.

mishadoff
  • 10,719
  • 2
  • 33
  • 55