Solved

String to date java

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 54
ejb stateless example 2 27
ejb wildfly example 2 29
ejb entity bean example 2 17
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

820 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