943,078 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 5929
  • Java RSS
Apr 23rd, 2009
-1

Gregorian Calendar program

Expand Post »
I am trying to execute this program, but I am getting errors
this is the code

Java Syntax (Toggle Plain Text)
  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

Java Syntax (Toggle Plain Text)
  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.

Java Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
redmaverick is offline Offline
35 posts
since Feb 2009
Apr 23rd, 2009
0

Re: Gregorian Calendar program

code is from a text book
Reputation Points: 10
Solved Threads: 0
Light Poster
redmaverick is offline Offline
35 posts
since Feb 2009
Apr 24th, 2009
2

Re: Gregorian Calendar program

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Apr 24th, 2009
1

Re: Gregorian Calendar program

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.
Reputation Points: 8
Solved Threads: 15
Light Poster
sincerelibran is offline Offline
41 posts
since Mar 2009
Apr 24th, 2009
0

Re: Gregorian Calendar program

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
redmaverick is offline Offline
35 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Installer Files..
Next Thread in Java Forum Timeline: Looping through different sets





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC