Solved

String to date java

Posted on 2015-01-23
3
91 Views
Last Modified: 2015-01-23
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!
0
Comment
Question by:ClaytonGlass
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 40566211
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
 

Author Closing Comment

by:ClaytonGlass
ID: 40566217
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 40566241
Yes, that should work unless you get psychic bank statements ;) Of course i don't know anything about previous years ...
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question