John Smith John Smith - 2 months ago 11
Java Question

I get false hour and minute in java

I try to get the current time and hour in java:

import java.util.Calendar;
import java.util.Date;
public class Clock2
{
// instance variables - replace the example below with your own
private int minute;
private int hour;
private String daytime;

/**
* Constructor for objects of class Clock2
*/
public Clock2()
{
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
this.hour = Calendar.HOUR_OF_DAY;
this.minute = Calendar.MINUTE;
}


But somehow for now
13.33
i get hour
11
an minute
12
what do i wrong ? Thanks

Answer

Its giving right answer

import java.util.Calendar;
 import java.util.Date;

 public class Clock2 {

public static void main(String[] args) {
    Clock clock = new Clock();
}

}

class Clock {
/**
 * Constructor for objects of class Clock2
 */
private final int minute;
private final int hour;

public Clock() {
    Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    hour = calendar.get(Calendar.HOUR_OF_DAY);
    minute = calendar.get(Calendar.MINUTE);
    System.out.println(hour + " " + minute);
}
 }
Comments