I am trying to make a program which will compare the hours with the actual hour of new Date() and I have difficulties with it. Here is what I have so far:
File HelloDate3.java
import java.util.Date; public class HelloDate3 { public static void main (string [] args) { int hour = Integer.parseInt (args[0]); if (hour >= 06:59:00:00 && hour <=12:59:00:00) { System.out.println (�Good Morning, Today is � + new Date()); }
else if (hour >= 13:00:00:00 && hour <= 16:59:00:00) { System.out.println (�Good Afternoon, Today is � + new Date()); } } }
Instead of �hour� should I use another word? �time� perhaps ?
Later, as soon at it works, I shall extend the program to include �Good Evening��..�
First of all, the variable name you use does not matter. The words "hour" and "time" have no special significance to the Java programming language. Since you declared a variable as "int hour", the word "hour" only represents an integer value, but has no extra significance beyond that.
Next, you have "hour >= 06:59:00:00" which I suspect is the main cause for your compiler errors. Since hour is declared as an int, you can only compare it to another integer (i.e. a number). Beyond that, the : character can only be used in a few places, and this isn't one of them.
So to fix this problem, you will need to follow Barry's suggestion above and use the GregorianCalendar class. This will allow you to extract the hour from the current date and time.
If you still have questions, please come back and show us what you have tried. We will be glad to help further if needed.
Thank you Barry and Layne for your valuable input. Thanks to your help, I was able to make some inroads with my problem, but I still have 4 errors in my program:
Here it is:
Command Results for HelloDate2.java:
C:\java-CK\HelloDate2.java:22: illegal start of type if (new Date) =06 && date <=12)) ^ C:\java-CK\HelloDate2.java:24: <identifier> expected morning = 'A'; ^ C:\java-CK\HelloDate2.java:29: illegal start of type if (new Date) >=13 && date <= 18)) ^ C:\java-CK\HelloDate2.java:31: <identifier> expected afternoon = 'B'; ^ 4 errors
Tool completed with exit code 1
Here are the 4 errors:
Command Results for HelloDate2.java:
C:\java-CK\HelloDate2.java:22: illegal start of type if (new Date) =06 && date <=12)) ^ C:\java-CK\HelloDate2.java:24: <identifier> expected morning = 'A'; ^ C:\java-CK\HelloDate2.java:29: illegal start of type if (new Date) >=13 && date <= 18)) ^ C:\java-CK\HelloDate2.java:31: <identifier> expected afternoon = 'B'; ^ 4 errors
Tool completed with exit code 1
I am looking forward to receive your answers. Thank you in advance: Charles.
I just realized that I sent you the results twice, but not the actual program.
Here it is:
Command Results for HelloDate2.java:
C:\java-CK\HelloDate2.java:22: illegal start of type if (new Date) =06 && date <=12)) ^ C:\java-CK\HelloDate2.java:24: <identifier> expected morning = 'A'; ^ C:\java-CK\HelloDate2.java:29: illegal start of type if (new Date) >=13 && date <= 18)) ^ C:\java-CK\HelloDate2.java:31: <identifier> expected afternoon = 'B'; ^ 4 errors
My advice is to worry about one error at a time. When you fix the first one, the rest of the errors are likely to change to something completely different.
With that in mind, the first error says that there is a problem with the line that says "if (new Date) =06 && date <=12))".
There are many problems with this line. However, since the ^ points to the if statement, the compiler is actually complaining about something else entirely. Notice that you have a } on the line just before this. The } is the end of the main() method, so the compiler expects a declaration for either a variable or another method. Since "if" is neither of these, the compiler complains. I think you need to remove the spurious }.
Of course, this will lead to a bunch of new errors, but I will let you see what they are before I dicuss any other problems in your code.
Memo to Layne: Thank you very much for your swift reply. I reconstructed my program and I took away the bad curly brace. Now, that how my program looks: // File \\586\C\Java-CK\HelloDate2.java (only GEL-IDE has line numbers) // (Text-Pad is better for command results) // Sep.25.05 // //Output:See listing = 4 errors // Trials: 1) with if (date... (no good) // 2) with if (new Date)...twice....(no good) // 3) with if ((Date)... twice (no good) // 4) with if (bartDateFormat... twice (no good) // 5) no curly bracket before if (GOOD!) // 6) use else without "if" // 7) use bartDateFormat at "else section" // 8) use second if instead of "else" // 9) Hint: operator >= cannot be applied to // Java.text.SimpleDateFormat.int bartDateFormat // 10) applied again: if ((new Date) twice...No Good // 11) Hint: operator >= cannot be applied to // 12) java.util.Date.int & new Date() // 13) morning replaced by value.int: VERY GOOD ! Now, only 1 error // which is: line 28: cannot find symbol "class string" // "public static void (string[] args) {" // ------------------------------------------------------------------------- import java.text.SimpleDateFormat; import java.util.Date;
public class HelloDate2 {
public static void main (string[] args) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("H"); // "H" stands for hour and gives 12, 14 or 18, no minutes
Date date = new Date();
int value;
if (value >=06 && value <=12) //morning = 'A'; { System.out.println ("Good Morning, it's: ");bartDateFormat.format(date); }
if (value >=13 && value <= 18)
// afternoon = 'B'; { System.out.println ("Good Afternoon, it's: ");bartDateFormat.format(date); } } } ----------------------- and the command result is only 1 error (cannot find symbol class string, which I don't understand what they mean).
Java:Line 28: cannot find symbol symbol: class string location: class HelloDate2 public static void main(string[] args) { ^
I am still not able to create a class file, on account of that error
Can you help me please? Thank you very much for your valuable help.
Originally posted by Charles Keller: Java:Line 28: cannot find symbol symbol: class string location: class HelloDate2 public static void main(string[] args) { ^
Remember that Java is case-sensitive. Have a look at the declaration of main() in any other program and you should see the difference.
Thanks to Barry, Layne and Kym, I was finally able to get my program working and I called it now: "DatecomparedtoGregDate.java". Here it is:
// File \\586\C\Java-CK\DatecomparedtoGregDate.java // (Text-Pad is best for editing and printing) // (only GEL has line numbers) // (Dos Box is best for Compiling and RUN) // Sep.27.05 // Output: // RUN: it says: "Good Morning, it's :" , but no Date and Hours // Trial: instead of: bartDateFormat.format(new Date)); // I changed it to: + new Date()); // Result: it worked ! // -----------------------------------------------------------------------
public class DatecomparedtoGregDate { public static void main (String[] args) { SimpleDateFormat bartDateFormat = new SimpleDateFormat("H"); // "H" stands for hour and gives 06, 14 or 21, no minutes.
Date date = new Date();
int value = 06;
if (value >=06 && value <=12)
{ System.out.println("Good Morning, Today it's: " + new Date()); }
Greetings. I'm starting to play with dates too. There's a really nice intro to the GregorianCalendar class in AgileJava page 95; the class seems daunting but it's not that bad to use. Date has been deprecated.
I though my revised program �DatacomparedtoGregDate2..java� worked, but it doesn�t. I noticed that the first part, which says: if �value >6= && value <=12)� works, but not the second part, which says: if �value >13= && value <= 18)� does not.
I am at a loss to understand why it does not work. I even tried another program called �DatecomparedtoGregDate3.java� which does not work at all.
What do I do wrong?
Here is the revised program, which does work at all: // File \\586\C\Java-CK\DatecomparedtoGregDate3.java - Oct.1.05 // Output: // RUN: it says: "Good Morning, it's :" , but no Date and Hours // Trial: instead of: bartDateFormat.format(new Date)); // I changed it to: + new Date()); // Result: it worked ! // New Trial: instead of int value = 6; // changed to: char value = '6'; // in order to make comparison using >= and <= // Result: it worked very well ! // New Trial: I should be able to compare // the hour of "(value >=6 && value <12)" // or the hour of "(value >=13 && value >=18)" // to the hour of the Gregorian Calendar, which is // new SimpleDateFormat("H"); // Oct.1.05 = 6 to 12 seems to work, // it compiles and it runs... // but at 19.30 H. it stays at "Good Morning"..... // ----------------------------------------------------------------------- import java.text.SimpleDateFormat; import java.util.Date;
public class DatecomparedtoGregDate3 { public static void main (String[] args) { SimpleDateFormat bartDateFormat = new SimpleDateFormat("H"); // "H" stands for hour and gives 06, 14 or 21, no minutes. Date date = new Date(); char value; if (new SimpleDateFormat ("H") || (value >=6 && value <=12)) { System.out.println("Good Morning, Today it's: " + new Date()); } else if (new SimpleDateFormat ("H") && (value >=13 && value <= 18)) { System.out.println("Good Afternoon, Today it's: " + new Date()); } else if (new SimpleDateFormat ("H") && (value >19 && value >=23)) { System.out.println("Good Evening, Today it's: " + new Date()); } else System.out.println("Good Night, sleep well, see you Tomorrow: " + new Date()); } }
I though my revised program �DatacomparedtoGregDate2..java� worked, but it doesn�t. I noticed that the first part, which says: if �value >6= && value <=12)� works, but not the second part, which says: if �value >13= && value <= 18)� does not.
I am at a loss to understand why it does not work. I even tried another program called �DatecomparedtoGregDate3.java� which does not work at all.
What do I do wrong?
Here is the revised program, which does work at all: // File \\586\C\Java-CK\DatecomparedtoGregDate3.java - Oct.1.05 // Output: // RUN: it says: "Good Morning, it's :" , but no Date and Hours // Trial: instead of: bartDateFormat.format(new Date)); // I changed it to: + new Date()); // Result: it worked ! // New Trial: instead of int value = 6; // changed to: char value = '6'; // in order to make comparison using >= and <= // Result: it worked very well ! // New Trial: I should be able to compare // the hour of "(value >=6 && value <12)" // or the hour of "(value >=13 && value >=18)" // to the hour of the Gregorian Calendar, which is // new SimpleDateFormat("H"); // Oct.1.05 = 6 to 12 seems to work, // it compiles and it runs... // but at 19.30 H. it stays at "Good Morning"..... // ----------------------------------------------------------------------- import java.text.SimpleDateFormat; import java.util.Date;
public class DatecomparedtoGregDate3 { public static void main (String[] args) { SimpleDateFormat bartDateFormat = new SimpleDateFormat("H"); // "H" stands for hour and gives 06, 14 or 21, no minutes. Date date = new Date(); char value; if (new SimpleDateFormat ("H") || (value >=6 && value <=12)) { System.out.println("Good Morning, Today it's: " + new Date()); } else if (new SimpleDateFormat ("H") && (value >=13 && value <= 18)) { System.out.println("Good Afternoon, Today it's: " + new Date()); } else if (new SimpleDateFormat ("H") && (value >19 && value >=23)) { System.out.println("Good Evening, Today it's: " + new Date()); } else System.out.println("Good Night, sleep well, see you Tomorrow: " + new Date()); } }
Originally posted by James Hejmanowski: Greetings. I'm starting to play with dates too. There's a really nice intro to the GregorianCalendar class in AgileJava page 95; the class seems daunting but it's not that bad to use. Date has been deprecated.
No, the Date class is not deprecated. Only certain methods in the Date class have been deprecated, not the whole class. It is still okay to use Date. The way I think about it is that Date is to Calandar as String is to StringBuffer. In otherwords, you use Calendar to mainpulate Date objects, whereas Dates should be treated as immutable just like Strings are.
Charles, it looks like you are still struggling with some basic program construction issues, and the complexities of the GregorianCalendar class are not going to help you much. So here is some code which shows you how to get started. You will need to add some more if statements to complete the required logic.
Memo to Kym: Thank you very much for your most valuable help. I will work on your program and hopefully I will make some progress with my grasp of the complexity of the Gregorian Calendar. Charles.
Memo to Kym: I followed your advice and made a revised program called HourExampleCKH3.java. It works fine, but I am just wondering if I could use the SimpleDateFormat as new Date in the first part of my comparison (before &&) and the HourOfDay in the second part (after &&) or would I have difficulty with "char" in the first part and "int" in the second part, or can I convert the "char" into an integer?
Here is the new program:
//File: HourExampleCKH3.java // Created: Oct.07.05 // Output: compiling & running on DOS Box: fine // Output #1 = from SimpleDATEfORMAT: // date "H" gives 16 or whatever the hour is. // #2 = from new GregorianCalendar: // before 11 H.=prints "Good morning" on every hour // between 12 and 17 H= prints "Good afternoon" on every hour // on or after 18 H+ prints "Good evening" on every hour //------------------------------------------------------------------------- import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; import java.util.GregorianCalendar;
public class HourExampleCKH3 {
public static void main (String[] args) {
SimpleDateFormat bartDateFormat = new SimpleDateFormat("H");
Date date = new Date();
{System.out.println("File Name: 'HourExampleCKH3.java' - Oct.7.05"); System.out.println("To print this page, add >prn to your java command"); System.out.println(); System.out.println("From SimpleDateFormat:"); System.out.println(bartDateFormat.format(date)); System.out.println("The hour 'H' became: " + bartDateFormat.format(date)); System.out.println();} Calendar calendar = new GregorianCalendar();
System.out.println("From the new Gregorian Calendar:"); System.out.printf("The current time is %1$tH:%1$tM%1$tp\n", calendar); System.out.println("Testing over 24 hours..."); System.out.println();