-1

I want to retrieve the current system hour and when the user clicks on Button1 the var "ct" will the the current system hour + 1.

Then, I want that when the var ct = current system hour the program sets a textfield text as "Done" but I can't seem to be able to work with the time.

I have in main class:

Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);
int ct=0;


When user click Button1
  name = jTextField1.getText();
         ct = minute + 1;     
        jLabel3.setText("Ends at "+ct);

Now how can I permanently run a method to check if ct = current hour?

abhi
  • 1,760
  • 1
  • 24
  • 40
Ângelo
  • 71
  • 2
  • 3
  • 12
  • 1
    What exactly is your problem with this? Please write the error you are having. – Carsten Hoffmann Jan 13 '14 at 00:40
  • Hello, as i said in the prelast sentence:Now how can i permanently run a method to check if ct = current hour? – Ângelo Jan 13 '14 at 00:41
  • 1
    Your question is unclear. Are you trying to implement a timer of some kind? – Radiodef Jan 13 '14 at 00:43
  • I believe what you mean is _"continuously"_ not _"permanently"_. you need [timer](http://docs.oracle.com/javase/7/docs/api/java/util/Timer.html) – Baby Jan 13 '14 at 00:44
  • We could say so. Imagine a browser game, you want to build a car but it takes 2 hours to build the car, so yes, you get the current time (example: 12h) and then you must check if since 12h has already passed 2h and if it has it will (in this case) change the message at jTextField1 to "Done!" :) – Ângelo Jan 13 '14 at 00:45
  • As @RafaEl has pointed out, with java.util.Timer you can just schedule a task for 2 hours in the future. – Radiodef Jan 13 '14 at 00:47

3 Answers3

2

Java's date/time handling is pretty well acknowledged to be pretty bad. If you have the freedom, you're much better of with the Joda-Time library.

DateTime now = new DateTime();
int hour = now.getHourOfDay();

To me, it's a lot more readable than what Java's date/time provides. Plus there' support for actual time durations, date only, time only, etc...

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Matt
  • 11,523
  • 2
  • 23
  • 33
  • Or one line: `int hour = DateTime.now().getHourOfDay();` – Basil Bourque Jan 13 '14 at 07:16
  • FYI, the [Joda-Time](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Feb 20 '17 at 06:54
1

Method to check if ct==current-hour

public static boolean compareToHour(int arg)
{
    Calendar temp = Calendar.getInstance();
    int tempHour = temp.get(Calendar.HOUR_OF_DAY);
    return arg==tempHour;

}

To compare the two, run the following method anywhere: compareToHour(ct)

mjkaufer
  • 4,047
  • 5
  • 27
  • 55
  • FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Feb 20 '17 at 06:54
0

tl;dr

ZonedDateTime.now( ZoneId.of( "America/Montreal" ) )
             .getHour()

Using java.time

The modern way is with the java.time classes.

Get current moment in UTC.

Instant instant = Instant.now(); 

Adjust into the desired/expected time zone. You can check for the JVM’s current default time zone. But know that the default can be changed at any moment during runtime by any code in any thread of any app within the JVM. So if critical, specify the desired/expected time zone, asking the user if need be.

Apply a ZoneId to get a ZonedDateTime object.

ZoneId z = ZoneId.of( "America/Montreal" );  // Or ZoneId.systemDefault()
ZonedDateTime zdt = instant.atZone( z );

You can interrogate for the hour.

int hour = zdt.getHour(); 

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154