Solved

String to date java

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

21 Experts available now in Live!

Get 1:1 Help Now