-1

I get this error whenever I try to remove the decimal point of the Instant datatype in java.

! java.util.IllegalFormatConversionException: M != java.time.Instant

Here is the line of code I am trying to use for the conversion. I am using String.format

subCategory.setSla(Instant.parse(String.format("%tM", Instant.ofEpochSecond(dto.getSla().atZone(ZoneOffset.UTC).getMinute()))));

Without the string format, I get something like 24.00000

I don't want to see the decimal points after 24, how best do you think, it can be handled?

My dto has something like

private Instant sla;

My dto is returning an Instant, that I am very sure of. I want to display just the minute and send just the minute to users in an email. I don't want the default representation with decimal point.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Curious
  • 72
  • 7
  • 2
    [mcve] please ... – Seelenvirtuose May 30 '22 at 14:30
  • 6
    `Instant` does not have a decimal point - it is just an instant in time... its default representation may have a decimal point... but that is not what posted code seems to be retrieving... what exactly do you want to get? – user16320675 May 30 '22 at 14:33
  • 2
    That is purely display related. Even if you would cut it from parsing/displaying, it would still be stored. You can only alter the output with a `DateTimeFormatter` – XtremeBaumer May 30 '22 at 14:33
  • @user16320675 I want to display just the minute and send just the minute to users in an email. I don't want the default representation with decimal point – Curious May 30 '22 at 14:39
  • @user16320675 using String.format will throw an error, my dto is of Instant type. Thus, I am to provide an instant not a string – Curious May 30 '22 at 14:42
  • I don't want to use now as I am getting the Instant from somewhere. Why don't you recommend tho? – Curious May 30 '22 at 14:46
  • String.format("%tM", Instant.now().toEpochMilli()) this won't work for me because of the reasons I stated earlier. – Curious May 30 '22 at 14:49
  • Again, it won't work. My dto has something like `private Instant sla;` doing dto.getSla using the format you provided won't work is all I am saying. My dto is returning an instant, that I am very sure of – Curious May 30 '22 at 14:53
  • An `Instant` hasn’t got a minute of hour. It’s a point of time independent of time zone. To have a minute, you need a time zone. – Ole V.V. May 30 '22 at 15:03
  • 1
    `String.format("%tM", yourInstant.atOffset(ZoneOffset.UTC))` prints `24` without any decimal point. The same does `String.format("%02d", yourInstant.atOffset(ZoneOffset.UTC).getMinute())`. My usual preference would be `yourInstant.atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ofPattern("mm"))`, though. It’s a bit longer, but it’s the standard way of formatting date and time data. – Ole V.V. May 30 '22 at 15:10
  • 1
    If the user is located in Nepal, do you want the minute in Asia/Kathmandu time zine or the minute in UTC? They are not the same. – Ole V.V. May 30 '22 at 15:28

1 Answers1

2

tl;dr

instant                      // Represents a moment as seen in UTC, an offset of zero hours-minutes-seconds. 
.atOffset( ZoneOffset.UTC )  // Returns an `OffsetDateTime` object, converting from `Instant`. 
.getMinute()                 // Returns an `int` primitive value. 

Details

You are conflating date-time objects with text. Classes such as Instant are not text. These classes use their own internally defined representation of date-time values. That representation is not plain text.

Apparently you want to extract just the minute of the hour from an Instant object. That class is a basic building-block class in java.time. So convert to the more flexible OffsetDateTime class.

OffsetDateTime odt = instant.atOffset( ZoneOffset.UTC ) ;

Extract the minute of hour.

int minute = odt.getMinute() ;

Example:

Instant
.parse( "2023-01-23T19:42:07.123456789Z" )
.atOffset( ZoneOffset.UTC )
.getMinute()

42

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154