0

I am getting a date value as 1598331600000 from a API call

I am trying to convert this to Readable format using SimpleDateFormat

But i am getting Out of Range Compile Time Error in the Date Constructor

This is my Program

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Calendar cal = Calendar.getInstance();

        Date date = new java.sql.Date(1598331600000);
        SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMddyyyy");

        String formattedDate = sdf.format(date);

        System.out.println(formattedDate);

    }

}

Could you please let me know how to resolve this error .

Pavan
  • 77
  • 1
  • 9
  • 1
    [How can I create a Java 8 LocalDate from a long Epoch time in Milliseconds?](https://stackoverflow.com/questions/35183146/how-can-i-create-a-java-8-localdate-from-a-long-epoch-time-in-milliseconds) – Abra Aug 21 '20 at 16:25
  • What are you using `java.sql.Date` for? That class is a hack on top of the already poorly designed `java.util.Date`, and both are long outdated. Also `java.sql.Date` was originally meant for transferring dates to and from SQL databases, it’s rather pointless to use it for anything else. For a date use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 22 '20 at 05:11

3 Answers3

3

1598331600000 without a suffix is treated as an int, and this value is too big for it (int can hold values up to around 2 billion, 2^31 - 1). Use L suffix for long type, which can hold values up to 2^63 - 1: 1598331600000L.

Andrew Vershinin
  • 1,958
  • 11
  • 16
2

I would recommend to use java-8 date time api, and stop using legacy Calendar, SimpleDateFormat

Instant.ofEpochMilli(1598331600000l)
       .atZone(ZoneId.systemDefault())
       .format(DateTimeFormatter.ofPattern("MMddyyyy")) //08252020
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • Two further recommendations: (1) use underscore as thousand separator (2) use upper case `L` since lower case `l` easily reads as digit `1` (in many fonts). So `1_598_331_600_000L` – Ole V.V. Aug 22 '20 at 08:15
0

Your value here is treated as integer.

Date date = new java.sql.Date(1598331600000);

The constructor can take Long values. Like this :

long millis=System.currentTimeMillis();
Date date = new java.sql.Date(millis);

Hence it is throwing the error.

Try out this code :

import java.util.*;
import java.text.SimpleDateFormat;


public class Main{
    public static void main(String[] args) {
        
        Calendar cal = Calendar.getInstance();
        /*long millis=System.currentTimeMillis();  <---- This also works
        Date date = new java.sql.Date(millis);*/
        Date date = new java.sql.Date(1598331600000L);  // <---Look the L for Long here
        SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMddyyyy");
        String formattedDate = sdf.format(date);
        System.out.println(formattedDate);
    }
}

Output :

08252020
Som
  • 1,522
  • 1
  • 15
  • 48