String to date java

HI: I am importing a bank statement into a file using java. Typically banks do not consider the users and the date column is in the format "dd-MMM" - eg "01-Jan". Is there a way of converting this text to a date - and somehow get the year right (eg if date is in last week of year take this 'year -1', else 'this year')?
Thanks!
Richard TeasdaleFinancial ControllerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CEHJConnect With a Mentor Commented:
Try

   public static Date toDate(String date) {
        DateFormat df = new SimpleDateFormat("dd-MMM");
        Date d = df.parse(date, new ParsePosition(0));
        Calendar cNow = Calendar.getInstance();
        Calendar cBal = Calendar.getInstance();
        cBal.setTime(d);
        cBal.set(Calendar.YEAR, cNow.get(Calendar.YEAR));

        if (cBal.after(cNow)) {
            cBal.add(Calendar.YEAR, -1);
        }

        return cBal.getTime();
    }

Open in new window

0
 
Richard TeasdaleFinancial ControllerAuthor Commented:
Looks very neat! So you are saying that if the date derived is after today it is last year! Excellent!  Thank you very much!
0
 
CEHJCommented:
Yes, that should work unless you get psychic bank statements ;) Of course i don't know anything about previous years ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.