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

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

getting Out of Range Compile Time Error in the Date Constructor

问题

我从 API 调用中得到一个日期值 1598331600000

我正在尝试使用 SimpleDateFormat 将其转换为可读格式

但是在日期构造函数中我得到了超出范围的编译时错误

这是我的程序

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);
    }
}
请告诉我如何解决这个错误
英文:

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 .

答案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

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

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

答案3

得分: 0

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

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

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

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

因此它会抛出错误。

尝试运行这段代码:

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

public class Main {
    public static void main(String[] args) {
        
        Calendar cal = Calendar.getInstance();
        /*long millis = System.currentTimeMillis();  //<---- 这也可以工作
        Date date = new java.sql.Date(millis);*/
        Date date = new java.sql.Date(1598331600000L);  //<--- 在这里查看L表示长整型
        SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMddyyyy");
        String formattedDate = sdf.format(date);
        System.out.println(formattedDate);
    }
}

输出:

08252020
英文:

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();  &lt;---- This also works
        Date date = new java.sql.Date(millis);*/
        Date date = new java.sql.Date(1598331600000L);  // &lt;---Look the L for Long here
        SimpleDateFormat sdf = new java.text.SimpleDateFormat(&quot;MMddyyyy&quot;);
        String formattedDate = sdf.format(date);
        System.out.println(formattedDate);
	}
}

Output :

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:

确定