超出范围的编译时错误在日期构造函数中

huangapple go评论121阅读模式
英文:

getting Out of Range Compile Time Error in the Date Constructor

问题

  1. 我从 API 调用中得到一个日期值 1598331600000
  2. 我正在尝试使用 SimpleDateFormat 将其转换为可读格式
  3. 但是在日期构造函数中我得到了超出范围的编译时错误
  4. 这是我的程序
  5. public class Test {
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8. Calendar cal = Calendar.getInstance();
  9. Date date = new java.sql.Date(1598331600000);
  10. SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMddyyyy");
  11. String formattedDate = sdf.format(date);
  12. System.out.println(formattedDate);
  13. }
  14. }
  15. 请告诉我如何解决这个错误
英文:

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

  1. public class Test {
  2. public static void main(String[] args) {
  3. // TODO Auto-generated method stub
  4. Calendar cal = Calendar.getInstance();
  5. Date date = new java.sql.Date(1598331600000);
  6. SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMddyyyy");
  7. String formattedDate = sdf.format(date);
  8. System.out.println(formattedDate);
  9. }
  10. }

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

答案1

得分: 3

1598331600000没有后缀时被视为int,而此值对于int来说过大了(int可以容纳最大约为20亿的值,即2的31次方减1)。请使用L后缀来表示long类型,long类型可以容纳最大值为2的63次方减1:1598331600000L

英文:

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.

答案2

得分: 2

我建议使用 java-8 日期时间 API,停止使用旧的 CalendarSimpleDateFormat

  1. Instant.ofEpochMilli(1598331600000l)
  2. .atZone(ZoneId.systemDefault())
  3. .format(DateTimeFormatter.ofPattern("MMddyyyy")) //08252020
英文:

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

  1. Instant.ofEpochMilli(1598331600000l)
  2. .atZone(ZoneId.systemDefault())
  3. .format(DateTimeFormatter.ofPattern("MMddyyyy")) //08252020

答案3

得分: 0

您的价值在此被视为整数。

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

构造函数可以接受长整型数值。像这样:

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

因此它会抛出错误。

尝试运行这段代码:

  1. import java.util.*;
  2. import java.text.SimpleDateFormat;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Calendar cal = Calendar.getInstance();
  6. /*long millis = System.currentTimeMillis(); //<---- 这也可以工作
  7. Date date = new java.sql.Date(millis);*/
  8. Date date = new java.sql.Date(1598331600000L); //<--- 在这里查看L表示长整型
  9. SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMddyyyy");
  10. String formattedDate = sdf.format(date);
  11. System.out.println(formattedDate);
  12. }
  13. }

输出:

  1. 08252020
英文:

Your value here is treated as integer.

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

The constructor can take Long values. Like this :

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

Hence it is throwing the error.

Try out this code :

  1. import java.util.*;
  2. import java.text.SimpleDateFormat;
  3. public class Main{
  4. public static void main(String[] args) {
  5. Calendar cal = Calendar.getInstance();
  6. /*long millis=System.currentTimeMillis(); &lt;---- This also works
  7. Date date = new java.sql.Date(millis);*/
  8. Date date = new java.sql.Date(1598331600000L); // &lt;---Look the L for Long here
  9. SimpleDateFormat sdf = new java.text.SimpleDateFormat(&quot;MMddyyyy&quot;);
  10. String formattedDate = sdf.format(date);
  11. System.out.println(formattedDate);
  12. }
  13. }

Output :

  1. 08252020

huangapple
  • 本文由 发表于 2020年8月22日 00:17:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63526474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定