Home > Net >  How can I add 18 as day to current month using java Instant
How can I add 18 as day to current month using java Instant

Time:12-02

I can get the current date using

 Instant.now()

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

CodePudding user response:

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.

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 ) ;

CodePudding user response:

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.

CodePudding user response:

In Java, you can use the Instant class from the java.time package to represent a point in time. To get the 18th of the current month, you can use the now method of the Instant class to get the current time, then use the with method to set the day of the month to 18. Here is an example:

Instant instant = Instant.now().with(TemporalAdjusters.dayOfMonth(18));

This code gets the current time, then sets the day of the month to 18 using the with method and a TemporalAdjuster for the day of the month. The resulting Instant object represents the 18th of the current month at the current time.

Keep in mind that this code only works if the current day of the month is greater than or equal to 18. If the current day of the month is less than 18, this code will set the day of the month to 18 in the next month, not the current month. To ensure that the Instant object always represents the 18th of the current month, you can use the with method to set the day of the month to 18 and the hour of the day to 0, like this:

Instant instant = Instant.now()
    .with(TemporalAdjusters.dayOfMonth(18))
    .with(ChronoField.HOUR_OF_DAY, 0);

This code sets the day of the month to 18 and the hour of the day to 0, which ensures that the Instant object always represents the 18th of the current month, regardless of the current time.

  • Related