Recipe Name:
Rewrite getMillis() to .toInstant().toEpochMilli()
Description:
Rewrite getMillis() to .toInstant().toEpochMilli()
Level:
warning
Language:
  • java
Tags:
  • framework specific
  • java.time
  • Joda-Time
  • quality
Documentation

getMillis() is not available in java.time

Joda-Time provided a getMillis() method which returned the milliseconds since epoch.

java.time does not provide this method however the same result can be achieved by converting to an Instant and using the Instant's toEpochMilli() method.

int epochMillis = zonedDateTime.toInstant().toEpochMilli();
Recipe
id: scw:java.time:Joda-Time:DateTime-getMillis
version: 10
metadata:
  name: Rewrite getMillis() to .toInstant().toEpochMilli()
  shortDescription: Rewrite getMillis() to .toInstant().toEpochMilli()
  level: warning
  language: java
  enabled: true
  comment: |
    Joda-time DateTime provided a getMillis() method used to get the Milliseconds since Epoch.
    In java.time this method is no longer available.
    To achieve the same result you can first convert the ZonedDatetime or OffsetDateTime to an Instant, and then use the Instant's toEpochMilli() method.

    This recipe is designed to match on broken code as part of an overall migration from Joda-Time to java.time.
    After an Joda-Time object has been migrated to a java.time equivalent, some subsequent method calls may become invalid as they are no longer provided by java.time.
    This recipe is designed to match on one of those method calls and provide a fix.
  descriptionFile: descriptions/getMillis.html
  tags: framework specific;java.time;Joda-Time;quality
search:
  methodcall:
    argCount: 0
    name: getMillis
    anyOf:
    - type: java.time.ZonedDateTime
    - type: java.time.OffsetDateTime
availableFixes:
- name: Rewrite using java.time .toInstant().toEpochMilli()
  actions:
  - rewrite:
      to: '{{{ qualifier }}}.toInstant().toEpochMilli()'