Extracting date, hour and minute from Instant.now() generated date
Asked Answered
H

2

18

I have this code that generates a date and time,

ZoneId z = ZoneId.of( "Africa/Nairobi" );
Instant instant = Instant.now();
ZonedDateTime zdt = instant.atZone(z);
return zdt.toString();
        
//2018-03-19T09:03:22.858+03:00[Africa/Nairobi]

Is there a lib like chrono - https://docs.oracle.com/javase/8/docs/api/java/time/temporal/ChronoField.html field that I can use to get the date, hour and minute?

Chrono fields does not extract the complete date.

Herv answered 19/3, 2018 at 6:45 Comment(1)
What do you mean by "get the date"? You want the day, month and year in separate calls?Lepsy
N
24

Since you seem to have been confused about how to get the date from your ZonedDateTime, I should like to supplement Claudiu Guja’a good and correct answer.

    ZoneId z = ZoneId.of("Africa/Nairobi");
    ZonedDateTime zdt = ZonedDateTime.now(z);
    System.out.println("Date         " + zdt.toLocalDate());
    System.out.println("Year         " + zdt.getYear());
    System.out.println("Month        " + zdt.getMonth());
    System.out.println("Day of month " + zdt.getDayOfMonth());

This just printed:

Date         2018-03-19
Year         2018
Month        MARCH
Day of month 19

Please check the documentation for more methods including getMonthValue for the number of the month (1 through 12). I include a link at the bottom. Since ZonedDateTime class has a now method, you don’t need Instant.now() first.

If you wanted an old-fashioned java.util.Date object — first answer is: don’t. The modern API you are already using is much nicer to work with. Only if you need a Date for some legacy API that you cannot change or don’t want to change just now, get an Instant and convert it:

    Instant instant = Instant.now();
    Date oldfashionedDateObject = Date.from(instant);
    System.out.println("Old-fashioned java.util.Date " + oldfashionedDateObject);

This printed:

Old-fashioned java.util.Date Mon Mar 19 12:00:05 CET 2018

Even though it says CET for Central European Time in the string, the Date does not contain a time zone (this confuses many). Only its toString method (called implicitly when I append the Date to a String) grabs the JVM’s time zone setting and uses it for generating the String while the Date stays unaffected.

In the special case where you just want a Date representing the date-time now, again, it’s ill-advised unless you have a very specific need, it’s very simple:

    Date oldfashionedDateObject = new Date();

The result is the same as above.

Links

Namhoi answered 19/3, 2018 at 11:0 Comment(1)
Thank you for the credit. Sadly, I did not notice that I had questions on an answer, otherwise I would have answered myself...Loupgarou
L
2

ZonedDateTime has methods such as getHour(), getMinute(), getMonth, getYear, getdayOfMonth and more such, which should suffice. You can simply get the year, month and day of month using their respective methods.

If you want all of this data in a formatted string, you can format the ZonedDateTime using a DateTimeFormatter to get a string to you liking. Or, there is always the LocalDate you can get from the ZonedDateTime using its toLocalDate method.

Loupgarou answered 19/3, 2018 at 6:47 Comment(6)
I see, but no date anywhere.Herv
ok, then how about the .getMonth, .getYear and .getdayOfMonth methods? or do you want all of this data in a formattable string?Loupgarou
I hope you are not asking for java.util.Date.Wellhead
Date is - simple millisecondGazehound
@ClaudiuGuja I just want to extract the date now that i can extract hour and minute. It now seems i need to get date outside of Instant.now()Herv
and in what format do you want to extract the date? you can simply get the year, month and day of month using thier respective methods, or you can format the ZonedDateTime using a DateTimeFormatter to get a string to you liking. Or, there is always the LocalDate you can get from the ZonedDateTime. What exactly do you want?Loupgarou

© 2022 - 2024 — McMap. All rights reserved.