Consider the below code
Instant instant = Instant.now();
System.out.println(instant);
RESULT:
2020-01-13T09:01:06.405Z
Now from the above result I want get the current hour and current minutes.
Consider the below code
Instant instant = Instant.now();
System.out.println(instant);
RESULT:
2020-01-13T09:01:06.405Z
Now from the above result I want get the current hour and current minutes.
instant.atZone(ZoneOffset.UTC).getMinute()
and
instant.atZone(ZoneOffset.UTC).getHour()
(That's for UTC; otherwise choose your time zone).
If you want to know the single parts of an Instant
for the time zone of your system, then do this:
public static void main(String[] args) {
Instant instant = Instant.now();
// convert the instant to a local date time of your system time zone
LocalDateTime ldt = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
int day = ldt.getDayOfMonth();
int month = ldt.getMonthValue();
int year = ldt.getYear();
int hour = ldt.getHour();
int minute = ldt.getMinute();
int second = ldt.getSecond();
System.out.println("day:\t" + day);
System.out.println("month:\t" + month);
System.out.println("year:\t" + year);
System.out.println("hour:\t" + hour);
System.out.println("minute:\t" + minute);
System.out.println("second:\t" + second);
}
On my system, the output was:
day: 13
month: 1
year: 2020
hour: 10
minute: 38
second: 51
Otherwise (if you want to have UTC time zone) use one of the other answers, which are basically the same code but putting a different time zone.
Can you try to do this
System.out.println("Get Hours "+LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).getHour());
System.out.println("Get Minute "+LocalDateTime.ofInstant(Instant.now(), ZoneOffset.UTC).getMinute());