Solved

String to date java

Posted on 2015-01-23
3
85 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

930 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now