I am given dates in string format- Eg. 11/09/2010. (11-09-2010 not allowed) How can I convert it into integer dates?

I am given two such dates in string format only and I want to find the no of days between them.

SimpleDateFormat. (It's in the API reference)
Create a format to match your data, then use the parse(...)method to convert to a Date

you can use switch case.its a long method.

SimpleDateFormat. (It's in the API reference)
Create a format to match your data, then use the parse(...)method to convert to a Date

I have tried many combinations but I m surely missing sumthing.

Date date = new Date();
	   
	   
	   String DATE_FORMAT = "dd/MM/yyyy";
	  
	   SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
	  Date d1= df.parse(15/11/2010);

I have tried other combinations also...but couldnt figure out how to correct it!! :(

And is there any function to find the no of days in between 2 dates ?

Member Avatar for coil

You can also use substring to do it manually if the inputted String is always in the format dd/mm/yyyy.

You can also use substring to do it manually if the inputted String is always in the format dd/mm/yyyy.

Wont it leave it in string format only?? My main motive is to find the no. of days between two given dates. How do I do that?

Member Avatar for coil

What I was referring to was, if you have problems creating the Dates from the input, you could use substring and then parse it to an int.

To find the number of days, I don't think there's a method (you should probably check the API). You'll probably need to do it manually, i.e. number of months difference * days in the months + days difference

What I was referring to was, if you have problems creating the Dates from the input, you could use substring and then parse it to an int.

N how do i do that now for a date 15/10/2010?
U mean I could store 15 in int day, 10 in month and 2010 in year?

but couldnt figure out how to correct it

Does that mean you got erors compiling or executing the code?
Or does it mean that the value of d1 was incorrect?

Does that mean you got erors compiling or executing the code?
Or does it mean that the value of d1 was incorrect?

I am getting error compiling the code in parse("Date")-

Date date = new Date();
	   
	   
	   String DATE_FORMAT = "dd/MM/yyyy";
	  
	   SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
	  Date d1= parse("10/11/2010"); //Error:The method parse(String) is undefined for the type CompareDatesFunc

Otherwise how can I use substrings to store the date in diff variables int day, int month, int year? I think that would make it easier to calculate the no of days between 2 dates then.

How can I convert it into integer dates?

What is an "integer date"?

Date d1= parse("10/11/2010"); //Error:The method parse(String) is undefined

What class is the parse method in?

What is an "integer date"?

By integer date I meant the normal date in Java date class on which I could perform functions. Right now I am taking date as a string from user. I wanted to find the no. of days in between 2 dated and becoz of tht I wrote "integer date".

What class is the parse method in?

Its in CompareDatesFunc class only!
Here's the complete code

import java.util.*;
import java.text.*;

public class CompareDatesFunc {
   public static void main(String[] args)
   {

	   Date date = new Date();
	   
	   
	   String DATE_FORMAT = "dd/MM/yyyy";
	  
	   SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
	  Date d1= parse("10/11/2010"); //Error:he method parse(String) is undefined for the type CompareDatesFunc
	   
	   
   }
}

ok i found the error...Now I am stuck on finding the no if days in between the 2 dates..

import java.util.*;
import java.text.*;

public class CompareDatesFunc {
   public static void main(String[] args) throws ParseException
   {

	   Date date = new Date();
	   
	   
	   String DATE_FORMAT = "dd/MM/yyyy";
	  
	   SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
	  Date d1= df.parse("10/11/2015"); 
	  Date d2=df.parse("10/11/2010");
	  
	  
	   
   }

How do I find no if days in between the 2 given years?

Where is the parse method defined in CompareDatesFunc class?

the normal date in Java date class

You're referring to the Date class?
A Date object is returned by the SimpleDateFormat parse() method.
Given two Date objects created by the parse method, use their getTime() method to get the Epoch times and subtract them to get the difference between the two dates in ms.

How do I find no if days in between the 2 given years?

Use the Date class getTime() method and subtract and then divide by 24*60*60*1000

Done!!!

Thanku so much!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.