0

I've got this string

23/Gennaio/2014

and I need this other string

23/01/2014

I tried using joda.time:

DateTimeFormatter format =  DateTimeFormat.forPattern("dd/MMM/yyyy").withLocale(Locale.ITALY);
DateTime instance = format.parseDateTime("23/Gennaio/2014");  
String month_number = String.valueOf(instance.getMonthOfYear());

But I get this exception:

01-06 13:31:55.341: E/AndroidRuntime(1116): java.lang.IllegalArgumentException: Invalid format: "23/Gennaio/2014"

What am I missing?

venergiac
  • 7,469
  • 2
  • 48
  • 70
nicky_1525
  • 929
  • 1
  • 10
  • 24
  • MMM means short form of a month e.g. JAN, FEB. What you want is the long form, MMMM. – W.K.S Jan 06 '14 at 13:39
  • FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [*java.time*](http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Apr 19 '18 at 04:27

4 Answers4

5

It seems to expect the month name in lower case (not sure why):

DateTime instance = format.parseDateTime("23/Gennaio/2014".toLowerCase(Locale.ITALIAN));

should work better.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • First letter is lowercase because that is the [cultural norm for Italian](https://en.wikibooks.org/wiki/Italian/Vocabulary/Day_and_Month). Ditto for French and some other Romance languages. – Basil Bourque Apr 19 '18 at 04:31
2

23/Gennaio/2014 is not a valid date string to be parsed.
Try 23/gennaio/2014 and try to parse it with "dd/MMMM/yyyy" format (add an M)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
1

why not pure java?

 SimpleDateFormat formatIn = new SimpleDateFormat("dd/MMM/yyyy", Locale.ITALY);
 Date instance = formatIn.parse("23/Gennaio/2014");  
 SimpleDateFormat formatOut = new SimpleDateFormat("dd/MM/yyyy", Locale.ITALY);

 System.out.println(formatOut.format(instance));

 String month_number = String.valueOf(instance.getMonth()); //DEPRECETDE USE CALENDAR

result is "23/01/2014"

venergiac
  • 7,469
  • 2
  • 48
  • 70
  • FYI, the troublesome old date-time classes such as `java.util.Date`, `java.util.Calendar`, and `java.text.SimpleDateFormat` are now legacy, supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. Much of the *java.time* functionality is back-ported to Java 6 & Java 7 in the [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) project. Further adapted for earlier Android in the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706). – Basil Bourque Apr 19 '18 at 04:26
0

Use this code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    String dateString = "23/Gennaio/2014";


     Log.i("meenal","Date 2:"+localizeDate(dateString, Locale.ITALY));


}

private String localizeDate(String inputdate, Locale locale) { 

    Date date = new Date();
    SimpleDateFormat dateFormatCN = new SimpleDateFormat("dd/MMM/yyyy", locale);       
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");


    try {
        date = dateFormatCN.parse(inputdate);

        Log.i("meenal", "Date:"+date);
    } catch (Exception e) {
        Log.e("meenal", e.getMessage(),e);
        return inputdate;
    }
    return dateFormat.format(date);
}
Meenal
  • 2,879
  • 5
  • 19
  • 43