user1197252
user1197252

Reputation:

String to Date Conversion and Date arithmetic

I have a list of strings in my java application that represent dates. The format is yyyy/MM/dd. I want to be able to take all of these strings and convert them to actual date objects so arithmetic can be performed on them.

Basically I want to go through the list and remove dates that have already occurred. I have attached the code.

List<String> datesList = new ArrayList<String>();
        datesList.add("2011-11-01");
        datesList.add("2015-11-01");

        //Get todays date and format it 
        Calendar currentDate = Calendar.getInstance(); 
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd"); 

        //This string will also need to be converted to a date object so the loop arithmetic can be performed.
        String today = formatter.format(currentDate.getTime());
        System.out.println(today);

        for(String date: datesList) {
            //The list cannot change from a list of strings.
            //So the conversion will probably have to take place in this loop.
            System.out.println(date);
            //if(date < today) ...
            //datesList.remove(date);
        }

Updating to include solution:

List<String> datesList = new ArrayList<String>();
        datesList.add("2013-11-01");
        datesList.add("2011-11-01");
        datesList.add("2013-04-29");
        datesList.add("2001-05-19");

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd"); 

        List<String> validDatesList = new ArrayList<String>();

        for(String date: datesList) {
            Date listItem = formatter.parse(date);
            Date todayDate = new Date();

            if(todayDate.after(listItem)) {
                System.out.println(listItem+" has already happened because today is "+todayDate);
            } else{
                validDatesList.add(date);
            }
        }

        for(String validDate: validDatesList) {
            System.out.println("Valid date: "+validDate);
        }

Thanks.

Upvotes: 0

Views: 206

Answers (4)

smttsp
smttsp

Reputation: 4191

I would suggest you to convert date into long, then your work would be much easier.

Date d1 = new Date();//current time
d1.getTime()/1000;//will give you current time as a long value in second. 

See epoch/unix time

Upvotes: 0

Tedil
Tedil

Reputation: 1933

A quite different and less efficient approach: add today's date to the list, sort the list, and remove all list items whose index is greater/smaller (depending on sort order) than today. Note that this will only work for the date format you specified ("yyyy-MM-dd") (with leading zeroes!) and only makes sense if removing previous dates is the only operation you'd like to do.

Upvotes: 0

drew moore
drew moore

Reputation: 32680

You can use a SimpleDateFormat object to parse a string into a Date object as well:

Date d = formatter.parse(today);, where today is your date in String format.

Then to check if the dare is after today, having gotten the current date as follows:

Date today = currentDate.getTime();

, your if statement would look like:

(if d.after(today)) {
    datesList.remove(date);

Upvotes: 1

Schaemelhout
Schaemelhout

Reputation: 705

Parse your Date's with this, and then you can compare the date with today's date

Date newDate = formatter.parse(date);
Date todayDate = new Date();
if(todayDate.after(newDate)) {

}

Upvotes: 1

Related Questions