Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to compare (Hour) with actual new Date() ?

Status
Not open for further replies.

cgkeller

Programmer
Apr 12, 2000
200
CA
File \\586\E\Data\Java-doc\*.*
----------------------------------------------------------

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……..”

Your help will be very much appreciated.

Charles.


 
To Diancecht:
Thank you for your input. The article that you mentioned is very good and it helped me.

I changed the wording of my initial program and I'm now down to 1 error.

Here is the code:
// 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 here are the command results (with Text-Pad):
C:\java-CK\HelloDate2.java:28: cannot find symbol
symbol : class string
location: class HelloDate2
public static void main (string[] args) {
^
1 error

Tool completed with exit code 1

NOTE: I don't understand what it means:
"symbol: class string"

What shall I change?
Thank you very much for your innput.
Charles.
 
Java is case sensitive - its String , not string .

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
BTW, that code is never going to work - you declare an int named "value", and then attempt to compare it to some other values - but there is no actual value in the variable.
Also, I think you are really misunderstanding what primitive data types actually are.

I mean, an int is a whole number with no decimals, which has a signed size of 32 bits (or 4 bytes).

"06" is just like "6" - you can have no leading zeros in integers.

In your first example, you attempted to compare an int to "12:59:00:00" - which clearly is not an integer !

I think you should go away and read about some basic programming concepts ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Memo to sedj:
You said: "that code is never going to work", whell I beg to differ, because I made a few changes and now it works like a charm.

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 !
// -----------------------------------------------------------------------

import java.text.SimpleDateFormat;
import java.util.Date;

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());
}

if (value >=13 && value <= 18)

{
System.out.println ("Good Afternoon, Today it's: " + new Date());
}

if (value >=19 && value <=23)

{
System.out.println ("Good Evening, Today it's: " + new Date());
}
}

}

---------

What do you say now ?

Charles.
 
He meant "that code", now you're running "this code" :p


Cheers,
Dian
 
What do I say now ?!!

I still say you're code will not work.

It may compile, and it may run ... but the code is still flawed.

You declare :

Code:
int value = 06;

There are two problems with this.
1) What is the point of initializing the variable with "06" ? As I said before, I think you need to understand primitive int data types. You should declare the value as "6", not "06" ...

2) [and more worrying this one] You hard code the "value" variable, and then perform some conditional statements on that variable. But you have hardcoded it ! So whats the point of that ? The only point in performing conditional statements is if the value is dynamic - which its not as you have hardcoded it !

You're programme may as well be written as such (because this is the only outcome that will ever be true ) :
Code:
public class DatecomparedtoGregDate {
  public static void main (String[] args) {
    System.out.println("Good Morning, Today it's: " + new java.util.Date());
  }
}


--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
What do you say now ?
Perhaps he should say "you're welcome" because it's his suggestion that got your code to compile in the first place.

I see you neglected to say thanks tho.

(even tho he's 10000% correct in saying that your code is flawed, perhaps you should ask yourself this - according to your code when is "value" ever going to equal anything other than 6?)

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top