Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 222
Gandalf Avatar asked Nov 27 '25 18:11

Gandalf


2 Answers

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

  • ZonedDateTime documentation
  • All about java.util.date
like image 164
Ole V.V. Avatar answered Nov 30 '25 09:11

Ole V.V.


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.

like image 43
Claudiu Guja Avatar answered Nov 30 '25 07:11

Claudiu Guja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!