vip1all
vip1all

Reputation: 122

Parse exception in joda-time parsing day and time

The problem is pretty simple, I have a TimerTask that is going to be scheduled daily or weekly. (Depends on the given start date and period in config file). So in config file I specify the day of week and time of execution with period between executions. But Joda-Time refuses to work with my date :(

Here is basic input:

String            input     = "Tue 12:00:00";
DateTimeFormatter formatter = DateTimeFormat.forPattern("EEE HH:mm:ss");
DateTime dateTime = DateTime.parse(input, formatter); // Here parse exception is thrown...

Exception message: java.lang.IllegalArgumentException: Invalid format: "Tue 12:00:00"

Could someone explain me why I cannot parse this date like that and maybe point me into right direction to solve this kind of problem. Of course I could manually parse the date and set things in calendar, but I don't want to reinvent the wheel if there is something like Joda-Time that could do it for me.

Upvotes: 1

Views: 1301

Answers (2)

SkyWalker
SkyWalker

Reputation: 29140

You can use Locale. The following code is working fine.

import java.util.Locale;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class Test22 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DateTime currentDate = new DateTime();
        DateTimeFormatter dtf = DateTimeFormat.forPattern("EEE HH:mm:ss").withLocale(Locale.ENGLISH);
        String formatedDate = dtf.print(currentDate);
        System.out.println(formatedDate);
        DateTime now = dtf.parseDateTime(formatedDate);
        System.out.println(now);
    }

}

Output:

Thu 23:36:33

2000-12-28T23:36:33.000+06:00

Resource Link: https://stackoverflow.com/a/41465180/2293534

Upvotes: 2

Reimeus
Reimeus

Reputation: 159754

The weekday text Tue probably doesnt match that of your default locale.

DateTimeFormatter formatter = 
        DateTimeFormat.forPattern("EEE HH:mm:ss").withLocale(Locale.ENGLISH);

Upvotes: 3

Related Questions