0

What is the best way to transform like October-2016 into an Date Object?

When I try to use Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");

it throws an exception, because it cannot convert this type of date.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
SepDev
  • 153
  • 11

2 Answers2

0

since the string October-2016 is containing information related to a language YOU NEED TO SPECIFY the Locale in the instance of the SimpleDateFormat

Date date = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0
String st = "October-2016";
        DateFormat format = new SimpleDateFormat("MMMM-yyyy");
        Date date = format.parse(st);
        System.out.println(date); 

Both working same:

Date date = new SimpleDateFormat("MMMM-yyyy").parse("October-2016");
        Date date1 = new SimpleDateFormat("MMMM-yyyy", Locale.US).parse("October-2016");
        System.out.println(date); 
        System.out.println(date1); 
Jay Prakash
  • 787
  • 6
  • 22