I can get the current date using
Instant.now()
I am looking to get 18-<current month>-<current year>
I can get the current date using
Instant.now()
I am looking to get 18-<current month>-<current year>
On
I endorse Basil Bourque's answer. However, if you are looking for an Instant object, you can get it by adjusting the current OffsetDateTime at UTC to 18th day of the month as shown below:
public class Main {
public static void main(String[] args) {
Instant thisInstantOn18th = OffsetDateTime.now(ZoneOffset.UTC)
.with(ChronoField.DAY_OF_MONTH, 18)
.toInstant();
System.out.println(thisInstantOn18th);
}
}
Output:
2022-12-18T19:19:20.128313Z
Learn more about the modern Date-Time API from Trail: Date Time.
tl;dr
Details
As Comments by Ole V.V. explain, you are using the wrong class. No need for
Instanthere.LocalDate.YearMonth.Capture the current year-month.
Doing so requires a time zone. For any given moment, the date varies around the globe by time zone. So the current month could be simultaneously “next month” in Tokyo Japan while “last month” in Toledo Ohio.
If you want the current month as seen with an offset of zero hours-minutes-seconds from UTC, use
ZoneOffset.UTCconstant.Apply a day of month to get a date.
Generate text in standard ISO 8601 format.
Generate text in a specific format.