Link to home
Start Free TrialLog in
Avatar of Jeevanjosh
Jeevanjosh

asked on

how to get today value"

Hi
 ia have the  table room_booking and the value is stored in the string formate("dd/mm/yyyy e.g 04/07/2008")

i want to retrive the value  wich is equal to today can you pls help in solving the problem

i have the  compareResult1 method



  public static int getCompareResult1(String date) throws ParseException{            
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
                     df.setLenient ( false );
        
        Date d1 = df.parse(date);
        Date today = new Date();
               int relation;
        if (d1.equals(today))
             relation = 0;
        else if(d1.before(today))
            relation = -1;
        else
         relation = 1;
        return relation;
        }      
            
}

when i call this in my jsp
Avatar of mikeblamires
mikeblamires
Flag of United Kingdom of Great Britain and Northern Ireland image

In what format are you passing data in the method?
The "new Date()" line will produce a data along the lines of "Mon Apr 14 15:03:32 BST 2008", i.e.. the correct full time for now. If you are only passing "14/04/2008" to the method then it will be "Mon Apr 14 00:00:00 BST 2008".
Comparing these two will will result in a false, to result in a true then you would need to pass a date which is fully equivalent to the time at the moment today is set.

let me know if the above isn't clear.

Mike


This works for me if you are passing a value in the form of "14/04/2003" as the above code suggests.

Let me know if you it need explanation. The code is not robust for internationalisation, i.e. American format dates would cause ParseException.
public static int getCompareResult1(String date) throws ParseException {      
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
        df.setLenient ( false );
        
        DateFormat df1 = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyy", Locale.getDefault());
        df1.setLenient ( false );        
        
        Date d1 = df.parse(date);
        Date now = df1.parse(new Date().toString());
    
              
        Calendar today = Calendar.getInstance(Locale.getDefault());
        today.setTime(now);
        Calendar compare = Calendar.getInstance(Locale.getDefault());
        compare.setTime(d1);
        
        compare.set(Calendar.HOUR_OF_DAY, 0);
        compare.set(Calendar.MINUTE, 0);
        compare.set(Calendar.SECOND, 0);
        compare.set(Calendar.MILLISECOND, 0);
        
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        
        
        if (today.equals(compare)) {
            return 0;
        } else if(today.before(compare)) {
            return -1;
        } else if(today.after(compare)) {
            return 1;
        } 
}

Open in new window

Avatar of Jeevanjosh
Jeevanjosh

ASKER

Hi  Expert

Thank You so much for the reply
 I am passing the  date like  i attach the steps how  i reach my final value
  with this  steps even my data base is storing the value in the form of dd/MM/yyyy
i attach the output also but when i tr to get the reuslt wich is equal to today it giving zero result

kindly help in solving this problem

thank you
regards
jeevanjosh

part of Util.java
 
 
	/**
	This method return the date in "day/month/year" numerically
	@param date "May , 2003" format
	@param day in numeric format
	@return String "day/month/year"
	*/
	public static String stringDate(String date, String day) {
		StringTokenizer st = new StringTokenizer(date);
		String month = st.nextToken(" , ");
		String year = st.nextToken();
		int monthInt = getMonthInt(month) + 1;
		//  int monthInt = Integer.parseInt(month);
		   //  monthInt++;
		        if(monthInt < 10)
		        	return day + "/" + "0"+monthInt+ "/" + year;
		        else
					return day + "/" + monthInt + "/" + year;
		      
	}   	
 
 
 
 
	
//	public static class CompareDates{
	  public String getCompareResult(String date) throws ParseException {
	   DateFormat df = new SimpleDateFormat("dd/MM/yyyy");//error line1
	   System.out.println(date);
	   Date d1 = df.parse(date); 
	   Date today = new Date();
	   String relation;
	   if (d1.equals(today))
	   relation = "the same as date";
		else if(d1.before(today))
		 relation = "before";
		  else
			  relation = "after";
		return df.format(d1) + "is" + relation +' '+ df.format(today); 			
	  }
 
 
//	  public static int getCompareResult1(String date) throws ParseException{
	  //DateFormat df = new SimpleDateFormat("dd/mm/yyyy");//same error as line1
	
	  public static int getCompareResult1(String date) throws ParseException{		
	  DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
			   df.setLenient ( false );
	  
	  Date d1 = df.parse(date); 
	  Date today = new Date();
		   int relation;
	  if (d1.equals(today))
		 relation = 0;
	  else if(d1.before(today))
		relation = -1;
	  else
	   relation = 1;
	  return relation;
	  }	
		
		      	   

Open in new window

Step-1.doc
output.doc
Hi mike
when i try the avove solution dated this it is giving an error
ERROR : this method must return a result of type int



mikeblamiresDate:04.14.2008 at 10:10PM SGT
error1.doc
The compiler doesn't like the the last if statement, left it in by mistake. Remove the last condition as below and it will work.
public static int getCompareResult1(String date) throws ParseException {      
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
        df.setLenient ( false );
        
        DateFormat df1 = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyy", Locale.getDefault());
        df1.setLenient ( false );        a
        
        Date d1 = df.parse(date);
        Date now = df1.parse(new Date().toString());
    
              
        Calendar today = Calendar.getInstance(Locale.getDefault());
        today.setTime(now);
        Calendar compare = Calendar.getInstance(Locale.getDefault());
        compare.setTime(d1);
        
        compare.set(Calendar.HOUR_OF_DAY, 0);
        compare.set(Calendar.MINUTE, 0);
        compare.set(Calendar.SECOND, 0);
        compare.set(Calendar.MILLISECOND, 0);
        
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        
        
        if (today.equals(compare)) {
            return 0;
        } else if(today.before(compare)) {
            return -1;
        } else {
            return 1;
        } 
}

Open in new window

and teh extra a added in on line 6!
Hi  mike

Thank you so much for the wonderful  &very kind help i am getting the  result for todays booking , i am happy

your help is rely wonderful very very kind of you, it is rely  wonderful .

mike if you pls kindly explain this part to me ,so that i can undersatnd this  part

ie line 5 & 6
DateFormat df1 = new SimpleDateFormat("EEE MMM dd kk:mm:ss zzz yyy", Locale.getDefault());
df1.setLenient ( false );        

Thank You so much
my thanks to you  ,

thank you so much for understanding my problem and answered to me according to my need.

rely my lots and lots of thanks to you.

pls pass  my spl thanks to Mr.Tomas Helgi bcos he helped me a lot  too very very kindly.

Thank you
great of you

with lots of thanks
Jeevanjosh


perfect-result.doc
ASKER CERTIFIED SOLUTION
Avatar of mikeblamires
mikeblamires
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Mike

Thank you so much  for all the kind help

Thank you so ,
 with lots and lots of thanks
jeevanjosh
Thank you so much Mike, all ur answers are very clear , and easy to understand, thank you so much ,thank you so much, the way you expalin is very very excellent
with lots and lots of thanks
-jeevanjosh
Hi Mike,

 sorry, to ask this question now,pls kindly help me in solving this problem,

i am getting the values equal to today and as well  greater than today from my table

but when i get the values ,  the values are not order by date ,
bcos some the values like  for example 01/6/2008  and some other values like 1/6/2008,
previously it was stored inthe tbale like 1/6/2008.

and one more problem it is comparing only the first value that is date ,i attached my output,  my table ,

pls kindly help how can iget my output order by date

i need very much need your help in this

with lots of thanks
jeevanjosh



outputdate.doc
sampout.doc
tableoutput.doc