-1

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.

Michael
  • 41,989
  • 11
  • 82
  • 128
Ren
  • 41
  • 1
  • 1
  • 2
  • How have you tried to get it? – deHaar Jan 13 '20 at 09:19
  • Have you tried the [documentation for `Instant`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/Instant.html#get(java.time.temporal.TemporalField))? – Federico klez Culloca Jan 13 '20 at 09:19
  • 2
    What have you tried? What didn't work out and why? Did you read the JavaDoc of the classes involved? – Nicktar Jan 13 '20 at 09:19
  • 2
    Does this answer your question? [How to get and set specified time in java.time.Instant?](https://stackoverflow.com/questions/31786450/how-to-get-and-set-specified-time-in-java-time-instant) (Literally the first Google result) – Smutje Jan 13 '20 at 09:20
  • @Ren Welcome to Stackoverflow! Please take some time to read [How to ask good questions?](https://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Also please try to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your own attempt and show it to us. – Smile Jan 13 '20 at 09:22
  • You can’t. Hour of day and minute of hour will be different for every time zone, and `Instant` hasn’t got a time zone. If you tell us which time zone you want it for, we can show you the conversion that you need. – Ole V.V. Jan 13 '20 at 18:25

3 Answers3

17

instant.atZone(ZoneOffset.UTC).getMinute()

and

instant.atZone(ZoneOffset.UTC).getHour()

(That's for UTC; otherwise choose your time zone).

Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
3

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.

deHaar
  • 17,687
  • 10
  • 38
  • 51
1

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());
Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19