0

I can get the current date using

 Instant.now()

I am looking to get 18-<current month>-<current year>

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Cork Kochi
  • 1,783
  • 6
  • 29
  • 45
  • 1
    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) – Jesse Dec 01 '22 at 17:31
  • 2
    `YearMonth.now(ZoneId.systemDefault()).atDay(18)`. – Ole V.V. Dec 01 '22 at 17:33
  • 3
    An `Instant` cannot be used for a date, a day. An `Instant` is, as the name says, a point in time. At that point in time the time zones of the world are on two if not three different dates. – Ole V.V. Dec 01 '22 at 17:34
  • What Ole said is right. But assuming you want the 18th according to UTC, then `instant.atZone(ZoneOffset.UTC).withDayOfMonth(18).toInstant()`. Or maybe you should be dealing with ZonedDateTime all along. Really depends on what you're using it for – Michael Dec 01 '22 at 17:37
  • I tried Instant.from(YearMonth.now(ZoneOffset.UTC).atDay(18)) – Cork Kochi Dec 01 '22 at 17:38
  • 1
    @CorkKochi Sorry to be blunt, but that code in the comment is gibberish. YearMonth.atDay gives you a LocalDate. You can't create an Instant from a date on its own. An Instant is more than a date. Seems you are not really understanding the different concepts these classes are designed to represent. I'd recommend reading through this: https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html – Michael Dec 01 '22 at 17:40
  • If you insist on an `Instant`, then you need to specify exactly which instant of the 18th of the month you want. There are approx. 175 trillion `Instant`s to choose from. And regularly there are also two months to choose from as “current month”. – Ole V.V. Dec 01 '22 at 19:22

2 Answers2

3

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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • That’s a possibility. We don’t know whether the OP wants UTC )I might think it unlikely), and we don’t know whether they want the current time of day in UTC on that day (as you are giving them). Maybe they do. – Ole V.V. Dec 01 '22 at 20:45
  • 1
    @OleV.V. This is a valid point. However, my answer is based on this assumption that the OP wanted to see the 18th as the day-of-month in the output, keeping other units unchanged. That's why I chose UTC. – Arvind Kumar Avinash Dec 01 '22 at 20:54
1

tl;dr

YearMonth                             // Represents a year and month only, no day of month. 
.now( 
    ZoneId.of( "America/Edmonton" )   // Returns a `ZoneId` object. 
)
.atDay( 18 )                          // Returns a `LocalDate` object. 
.format(
    DateTimeFormatter
    .ofPattern( "dd-MM-uuuu" )
)                                     // Returns a `String` object. 

Details

As Comments by Ole V.V. explain, you are using the wrong class. No need for Instant here.

  • To represent a date, use LocalDate.
  • To represent a year and month, use 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.

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
YearMonth ym = YearMonth.now( z ) ;

If you want the current month as seen with an offset of zero hours-minutes-seconds from UTC, use ZoneOffset.UTC constant.

YearMonth ym = YearMonth.now( ZoneOffset.UTC ) ;

Apply a day of month to get a date.

LocalDate ld = ym.atDay( 18 ) ;

Generate text in standard ISO 8601 format.

String output = ld.toString() ;

Generate text in a specific format.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd-MM-uuuu" ) ;
String output = ld.format( f ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154