Gregorian Calendar program

Thread Solved
Reply

Join Date: Feb 2009
Posts: 24
Reputation: redmaverick is an unknown quantity at this point 
Solved Threads: 0
redmaverick redmaverick is offline Offline
Newbie Poster

Gregorian Calendar program

 
0
  #1
Apr 23rd, 2009
I am trying to execute this program, but I am getting errors
this is the code

  1.  
  2. import java.util.*;
  3. public class DateUtils
  4. {
  5.  
  6. static final int MILLS_IN_DAY = 24*60*60*1000;
  7.  
  8. public static Date getCurrentDate()
  9. {
  10. GregorianCalendar currentDate = new GregorianCalendar();
  11. currentDate.set(Calendar.HOUR,0);
  12. currentDate.set(Calendar.MINUTE,0);
  13. currentDate.set(Calendar.SECOND,0);
  14. return currentDate.getTime();
  15. }
  16.  
  17. public static Date createDate(int year, int month, int day)
  18. {
  19. GregorianCalendar date = new GregorianCalendar(year,month,day);
  20. return date.getTime();
  21. }
  22.  
  23.  
  24. public static Date stripTime(Date date)
  25. {
  26. GregorianCalendar currentDate = new GregorianCalendar();
  27. currentDate.setTime(date);
  28. currentDate.set(Calendar.HOUR,0);
  29. currentDate.set(Calendar.MINUTE,0);
  30. currentDate.set(Calendar.SECOND,0);
  31. return currentDate.getTime();
  32. }
  33.  
  34.  
  35. public static int daysDiff(Date date1, Date date2)
  36. {
  37. date1 = stripTime(date1);
  38. date2 = stripTime(date2);
  39. long longDate1=date1.getTime();
  40. long longDate2=date2.getTime();
  41. long longDiff = longDate2 - longDate1;
  42. return (int)(longDiff/MILLS_IN_DAY);
  43.  
  44. }
  45.  
  46.  
  47. }

I am testing this with

  1. import java.util.*;
  2. public class Tes{
  3. public static void main(String[] args) {
  4.  
  5. GregorianCalendar currentGC = new GregorianCalendar();
  6. int currentYear = currentGC.get(Calendar.YEAR);
  7. Date currentDate = DateUtils.getCurrentDate();
  8. Date christmas = DateUtils.createDate(currentYear,Calendar.DECEMBER,25);
  9. int daysToChristmas = DateUtils.daysDiff(currentDate, christmas);
  10. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
  11. String formattedDate = dateFormat.format(currentDate);
  12. System.out.println("Today" + formattedDate);
  13. System.out.println("There are"+daysToChristmas);
  14. }
  15. }

The first code compiled fine. But I am having errors with the second one.

  1. Tes.java:11: cannot find symbol
  2. symbol : class DateFormat
  3. location: class Tes
  4. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
  5. ^
  6. Tes.java:11: cannot find symbol
  7. symbol : variable DateFormat
  8. location: class Tes
  9. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
  10. ^
  11. Tes.java:11: cannot find symbol
  12. symbol : variable DateFormat
  13. location: class Tes
  14. DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
  15. ^
  16. 3 errors

How should I correct the 3 errors?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 24
Reputation: redmaverick is an unknown quantity at this point 
Solved Threads: 0
redmaverick redmaverick is offline Offline
Newbie Poster

Re: Gregorian Calendar program

 
0
  #2
Apr 23rd, 2009
code is from a text book
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,576
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Gregorian Calendar program

 
2
  #3
Apr 24th, 2009
When do you think the compile time error 'symbol not found comes up'? What does your text book say about such errors? The example you posted seems a pretty bold one for someone who has just started out with Java.

Anyways, the error message means that the compiler doesn't know about the token 'DateFormat'; `import' the class in question and you should be good to go.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 16
Reputation: sincerelibran is an unknown quantity at this point 
Solved Threads: 9
sincerelibran sincerelibran is offline Offline
Newbie Poster

Re: Gregorian Calendar program

 
1
  #4
Apr 24th, 2009
Dear Friend, you have to use import java.text.*; since the Format and DateFormat are a part of java.text package. If you do that you won't get any error message.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 24
Reputation: redmaverick is an unknown quantity at this point 
Solved Threads: 0
redmaverick redmaverick is offline Offline
Newbie Poster

Re: Gregorian Calendar program

 
0
  #5
Apr 24th, 2009
Thanks for the help. It is working now. I know that this is pretty bold for a beginner. But I am understanding the concept behind it and I think that by understanding more such programs I will be able to become better with time.

With the internet and helpful communities like these willing to give a helping hand, I am sure I can improve myself.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC