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
Web Development SoftwareJava

Avatar of undefined
Last Comment
Jeevanjosh

8/22/2022 - Mon
mikeblamires

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


mikeblamires

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

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Jeevanjosh

ASKER
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
mikeblamires

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

mikeblamires

and teh extra a added in on line 6!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Jeevanjosh

ASKER
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
mikeblamires

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Jeevanjosh

ASKER
Hi Mike

Thank you so much  for all the kind help

Thank you so ,
 with lots and lots of thanks
jeevanjosh
Jeevanjosh

ASKER
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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Jeevanjosh

ASKER
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