1
import org.joda.time.DateTime;

public static void main(String[] args) {
    DateTimeFormatter f = DateTimeFormat.forPattern("dd MMM yyyy");
    f = f.withLocale(Locale.US);
    f.parseDateTime("13 januari 2016");

    DateTime date = new DateTime(f);
    date.getDayOfYear();
}

The month is in Dutch, not in English.

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "13 januari 2016" is malformed at "uari 2016"
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:899)
at com.company.Main.main(Main.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

How can I properly initialize this Joda DateTime? Many thanks guys!

Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
  • Try to replace `f.parseDateTime("13 januari 2016");` with `f.parseDateTime("13 jan 2016");` – Dhruv May 18 '16 at 13:01
  • @Dhruv that doesn't work. And I have 12 months, each length varies. – Jim Clermonts May 18 '16 at 13:04
  • I thought it is the problem due to your different DateTimeFormatter. In that you are having MMM format month, and you are passing value in it as `januari`. I think it should be like, `jan., feb., maart, apr., mei, juni, juli, aug., sept., oct., nov., dec.` – Dhruv May 18 '16 at 13:08
  • Possible duplicate of [joda DateTime parser error](http://stackoverflow.com/questions/6393765/joda-datetime-parser-error) – Dhruv May 18 '16 at 13:21
  • Sure, but these are the values that I have: Januari, Februari, Maart, April, Mei, Juni, Juli, Augustus, September, Oktober, November, December. And it has to work with these month values. Is that not possible somehow? – Jim Clermonts May 18 '16 at 13:22
  • I am not sure about that. Because I have used Joda library with English month name. If anything I will get, I will surely add comment here. :) – Dhruv May 18 '16 at 13:35

2 Answers2

2

Retrieve the appropriate Locale for the language you expect. Use it to construct a DateTimeFormat with the appropriate pattern. Then parse your date string.

For example

Locale dutch = Locale.forLanguageTag("nl-nl");
DateTimeFormatter f = DateTimeFormat.forPattern("dd MMM yyyy").withLocale(dutch);
DateTime date = f.parseDateTime("13 januari 2016");

I don't know what you were trying to do with

DateTime date = new DateTime(f);

That DateTime constructor expects an Object representing an instant.

The recognised object types are defined in ConverterManager and include ReadableInstant, String, Calendar and Date. The String formats are described by ISODateTimeFormat.dateTimeParser().

DateTimeFormat is neither of these.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
1

Joda-Time in maintenance mode

The Answer by Sotirios Delimanolis is correct. But beware that the Joda-Time project is now in maintenance mode, with its team advising migration to the java.time classes.

Using java.time

The LocalDate class represents a date-only value without time-of-day and without time zone.

When parsing a string, specify Locale to determine:

  • The human language for translation of name of day, name of month, and such.
  • The cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

Rather than specify a formatting pattern explicitly, you may let java.time automatically localize. To localize, specify a FormatStyle to determine how long or abbreviated should the string be.

String input = "13 januari 2016";
Locale l = new Locale ( "nl", "NL" );  // Dutch language, Netherlands country.
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate ( FormatStyle.LONG ).withLocale ( l );
LocalDate ld = LocalDate.parse ( input, f );

Dump to console.

System.out.println ( "input: " + input );
System.out.println ( "ld.toString(): " + ld );

input: 13 januari 2016

ld.toString(): 2016-01-13


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154