Parser template for monthOfYear, which does not have zero width without text separation of text

Is it possible to parse a date 8302011using jodatime? In a less painful format, it would look like 8/30/2011which I would label as MM/dd/yyyy.

What I tried:

  • Template Mddyyyy
    • 8302011Cannot parse "8302011": Value 83 for monthOfYear must be in the range [1,12]
    • 123020112011-12-30T00:00:00.000Z

Fortunately, the date is not ambiguous, as dayit is always represented as two digits. A month, however, is one or two digits.

I understand that it would be simple enough to fill the zeros on the left with 8 characters, but in this case I can not do this.

+4
source share
1 answer

I know the following is different from jodatime, but you can try using SimpleDateFormat to parse the date as an alternative

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by luan on 9/12/16.
 */
public class DateTimeFormatTest {
    public SimpleDateFormat simpleDateFormat = new SimpleDateFormat("Mddyyyy");
    public SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("MM/dd/yyyy");

    public Date getDate(String source){
        Date date = null;
        try {
            date = simpleDateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return date;
    }
    public String parseStringValue(Date date){
        String result = "";
        result = simpleDateFormat2.format(date);
        return result;
    }

    public static void main(String[] args) {
        DateTimeFormatTest obj = new DateTimeFormatTest();
        Date date = obj.getDate("8302011");
        System.out.println(date);
        String result = obj.parseStringValue(date);
        System.out.println(result);
    }
}

:

Tue Aug 30 00:00:00 ICT 2011
08/30/2011
0

All Articles